Android 在两个方向上旋转imageview无法顺利正常工作
我需要这种平滑的旋转动画。为此,我使用了Android 在两个方向上旋转imageview无法顺利正常工作,android,image-rotation,Android,Image Rotation,我需要这种平滑的旋转动画。为此,我使用了RotateAnimation。我需要它在顺时针方向和逆时针方向,但我的代码并没有给出我期望的gif图像输出 这是我的代码 public class MainActivity extends AppCompatActivity { ImageView imgView; private double mCurrAngle = 0; private double mPrevAngle = 0; @Override protected void onCrea
RotateAnimation
。我需要它在顺时针方向和逆时针方向,但我的代码并没有给出我期望的gif图像输出
这是我的代码
public class MainActivity extends AppCompatActivity {
ImageView imgView;
private double mCurrAngle = 0;
private double mPrevAngle = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = findViewById(R.id.imgView);
imgView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final float xc = imgView.getWidth() / 2;
final float yc = imgView.getHeight() / 2;
final float x = event.getX();
final float y = event.getY();
double angrad = Math.atan2(x - xc, yc - y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
imgView.clearAnimation();
mCurrAngle = Math.toDegrees(angrad);
animate(mPrevAngle, mCurrAngle, 2000);
break;
}
case MotionEvent.ACTION_MOVE: {
mPrevAngle = mCurrAngle;
mCurrAngle = Math.toDegrees(angrad);
animate(mPrevAngle, mCurrAngle, 2000);
break;
}
case MotionEvent.ACTION_UP: {
mPrevAngle = mCurrAngle = 0;
break;
}
}
return false;
}
});
}
private void animate(double fromDegrees, double toDegrees, long durationMillis) {
final RotateAnimation rotate = new RotateAnimation((float) fromDegrees, (float) toDegrees,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(durationMillis);
rotate.setInterpolator(new LinearInterpolator());
rotate.setFillEnabled(true);
rotate.setFillAfter(true);
imgView.startAnimation(rotate);
}
有人请回顾一下这个问题吗?谢谢你的帮助
public void rotate(View view){
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
image.startAnimation(animation);
}
代码rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="5000"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
</set>
请检查代码,我认为它将帮助您……此代码自动双向旋转。我也需要它在onTouch方法,因为你们可以看到n Gif图像。我需要相同的输出。请检查这个网站,它将帮助你,我在我的代码,但没有按照我的愿望输出工作