如果两个手指同时触摸屏幕,则无法捏缩放

时间:2013-05-29 18:45:14

标签: java android gesture pinchzoom

我已经在我的自定义视图上实现了缩放,该视图将网格绘制到画布上,但如果我同时将两个手指放在屏幕上,则缩放将不起作用。我必须首先放一个,然后另一个。

我按照Adam Powell的this博客文章来实现缩放,并制作了这个自定义视图:

public class ZoomView extends View{

    private float width;    
    private float height;   

    Paint lineColor; 
    Paint bgColor;

    private float mScaleFactor;
    private ScaleGestureDetector scaleDetector;

    public CanvasView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        lineColor = new Paint();
        lineColor.setColor(getResources().getColor(R.color.white));
        bgColor = new Paint();
        bgColor.setColor(getResources().getColor(R.color.black));

        mScaleFactor = 1;
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh){
        width = w/9;
        height = h/9;
        super.onSizeChanged(w,h,oldw,oldh);
    }

    @Override 
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        canvas.save();
        canvas.scale(mScaleFactor, mScaleFactor);     //for zooming

        for(int i=0; i <= 9; i++){
            canvas.drawLine(0,i*height, 9*width,i*height,lineColor);
            canvas.drawLine(i*width, 0, i*width, 9*height, lineColor);
        }

        canvas.restore();
    }

    @Override 
    public boolean onTouchEvent(MotionEvent ev){

        mScaleDetector.onTouchEvent(ev);
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        setMeasuredDimension(800, 1000);
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

            invalidate();
            return true;
        }
    }   
}

那么缩放为什么会这样呢? 任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

这可能看起来像一个愚蠢的问题,但多点触控行为是否适用于此设备上的其他应用程序(例如地图)? 我已经在LG Optimus Black,Android 4.0.3上测试了你的代码(稍作修改,见下文),它同时起作用了。

package com.example.teste;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class ZoomView extends View {

private float width;
private float height;

Paint lineColor;
Paint bgColor;

private float mScaleFactor;
private ScaleGestureDetector mScaleDetector;

public ZoomView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);

    lineColor = new Paint();
    lineColor.setColor(getResources().getColor(R.color.ics_blue_bright));
    bgColor = new Paint();
    bgColor.setColor(getResources().getColor(R.color.black));

    mScaleFactor = 1;
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = w / 9;
    height = h / 9;
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.scale(mScaleFactor, mScaleFactor); // for zooming

    for (int i = 0; i <= 9; i++) {
        canvas.drawLine(0, i * height, 9 * width, i * height, lineColor);
        canvas.drawLine(i * width, 0, i * width, 9 * height, lineColor);
    }

    canvas.restore();
}

@Override
public boolean onTouchEvent(MotionEvent ev) {

    mScaleDetector.onTouchEvent(ev);
    return true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(800, 1000);
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}
}