Android图片ImageView双击缩放并拖动移动位置

先放实例

Android图片ImageView双击缩放并拖动移动位置_第1张图片

图片缩放

借助 Matrix 的缩放功能来达到图片缩放的目的

private Matrix scaleMatrix;
//第一个参数为横向缩放值,第二个参数为纵向缩放值,第三个第四个参数为参考坐标
scaleMatrix.postScale(scale, scale, motionEvent.getX(), motionEvent.getY());
//切记修改控件缩放模式为 MATRIX
mView.imageMvShow.setScaleType(ImageView.ScaleType.MATRIX);
mView.imageMvShow.setImageMatrix(scaleMatrix);

图片移动

同上使用 Matrix 来实现

scaleMatrix.postTranslate(cW, cH);
mView.imageMvShow.setImageMatrix(scaleMatrix);

控件双击功能的实现

按下时的距离和抬起时的距离很小时,视作点击动作,如果两次点击动作的时间间隔很小时,视作双击动作。

private long lastClickTime = 0;    
private final View.OnTouchListener onTouchListener = new View.OnTouchListener() {
     
    private float mPosX;
    private float mPosY;
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
     
        //region 设定控件的滑动事件
        switch (motionEvent.getAction()) {
     
            case MotionEvent.ACTION_DOWN: //按下
                mPosX = motionEvent.getX();
                mPosY = motionEvent.getY();
                break;
            case MotionEvent.ACTION_MOVE: //移动
                break;
            case MotionEvent.ACTION_UP: //抬起
                if (Math.abs(motionEvent.getY() - mPosY) < 10) {
     
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - lastClickTime < 300) {
     
                        //执行双击事件

                    }
                    lastClickTime = currentTime;
                }
                break;
            default:
                break;
        }
        return true;
    }
};

切换图片后恢复初始状态

private void resetScale() {
     
    mView.imageMvShow.setScaleType(ImageView.ScaleType.FIT_CENTER);
    scaleMatrix = new Matrix();
    isScale = false;
    scale = 1.0f;
    mView.ivMvReset.setVisibility(View.GONE);
}

完整代码

private long lastClickTime = 0;   
private Matrix scaleMatrix;
private boolean isScale = false;
private float scale = 1.0f;
private final View.OnTouchListener onTouchListener = new View.OnTouchListener() {
     
    private float dX;
    private float dY;
    private float mPosX;
    private float mPosY;
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
     
        //region 设定控件的滑动事件
        switch (motionEvent.getAction()) {
     
            case MotionEvent.ACTION_DOWN: //按下
                mPosX = motionEvent.getX();
                mPosY = motionEvent.getY();
                dX = motionEvent.getRawX();
                dY = motionEvent.getRawY();
                break;
            case MotionEvent.ACTION_MOVE: //移动
                if (isScale) {
     
                    float nX = motionEvent.getRawX();
                    float nY = motionEvent.getRawY();
                    float cW = nX - dX;
                    float cH = nY - dY;
                    dX = nX;
                    dY = nY;
                    scaleMatrix.postTranslate(cW, cH);
                    mView.imageMvShow.setImageMatrix(scaleMatrix);
                    return true;
                } 
                break;
            case MotionEvent.ACTION_UP: //抬起
                if (Math.abs(motionEvent.getY() - mPosY) < 10) {
     
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - lastClickTime < 300) {
     
                        //执行双击事件
                        if (scale < 2.0f) {
     
                            isScale = true;
                            mView.ivMvReset.setVisibility(View.VISIBLE);
                            scale = scale + 0.25f;     
                            //第一次缩放时,移动图片到中间位置
                            if (scale == 1.25f) {
     
                                int iw = mView.imageMvShow.getWidth();
                                int ih = mView.imageMvShow.getHeight();
                                int dw = mView.imageMvShow.getDrawable().getBounds().width();
                                int dh = mView.imageMvShow.getDrawable().getBounds().height();
                                int cW = (int) (iw / 2 - dw * 1.25f / 2);
                                int cH = (int) (ih / 2 - dh * 1.25f / 2);
                                scaleMatrix.postScale(scale, scale);
                                scaleMatrix.postTranslate(cW, cH);
                            } else
                                scaleMatrix.postScale(scale, scale, motionEvent.getX(), motionEvent.getY());
                            mView.imageMvShow.setScaleType(ImageView.ScaleType.MATRIX);
                            mView.imageMvShow.setImageMatrix(scaleMatrix);
                        } else resetScale();
                    }
                    lastClickTime = currentTime;
                }
                break;
            default:
                break;
        }
        return true;
    }
};
//恢复初始状态
private void resetScale() {
     
    mView.imageMvShow.setScaleType(ImageView.ScaleType.FIT_CENTER);
    scaleMatrix = new Matrix();
    isScale = false;
    scale = 1.0f;
    mView.ivMvReset.setVisibility(View.GONE);
}

你可能感兴趣的:(Android,imageview,scale,matrix,posttranslate,doubleclick)