0

OnTouchListener を追加した ImageView があるので、2 本の指でピンチ ジェスチャを使用して後者をズームインおよびズームアウトできます。以下のコードを使用していますが、実際のビットマップのサイズ変更に問題があります。後者はぼやけ、同じサイズのままで、画像のサイズ変更されたバージョンでオーバーレイされます。これは、新しい画像を古い画像の上に置き換えるのではなく、単に重ねているかのようです。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 で 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); 

    }
于 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 に答える