Android旋转imageview动画问题

标签 android android-animation android-imageview

我正在处理 ImageView 的安卓旋转。当我运行 Project 时,onCreate() 中的动画效果很好,但是当我尝试在单击按钮上启动动画时它不起作用。

我该如何解决?

XML 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/getAngle"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:inputType="number" />

<ImageView
    android:id="@+id/rotateImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/spinner_new" />

<Button
    android:id="@+id/startbutton"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Start" />

Java类代码

public class MainActivity extends Activity {

EditText getAngle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    getAngle = (EditText) findViewById(R.id.getAngle);
    Button startbutton = (Button) findViewById(R.id.startbutton);
    startbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String endPointString = getAngle.getText().toString();
            int endPointInt = Integer.parseInt(endPointString);
            ImageView rotateImage = (ImageView) findViewById(R.id.rotateImage);
            Animation rotateanimation = new RotateAnimation(0, endPointInt,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateanimation.setDuration(1000);
            rotateanimation.setRepeatCount(0);
            rotateanimation.setRepeatMode(Animation.REVERSE);
            rotateanimation.setFillAfter(true);
            rotateImage.setAnimation(rotateanimation);
        }
    });

}

}

最佳答案

按照下面的步骤,一定可以的 1) 给你的 xml relativelayout (parent layout) 提供 Id [我只给出了 relative] 2)执行以下代码:

 public class MainActivity extends Activity {

EditText getAngle;
ImageView rotateImage;
RelativeLayout relative;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


getAngle = (EditText) findViewById(R.id.getAngle);
rotateImage = (ImageView) findViewById(R.id.rotateImage);
  relative = (RelativeLayout)findViewById(R.id.relative);
Button startbutton = (Button) findViewById(R.id.startbutton);
startbutton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        String endPointString = getAngle.getText().toString();
        int endPointInt = Integer.parseInt(endPointString);

        Animation rotateanimation = new RotateAnimation(0, endPointInt,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateanimation.setDuration(1000);
        rotateanimation.setRepeatCount(0);
        rotateanimation.setRepeatMode(Animation.REVERSE);
        rotateanimation.setFillAfter(true);
        rotateImage.setAnimation(rotateanimation);
        rotateanimation.start();
         relative.invalidate();

    }
});

}

}

关于Android旋转imageview动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31962368/

相关文章:

Android Gradle 添加原生库

java - 从屏幕顶部到中心的动画 View

android - 是安卓:animateLayoutChanges supposed to work when a child view's height is updated?

android - 动画在 SurfaceView 中不起作用

java - ImageView不根据屏幕尺寸进行调整

java - 如何使 IntelliJ/Android Studio 中的静态导入更快

java - Android map 注释上的按钮

android - Gradle 3.3 版本构建失败

android - 如何在 ImageView 中缩放自己的可绘制对象

安卓 : Update on UI Thread very fast