【问题标题】:Cannot pinch zoom if both fingers touch screen simultaneously如果两个手指同时触摸屏幕,则无法捏缩放
【发布时间】:2013-05-25 04:01:21
【问题描述】:

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

我按照 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;
        }
    }   
}

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

【问题讨论】:

    标签: java android gesture pinchzoom


    【解决方案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;
        }
    }
    }
    

    【讨论】:

    • 是的,其他应用可以正常使用缩放。我正在使用摩托罗拉 droid HD ......我想我会在你提到它的另一部手机上测试它。另外,您对代码做了哪些更改?乍一看我没注意到
    • 我已将private ScaleGestureDetector scaleDetector; public CanvasView(Context context) { 更改为private ScaleGestureDetector mScaleDetector; public ZoomView(Context context) {
    • 嘿路易莎。我能够在另一部手机上使用它,所以我将在我的机器人上进行出厂重置,看看是否能解决这个问题(无论如何它还有其他问题)。
    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    相关资源
    最近更新 更多