使用触摸侦听器旋转关于其中心的android布局视图

Sag*_*wan 5 layout android rotation touch ontouchlistener

以下是我设置旋转外观的方法.

在此输入图像描述

我正在倾听动作并继续旋转图标,尝试计算旋转角度并将该旋转角度应用于封装edittext的整个布局.应用的旋转必须大约是封装相对布局的中心.

以下是我在touchListener for rotation图标中尝试实现此目的的方法.

if(v == ivRotateSticker){


        //TODO grab xc and yc using (top and bottom)/2 and (left and right)/2 to grab center of sticker after each rotation
        final float x = event.getX();
        final float y = event.getY();
        rlTextStickerLayout.getGlobalVisibleRect(myViewRect);
        xc = myViewRect.width()/2;
        yc = myViewRect.height()/2;

        dx = scaleReceiver.getWidth() - myViewRect.width();
        dy = scaleReceiver.getHeight() - myViewRect.height();

        leftBound = scaleReceiver.getX();
        topBound = scaleReceiver.getY();

        switch (event.getAction()){

            case MotionEvent.ACTION_DOWN:
                mViewAngle = rlTextStickerLayout.getRotation();
                mPrevAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));

                break;

            case MotionEvent.ACTION_MOVE:

                Log.d("EventRegistered", " " + event.getRawX() + "--" + dx + " -- " + dy);

                mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
                if (x >= leftBound && y >= topBound && x <= dx && y <= dy) {
                    float rot = (float) (mViewAngle + mCurrAngle - mPrevAngle);
                    rlTextStickerLayout.setRotation((float) (mViewAngle + mCurrAngle - mPrevAngle));
                    Log.d("stickerRotation"," "+rot);
                }
                break;

        }

        return true;

    }
Run Code Online (Sandbox Code Playgroud)

但是由此产生的旋转是微不足道的,并不像所希望的那样平滑.此外,如果我将贴纸移动到其他位置,如何更新新的旋转中心,因为移动贴纸后的旋转会显着改变旋转中心.如何实现更顺畅的旋转?

Mis*_*agh 0

  1. 视图的旋转在 1000 毫秒内动画到指定值(90、180、270):

    view.animate().rotation(90f).setDuration(1000).start(); view.animate().rotation(180f).setDuration(1000).start(); view.animate().rotation(270f).setDuration(1000).start();

  2. 使用此库,您可以旋转整个视图层次结构https://github.com/rongi/rotate-layout