简单实现图片的缩放,平移——使用Matrix

使用matrix进行对图片的缩放

  1. layout文件
  2. Activity文件

layout文件代码:

    一个ImageView

   <ImageView  android:id="@+id/image" android:src="@mipmap/ic_launcher" android:scaleType="matrix" android:layout_width="match_parent" android:layout_height="match_parent" />

注意这里的scaleType=”martrix”,是声明图片的缩放模式的,没有这一句,实现不了缩放的效果,而且最好将图片大小设置为match_parent,而且图片的加载不能是设置background,这样是无效的,一定要是src导入进去才行.

MainActivity文件:

public class MainActivity extends AppCompatActivity {

    private ImageView image;
    private Matrix matrix=new Matrix();
    private Matrix currentmatrix=new Matrix();
    private static final  int DRAG=1;
    private static final  int ZOOM=2;
    private int mood=0;
    private PointF startPonit=new PointF();
    private PointF midpoint;
    private float startDis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        image.setOnTouchListener(new onTouchListener());
    }

    private void initView() {
        image = (ImageView) findViewById(R.id.image);

    }

    private class onTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()&MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_DOWN://手指放下
                    mood = DRAG;
                    currentmatrix.set(image.getImageMatrix());
                    startPonit.set(event.getX(), event.getY());
                    break;
                case MotionEvent.ACTION_MOVE ://手指进行移动
                    if (mood==DRAG){
                        float dx=event.getX()-startPonit.x;
                        float dy=event.getY()-startPonit.y;
                        matrix.set(currentmatrix);
                        matrix.postTranslate(dx,dy);//平移缩放模式,是postTranslate
                    }else if (mood==ZOOM){
                        float endis=distance(event);
                        if (endis>10f) {
                            float scale = endis / startDis;//缩放比例
                            Log.i("jae_scale", String.valueOf(scale));
                            matrix.set(currentmatrix);
                            matrix.postScale(scale, scale, midpoint.x, midpoint.y);
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP://手指抬起
                    mood=0;
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    mood=0;
                    break;
                case MotionEvent.ACTION_POINTER_DOWN://屏幕上已经有一根手指,再放下一根
                    mood=ZOOM;
                    startDis=distance(event);
                    if (startDis>10f) {
                        midpoint = mid(event);
                        currentmatrix.set(image.getImageMatrix());
                    }
                    break;
            }
            image.setImageMatrix(matrix);
            return true;//这里一定要是return true 不然也是无效的
        }
        //取中点
        private PointF mid(MotionEvent event) {
            float midx=event.getX(1)+event.getX(0);
            float midy=event.getY(1)+event.getY(0);
            return new PointF(midx/2,midy/2);
        }
        //取手指滑动距离
        private float distance(MotionEvent event) {
            float dx=event.getX(1)-event.getX(0);
            float dy=event.getY(1)-event.getY(0);
            return (float)Math.sqrt(dx*dx+dy*dy);
        }
    }



}

你可能感兴趣的:(Android)