其他分享
首页 > 其他分享> > android-移动,缩放和旋转ImageView OnTouch不起作用

android-移动,缩放和旋转ImageView OnTouch不起作用

作者:互联网

我有几天试图使这项工作,检查出其他答案,似乎没有一个对我有用:S

我将标签添加到RelativeLayout中,我希望标签一旦被选中就可以使用OnTouch方法移动,缩放和旋转(它具有调用setOnTouchListener的OnClickListener)
请记住,我可以添加N个贴纸.

private void addSticker(ImageView sticker)
{

    flMemeFrame.addView(sticker);

    sticker.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            {
                if (selectedView != null)
                {
                    CancelSelection(selectedView);
                }
                selectedView = v;

                v.setOnTouchListener(new View.OnTouchListener()
                {
                    private float mScaleFactor = 0.5f;
                    private float mRotationDegree = 0.f;
                    private float mFocusX = 0.f;
                    private float mFocusY = 0.f;
                    private int mScreenHeight;
                    private int mScreenWidth;
                    private Matrix matrix = new Matrix();//Các lớp Matrix giữ một ma trận 3x3 để di chuyển tọa độ.
                    private int mImageWidth, mImageHeight;
                    private ScaleGestureDetector mScaleDetector;
                    private RotateGestureDetector mRotateDetector;
                    private MoveGestureDetector mMoveDetector;

                    class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
                        @Override
                        public boolean onScale(ScaleGestureDetector detector) {
                            mScaleFactor *= detector.getScaleFactor();
                            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 1.0f));
                            return true;
                        }
                    }

                    class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
                        @Override
                        public boolean onRotate(RotateGestureDetector detector) {
                            mRotationDegree -= detector.getRotationDegreesDelta();
                            return true;
                        }
                    }

                    class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
                        @Override
                        public boolean onMove(MoveGestureDetector detector) {
                            PointF d = detector.getFocusDelta();
                            mFocusX += d.x;
                            mFocusY += d.y;

                            return true;
                        }
                    }
                    @Override
                    public boolean onTouch(View v, MotionEvent event)
                    {
                        mImageHeight = v.getHeight();
                        mImageWidth = v.getWidth();

                        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
                        mRotateDetector = new RotateGestureDetector(context, new RotateListener());
                        mMoveDetector = new MoveGestureDetector(context, new MoveListener());

                        mScaleDetector.onTouchEvent(event);
                        mRotateDetector.onTouchEvent(event);
                        mMoveDetector.onTouchEvent(event);
                        float scaleImageCenterX = (mImageWidth * mScaleFactor) / 2;
                        float scaleImageCenterY = (mImageHeight * mScaleFactor) / 2;

                        matrix.reset();
                        matrix.postScale(mScaleFactor, mScaleFactor);
                        matrix.postRotate(mRotationDegree, scaleImageCenterX, scaleImageCenterY);
                        matrix.postTranslate(mFocusX - scaleImageCenterX, mFocusY - scaleImageCenterY);

                        ImageView view = (ImageView) v;
                        view.setScaleType(ImageView.ScaleType.MATRIX);
                        view.setImageMatrix(matrix);

                        return true;
                    }
                });
            }
        }
    });
}

这是我设置图像的地方

ImageView sticker = new ImageView(context);
                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
                rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
                sticker.setLayoutParams(rlp);
addSticker(sticker);

该应用程序可以编译且不会崩溃,但是该标签没有显示OnTouch

标签:multi-touch,rotation,android
来源: https://codeday.me/bug/20191118/2029113.html