android图像缩放使用多点触控

时间:2015-07-30 11:22:16

标签: android zoom scale gesture multi-touch

我正在尝试创建一个应用,用户可以使用多点触摸手势触摸图像和缩放。我在网上搜索,发现很少有解决办法。我甚至实施了这些,但我所有的努力都是徒劳的。首先我的代码无法检测到多点触控,我意识到我们还需要在switch case中添加MotionEvent.ACTION_MASK。然后,当我尝试缩放使用多点触摸时,没有任何效果发生。请指正。

TIA。

    OnTouchListener onTouchListener = new OnTouchListener() {

    int prevX,prevY;
    float scale;
    float mCurrentScale = 1.0f;
    @Override
    public boolean onTouch(View v, MotionEvent event) {


        //view.setScaleType(ImageView.ScaleType.MATRIX);
        scaleGestureDetector.onTouchEvent(event);
        ImageView view = (ImageView) v;
        v.bringToFront();
        final RelativeLayout.LayoutParams par=(RelativeLayout.LayoutParams)v.getLayoutParams();
        switch(event.getAction() & MotionEvent.ACTION_MASK)
        {
        case MotionEvent.ACTION_UP:
            par.topMargin+=(int)event.getRawY()-prevY;
            par.leftMargin+=(int)event.getRawX()-prevX;
            v.setLayoutParams(par);
            break;
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            lastEvent = null;
            break;
        case MotionEvent.ACTION_DOWN:

            prevX=(int)event.getRawX();
            prevY=(int)event.getRawY();
            par.bottomMargin=-2*view.getHeight();
            par.rightMargin=-2*view.getWidth();
            v.setLayoutParams(par);
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            Log.d("hi","outside if DRAG");
            mode = DRAG;
            lastEvent = null;

        break;
        case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);
            Log.d("hi","Checking");
            if (oldDist > 10f) {
                savedMatrix.set(matrix);
                midPoint(mid, event);
                mode = ZOOM;
            }
            lastEvent = new float[4];
            lastEvent[0] = event.getX(0);
            lastEvent[1] = event.getX(1);
            lastEvent[2] = event.getY(0);
            lastEvent[3] = event.getY(1);
            d = rotation(event);
            break;
        case MotionEvent.ACTION_MOVE:


            if(mode == DRAG){
                par.topMargin+=(int)event.getRawY()-prevY;
                prevY=(int)event.getRawY();
                par.leftMargin+=(int)event.getRawX()-prevX;
                prevX=(int)event.getRawX();
                v.setLayoutParams(par);
            }
             if (mode == ZOOM) {
                float newDist = spacing(event);
                if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = (newDist / oldDist);

                    matrix.postScale(scale, scale, mid.x, mid.y);
                    Log.e("hi", "TEsting");
                }
                if (lastEvent != null && event.getPointerCount() == 3) {
                    newRot = rotation(event);
                    float r = newRot - d;
                    float[] values = new float[9];
                    matrix.getValues(values);
                    float tx = values[2];
                    float ty = values[5];
                    float sx = values[0];
                    float xc = (v.getWidth() / 2) * sx;
                    float yc = (v.getHeight() / 2) * sx;
                    matrix.postRotate(r, tx + xc, ty + yc);
                }
            }
            break;

        }
        //ImageView view = (ImageView) v;
        view.setImageMatrix(matrix);

        v.invalidate();
        return true;

    }
};

1 个答案:

答案 0 :(得分:0)

您可能需要使用这段代码:

import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  

public class MultiTouchActivity extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //setContentView(R.layout.activity_main);  

        TouchImageView img = new TouchImageView(this);  
        img.setImageResource(R.drawable.ice_age_2);  
        img.setMaxZoom(4f);  
        setContentView(img);  
    }  
}   

供参考使用链接 here