0

我有一个 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); 

    }

当我放大和缩小几次时,它会变成这样...... 在此处输入图像描述

感谢我能得到的任何帮助。

4

2 回答 2

1

实际上,您正在 onmove 中调用绘图函数,因此当您移动手指时它会继续调用它。因此,使用新高度和宽度生成的图像已设置为已设置的 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); 

    }
于 2012-10-30T12:08:34.727 回答
0

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

于 2012-10-30T13:12:14.477 回答