gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:53 25 4
gpt4 key购买 nike

这是我设置的旋转外观。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com