美文网首页
ImageView实现单指拖动双指缩放

ImageView实现单指拖动双指缩放

作者: xin你就是我的小笨蛋 | 来源:发表于2017-04-26 20:58 被阅读0次

    package com.xiao.pandapointerpicture;

    import android.content.Context;

    import android.graphics.Canvas;

    import android.util.AttributeSet;

    import android.view.MotionEvent;

    import android.widget.ImageView;

    /**

    * Created by StarMushroom on 2017/4/26.

    */

    public class MyImageView extends ImageView {

    private float scale = 0.05f;

    // 两点触屏后之间的长度

    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) {

    super(context, attrs);

    }

    @Override

    protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    }

    /**

    * 让图片跟随手指触屏的位置移动 beforeX、Y是用来保存前一位置的坐标 afterX、Y是用来保存当前位置的坐标

    * 它们的差值就是ImageView各坐标的增加或减少值

    *

    * @param event

    */

    public void moveWithFinger(MotionEvent event) {

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:

    beforeX = event.getX();

    beforeY = event.getY();

    break;

    case MotionEvent.ACTION_MOVE:

    afterX = event.getX();

    afterY = event.getY();

    this.setLocation((int) (afterX - beforeX), (int) (afterY - beforeY));

    beforeX = afterX;

    beforeY = afterY;

    break;

    case MotionEvent.ACTION_UP:

    break;

    }

    }

    /**

    * 用来设置ImageView的位置,移动Imageview

    *

    * @param x

    * @param y

    */

    public void setLocation(int x, int y) {

    //TODO 底部还是会滑出去  左上右正常

    //获取设备的宽高

    int widthPixels = getContext().getResources().getDisplayMetrics().widthPixels;

    int heightPixels = getContext().getResources().getDisplayMetrics().heightPixels;

    if (getLeft() + x > 5

    && getTop() + y > 5

    && getRight() + x < widthPixels - 5

    && getBottom() + y < heightPixels-5) {

    this.setFrame(getLeft() + x, getTop() + y, getRight() + x, getBottom() + y);

    }

    }

    /**

    * 通过多点触屏放大或缩小图像 beforeLenght用来保存前一时间两点之间的距离 afterLenght用来保存当前时间两点之间的距离

    *

    * @param event

    */

    public void scaleWithFinger(MotionEvent event) {

    float moveX = event.getX(1) - event.getX(0);

    float moveY = event.getY(1) - event.getY(0);

    //多点触摸判断

    switch (event.getActionMasked()) {

    case MotionEvent.ACTION_POINTER_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;

    // 如果当前时间两点距离大于前一时间两点距离,则传0,否则传1

    if (gapLenght > 0) {

    this.setScale(scale, 0);

    } else if (gapLenght < 0) {

    this.setScale(scale, 1);

    } else if (gapLenght == 0) {

    break;

    }

    beforeLenght = afterLenght;

    break;

    }

    }

    /**

    * 用来放大缩小ImageView.

    *

    * @param scaleSize

    * @param flag      0是放大图片,为1是缩小图片

    */

    private void setScale(float scaleSize, int flag) {

    int scale_x = (int) (scaleSize * getWidth());

    int scale_y = (int) (scaleSize * getHeight());

    switch (flag) {

    case 0:

    if (getWidth() < getContext().getResources().getDisplayMetrics().widthPixels * 3) {

    this.setFrame(

    getLeft() - scale_x,

    getTop() - scale_y,

    getRight() + scale_x,

    getBottom() + scale_y);

    }

    break;

    case 1:

    if (getWidth() > 50 + scale_x * 2) {

    this.setFrame(

    getLeft() + scale_x,

    getTop() + scale_y,

    getRight() - scale_x,

    getBottom() - scale_y);

    }

    break;

    }

    }

    }

    相关文章

      网友评论

          本文标题:ImageView实现单指拖动双指缩放

          本文链接:https://www.haomeiwen.com/subject/ygywzttx.html