带有 MultiTouch 的 Android 图库适配器?自定义图库还是自定义 ImageView?

Posted

技术标签:

【中文标题】带有 MultiTouch 的 Android 图库适配器?自定义图库还是自定义 ImageView?【英文标题】:Android Gallery Adapter with MultiTouch? Custom Gallery or Custom ImageView? 【发布时间】:2011-03-18 15:21:11 【问题描述】:

我想重新创建与 Gallery3D 相同类型的应用程序,但更简单:没有 3D 动画,基本上只保留 GridView 和 Gallery 小部件。我还想在所选图像上启用多点触控缩放-拖动-缩放,这就是我苦苦挣扎的地方。我在网上找了简单的教程,也看了Gallery3D的源代码,但找不到合适的解决方案。

我能得到的最接近的是在我的图库适配器中使用下面的自定义 ImageView。

它有效,我可以捏合缩放和拖动,只是图像离开屏幕并且我无法滚动到图库中的下一张图像。捏和缩放也不是完美的,因为它只会缩放图像并且不能正确地重新定位它。我应该在 Gallery 上添加一个 onTouchEvent 吗?


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class MyImageView extends View 

private static final int INVALID_POINTER_ID = -1;

    private BitmapDrawable mImage;
    private float mPosX;
    private float mPosY;

    private float mLastTouchX;
    private float mLastTouchY;
    private int mActivePointerId = INVALID_POINTER_ID;

    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;

    public MyImageView(Context context, BitmapDrawable bd) 
        this(context, null, 0);
     mImage = bd;
        mImage.setBounds(0, 0, mImage.getIntrinsicWidth(), mImage.getIntrinsicHeight());
    

    public MyImageView(Context context, AttributeSet attrs) 
        this(context, attrs, 0);
    

    public MyImageView(Context context, AttributeSet attrs, int defStyle) 
        super(context, attrs, defStyle);
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    

    @Override
    public boolean onTouchEvent(MotionEvent ev) 
        // Let the ScaleGestureDetector inspect all events.
        mScaleDetector.onTouchEvent(ev);

        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) 
        case MotionEvent.ACTION_DOWN: 
            final float x = ev.getX();
            final float y = ev.getY();

            mLastTouchX = x;
            mLastTouchY = y;
            mActivePointerId = ev.getPointerId(0);
            break;
        

        case MotionEvent.ACTION_MOVE: 
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) 
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                invalidate();
            

            mLastTouchX = x;
            mLastTouchY = y;

            break;
        

        case MotionEvent.ACTION_UP: 
            mActivePointerId = INVALID_POINTER_ID;
            break;
        

        case MotionEvent.ACTION_CANCEL: 
            mActivePointerId = INVALID_POINTER_ID;
            break;
        

        case MotionEvent.ACTION_POINTER_UP: 
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) 
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            
            break;
        
        

        return true;
    

    @Override
    public void onDraw(Canvas canvas) 
        super.onDraw(canvas);

        canvas.save();
        Log.d("DEBUG", "X: "+mPosX+" Y: "+mPosY);
        canvas.translate(mPosX, mPosY);
        canvas.scale(mScaleFactor, mScaleFactor);
        mImage.draw(canvas);
        canvas.restore();
    

    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, 10.0f));

            invalidate();
            return true;
        
    



谢谢

【问题讨论】:

【参考方案1】:

你应该看看这两个教程,我发现它们很有帮助:

One Finger Zooming and Panning Pinch Zoom Gesture

顺便说一句,他们都没有使用ScaleGestureDetector,但是实现的效果非常整洁。

【讨论】:

以上是关于带有 MultiTouch 的 Android 图库适配器?自定义图库还是自定义 ImageView?的主要内容,如果未能解决你的问题,请参考以下文章

不同uiview中的iOS MultiTouch事件

MultiTouch————多点触控,伸缩图片,变换图片位置

带有viewpager2 android的导航图

深度图 - 带有 OpenCV 的 Android 中的立体图像

缺少带有 ID 的必需视图,导航图 android

Android触摸事件-TouchEventHelper