这是我设置的旋转外观。
我正在监听向下的 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;
}
但是由此产生的旋转是有问题的,并不像预期的那样平滑。另外,如果我将贴纸移动到不同的位置,我该如何更新新的旋转中心,因为移动贴纸后旋转会显着改变旋转中心。我怎样才能实现更平滑的旋转?
最佳答案
要在 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();
使用此库,您可以旋转整个 View 层次结构 https://github.com/rongi/rotate-layout
关于android - 使用触摸监听器围绕其中心旋转 android 布局 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584784/