[英]Android animation when rotate
我有一个ImageButton
,它会在手机旋转时旋转。 它按照设备方向旋转。 就像是:
int rotation = this.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int relative_orientation = (current_orientation + degrees) % 360;
int ui_rotation = (360 - relative_orientation) % 360;
preview.setUIRotation(ui_rotation);
然后在ImageButton
我这样做:
view = findViewById(R.id.button1);
layoutParams = (RelativeLayout.LayoutParams)view.getLayoutParams();
view.setLayoutParams(layoutParams);
view.setRotation(ui_rotation);
它可以工作,但是没有动画。.因此旋转非常剧烈且不平滑。.是否可以放置动画?
编辑:我添加setUIRotation()方法
void setUIRotation(int ui_rotation) {
if( MyDebug.LOG )
Log.d(TAG, "setUIRotation");
this.ui_rotation = ui_rotation;
}
您可以使用类似这样的方法代替view.setRotation()
:
RotateAnimation rotate = new RotateAnimation(0f, ui_rotation,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);
rotate.setFillAfter(true);
view.startAnimation(rotate);
对于边缘情况(例如,当触发另一个旋转时动画尚未结束),可以使用setAnimationListener
获得更好的效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.