广告
返回顶部
首页 > 资讯 > 移动开发 >android ImageView 的几点经验总结
  • 950
分享到

android ImageView 的几点经验总结

Android 2022-06-06 10:06:46 950人浏览 泡泡鱼
摘要

最近作图片的显示,遇到了些问题,简单总结1)可以用ImageSwicher和ImageView结合在来做,这样会用到setFectory(),华而不实最要命的是如果图片的大小超

最近作图片的显示,遇到了些问题,简单总结
1)可以用ImageSwicher和ImageView结合在来做,这样会用到setFectory(),华而不实
最要命的是如果图片的大小超过屏幕,实现比较困难,目前是没有找到方法

2)最简单的方法是用ImageView,图片直接FIT_CENTER,Android会根据图片的大小自动调节
保持图片的比例。如果图片分辨率超过屏幕,android也会自动的调整到屏幕能放下整张的图片
在放大图片的时候,可以用ImageView的SetFrame() 和setScale()方法,可以把图片放大
到超过屏幕,原理就是ImageView放大,图片跟着放大。同时也是可以添加各种animation.
大致如下:
代码如下:
Animation animation = AnimationUtils.loadAnimation(Main.this, R.anim.my_scale_action);
imageView.setLayoutParams(new Gallery.LayoutParams(206, 206));
imageView.startAnimation(animation);

写一个自己的MyImageView类,代码如下,可以直接用
代码如下:
package com.practice.imageviewpic;

import android.app.Activity; 
import android.content.Context; 
import android.graphics.*; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent; 
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView; 
import android.widget.ImageView.ScaleType; 
import android.widget.LinearLayout;
    //创建一个自己的ImageView类 
    class MyImageView extends ImageView { 
        private float scale = 0.1f; 
      //两点触屏后之间的长度 
        private float beforeLenght; 
        private float afterLenght; 
        //单点移动的前后坐标值 
        private float afterX,afterY; 
        private float beforeX,beforeY; 
        public MyImageView(Context context) { 
            super(context); 
        } 
        public MyImageView(Context context, AttributeSet attrs) { 
        this(context, attrs, 0);
        }
        public MyImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        //用来设置ImageView的位置 
        private void setLocation(int x,int y) { 
            this.setFrame(this.getLeft()+x, this.getTop()+y, this.getRight()+x, this.getBottom()+y); 
        } 
         
         
        public void setScale(float temp,int flag) { 
            if(flag==0) { 
                this.setFrame(this.getLeft()-(int)(temp*this.getWidth()),  
                              this.getTop()-(int)(temp*this.getHeight()),  
                              this.getRight()+(int)(temp*this.getWidth()),  
                              this.getBottom()+(int)(temp*this.getHeight()));    
            }else { 
      &nbs p;         this.setFrame(this.getLeft()+(int)(temp*this.getWidth()),  
                              this.getTop()+(int)(temp*this.getHeight()),  
                              this.getRight()-(int)(temp*this.getWidth()),  
                              this.getBottom()-(int)(temp*this.getHeight())); 
            } 
        } 
        //绘制边框       
         @Override 
          protected void onDraw(canvas canvas) { 
              super.onDraw(canvas);     
              Rect rec=canvas.getClipBounds(); 
              rec.left++;
              rec.top++;
              rec.bottom--; 
              rec.right--; 
              Paint paint=new Paint(); 
              paint.setColor(Color.RED); 
              paint.setStyle(Paint.Style.STROKE); 
              canvas.drawRect(rec, paint); 
          } 
          
         
        public void moveWithFinger(MotionEvent event) { 
            switch(event.getAction()) { 
            case MotionEvent.ACTION_DOWN: 
            //Log.d(TAG, "down ..");
                beforeX = event.getX(); 
                beforeY = event.getY(); 
                break; 
            case MotionEvent.ACTION_MOVE: 
            //Log.d(TAG, "move ..");
                afterX = event.getX(); 
     &nbs p;          afterY = event.getY(); 
                this.setLocation((int)(afterX-beforeX),(int)(afterY-beforeY)); 
                beforeX = afterX; 
                beforeY = afterY; 
                break; 
            case MotionEvent.ACTION_UP: 
            //Log.d(TAG, "up ..");
                break; 
            } 
        } 
         
        public void scaleWithFinger(MotionEvent event) { 
            float moveX = event.getX(1) - event.getX(0); 
            float moveY = event.getY(1) - event.getY(0); 
            switch(event.getAction()) { 
            case MotionEvent.ACTION_DOWN: 
                beforeLenght = (float) Math.sqrt( (moveX*moveX) + (moveY*moveY) ); 
                break; 
            case MotionEvent.ACTION_MOVE: 
                //得到两个点之间的长度 
                afterLenght = (float) Math.sqrt( (moveX*moveX) + (moveY*moveY) ); 
                float gapLenght = afterLenght - beforeLenght; 
                if(gapLenght == 0) { 
                    break; 
                } 
                //如果当前时间两点距离大于前一时间两点距离,则传0,否则传1 
                if(gapLenght>0) { 
                    this.setScale(scale,0); 
                }else { 
                    this.setScale(scale,1); 
                } 
           ;      beforeLenght = afterLenght; 
                break; 
            } 
        } 
   //这里来监听屏幕触控时间 
   @Override 
    public boolean onTouchEvent(MotionEvent event) { 
        
        if(event.getY() > this.getTop() && event.getY() < this.getBottom() 
                && event.getX() > this.getLeft() && event.getX() < this.getRight()) { 
            if(event.getPointerCount() == 2) { 
            this.scaleWithFinger(event); 
            }else if(event.getPointerCount() == 1) { 
            this.moveWithFinger(event); 
            }            
        } 
        return true; 
    }        
}

您可能感兴趣的文章:Android属性动画实现炫酷的登录界面Android属性动画实现布局的下拉展开效果Android加载Gif动画实现代码android imageview图片居中技巧应用ImageView简单加载网络图片实例代码Android控件系列之ImageView使用方法Android开发ImageView图片无法显示解决过程Android实现ImageView图片双击放大及缩小Android使用PowerImageView实现播放强大的ImageView动画效果


--结束END--

本文标题: android ImageView 的几点经验总结

本文链接: https://www.lsjlt.com/news/27440.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作