android - 使用触摸监听器围绕其中心旋转 android 布局 View

标签 android layout rotation touch ontouchlistener

这是我设置的旋转外观。

enter image description here

我正在监听向下的 Action 并在旋转图标上移动,尝试计算旋转角度并将该旋转角度应用于封装编辑文本的整个布局。应用的旋转必须围绕封装相对布局的中心。

以下是我尝试实现此目的的方法,在用于旋转图标的 touchListener 中。

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;

    }

但是由此产生的旋转是有问题的,并不像预期的那样平滑。另外,如果我将贴纸移动到不同的位置,我该如何更新新的旋转中心,因为移动贴纸后旋转会显着改变旋转中心。我怎样才能实现更平​​滑的旋转?

最佳答案

  1. 要在 1000 毫秒内将 View 的旋转设置为指定值(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. 使用此库,您可以旋转整个 View 层次结构 https://github.com/rongi/rotate-layout

关于android - 使用触摸监听器围绕其中心旋转 android 布局 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584784/

相关文章:

java - Android 进度对话框不显示

并排放置 HTML5/CSS 布局 div

algorithm - 最优柔性盒布局算法

javascript - Three.js:在移动设备上旋转对象

ios - 旋转 UIView 后如何使边缘平滑

android - AsyncTaskLoader.deliverResult() 实际上是做什么的?

android - 无法在 Android 手机中下载文件 - Android..?

android - 正在从数据库中获取数据,但仍然给出 NullPointerException

c# - WPF DataGrid 无法重现 WindowsForms 数据网格大小调整行为

android - 在 Canvas 上垂直绘制文本