Android 收缩缩放ImageView时的奇怪行为

Android 收缩缩放ImageView时的奇怪行为,android,imageview,ontouchlistener,pinchzoom,Android,Imageview,Ontouchlistener,Pinchzoom,我有一个ImageView,我在其中添加了一个OnTouchListener,这样我就可以使用两个手指的捏手势放大和缩小后者。我正在使用下面的代码,但是我在调整实际位图的大小时遇到了问题。后者会变得模糊,保持不变大小,并被图像的大小调整版本覆盖,就好像它只是将新图像放在旧图像之上,而不是替换它一样。我认为drawMatrix方法有问题 int touchState; final int IDLE = 0; final int TOUCH = 1; final in

我有一个ImageView,我在其中添加了一个OnTouchListener,这样我就可以使用两个手指的捏手势放大和缩小后者。我正在使用下面的代码,但是我在调整实际位图的大小时遇到了问题。后者会变得模糊,保持不变大小,并被图像的大小调整版本覆盖,就好像它只是将新图像放在旧图像之上,而不是替换它一样。我认为drawMatrix方法有问题

    int touchState;
    final int IDLE = 0;
    final int TOUCH = 1;
    final int PINCH = 2;
    float dist0, distCurrent;

    public boolean onTouch(View view, MotionEvent event) {
        boolean handledHere = false;

        float distx, disty;

        final int action = event.getAction();

        switch(action & MotionEvent.ACTION_MASK){
        case MotionEvent.ACTION_DOWN:
            //A pressed gesture has started, the motion contains the initial starting location.
            touchState = TOUCH;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            //A non-primary pointer has gone down.
            touchState = PINCH;

            //Get the distance when the second pointer touch
            distx = event.getX(0) - event.getX(1);
            disty = event.getY(0) - event.getY(1);
            dist0 = FloatMath.sqrt(distx * distx + disty * disty);

            break;
        case MotionEvent.ACTION_MOVE:
            //A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP).
            if(touchState == PINCH){                        
                //Get the current distance
                distx = event.getX(0) - event.getX(1);
                disty = event.getY(0) - event.getY(1);
                distCurrent = FloatMath.sqrt(distx * distx + disty * disty);

                drawMatrix((ImageView) view);
            } 

            break;
        case MotionEvent.ACTION_UP:
            //A pressed gesture has finished.
            touchState = IDLE;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            //A non-primary pointer has gone up.
            touchState = TOUCH;
            break;

        }

        return handledHere;
    }

    private void drawMatrix(ImageView view){
        float curScale = distCurrent/dist0;
        if (curScale < 0.1){
            curScale = 0.1f;    
        }

        view.buildDrawingCache();
        Bitmap originalBitmap = view.getDrawingCache();
        Bitmap resizedBitmap;    
        int newHeight = (int) (view.getHeight() * curScale);
        int newWidth = (int) (view.getWidth() * curScale);
        resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false);
        view.setImageBitmap(resizedBitmap); 

    }
inttouchtate;
最终int怠速=0;
最终int TOUCH=1;
最终int-PINCH=2;
浮动dist0,distCurrent;
公共布尔onTouch(视图、运动事件){
布尔handlewhere=false;
浮动distx,disty;
final int action=event.getAction();
开关(动作和动作事件.动作屏蔽){
case MotionEvent.ACTION\u DOWN:
//按下的手势已开始,运动包含初始开始位置。
触摸状态=触摸;
打破
case MotionEvent.ACTION\u指针\u向下:
//非主指针已下降。
触摸状态=挤压;
//获取第二个指针接触时的距离
distx=event.getX(0)-event.getX(1);
disty=event.getY(0)-event.getY(1);
dist0=FloatMath.sqrt(distx*distx+disty*disty);
打破
case MotionEvent.ACTION\u移动:
//在按下手势的过程中(在“向下”和“向上”动作之间)发生了变化。
如果(touchtate==PINCH){
//获取当前距离
distx=event.getX(0)-event.getX(1);
disty=event.getY(0)-event.getY(1);
distCurrent=FloatMath.sqrt(distx*distx+disty*disty);
drawMatrix((图像视图)视图);
} 
打破
case MotionEvent.ACTION\u UP:
//按下的手势已完成。
触摸状态=空闲;
打破
case MotionEvent.ACTION\u指针\u向上:
//非主指针已上升。
触摸状态=触摸;
打破
}
返回此处处理;
}
私有void drawMatrix(图像视图){
浮动光标=distCurrent/dist0;
如果(光标<0.1){
粗刻度=0.1f;
}
view.buildDrawingCache();
位图originalBitmap=view.getDrawingCache();
位图大小调整的位图;
int newHeight=(int)(view.getHeight()*光标);
int newWidth=(int)(view.getWidth()*游标刻度);
resizedBitmap=Bitmap.createScaledBitmap(originalBitmap,newWidth,newHeight,false);
view.setImageBitmap(resizedBitmap);
}
当我放大和缩小几次,这里是如何变成。。。


感谢您的帮助。

实际上,您正在onmove中调用draw函数,因此当您移动手指时,它会继续调用该函数。因此,使用新高度和宽度生成的图像已设置为imageview,该值已设置为默认值。在将调整大小的图像设置为imageview之前,请尝试删除所有视图。希望这能解决您的问题

private void drawMatrix(ImageView view){
        float curScale = distCurrent/dist0;
        if (curScale < 0.1){
            curScale = 0.1f;    
        }

        view.buildDrawingCache();
        Bitmap originalBitmap = view.getDrawingCache();
        Bitmap resizedBitmap;    
        int newHeight = (int) (view.getHeight() * curScale);
        int newWidth = (int) (view.getWidth() * curScale);
        resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false);
        view.removeallviews();
        view.setImageBitmap(resizedBitmap); 

    }
private void drawMatrix(图像视图){
浮动光标=distCurrent/dist0;
如果(光标<0.1){
粗刻度=0.1f;
}
view.buildDrawingCache();
位图originalBitmap=view.getDrawingCache();
位图大小调整的位图;
int newHeight=(int)(view.getHeight()*光标);
int newWidth=(int)(view.getWidth()*游标刻度);
resizedBitmap=Bitmap.createScaledBitmap(originalBitmap,newWidth,newHeight,false);
view.removeallview();
view.setImageBitmap(resizedBitmap);
}

您可以使用TouchImageView类[https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java].
这为图像视图提供了很好的放大和缩小功能。

很抱歉,它不能与图像视图一起使用。我检查它时迟到了。实际上我正在寻找解决方法,很快就会回击。我完成了。让我看看谢谢,我期待着你的解决方案。我尝试过了,问题是我已经让它在特定区域内拖动成为可能。使用TouchImageView,要使其仅在该特定区域内可缩放,我必须将其高度和宽度设置为该区域的高度和宽度,但如果这样做,我将无法将其他图像视图拖动到该区域中。。。我需要一些东西来缩放ImageView本身,而不仅仅是ImageView区域内的图像。你明白吗?