平滑旋转(变换,而不是动画)RelativeLayout

时间:2018-07-27 07:07:01

标签: java android android-layout

我尝试在拖动时围绕其中心旋转相对布局。布局旋转,但旋转不平滑,旋转时会出现抖动。 这是我的代码-

layout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            final float xc = layout.getWidth() / 2;
            final float yc = layout.getHeight() / 2;

            final float x = event.getX();
            final float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
                    break;
                }
                case MotionEvent.ACTION_MOVE: {
                    mPrevAngle = mCurrAngle;
                    mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));

                    // I tried rotating an ImageView using a matrix, with the following code and it is working smoothly
                    // matrix.postRotate((float) (mCurrAngle-mPrevAngle), xc, yc);
                    // imageView.setImageMatrix(matrix);

                    layout.setRotation(layout.getRotation() + (float) (mCurrAngle-mPrevAngle));
                    break;
                }
                case MotionEvent.ACTION_UP : {
                    mPrevAngle = mCurrAngle = 0.0;
                    break;
                }
            }

            return true;
        }
    });

我使用上面相同的代码使用矩阵旋转图像,并平滑旋转。但是,在图像上应用setRotation()会导致其与布局相同的跳动。 我在做什么错了?

0 个答案:

没有答案