简体   繁体   English

Android旋转imageview动画问题

[英]Android rotate imageview animation issue

I am working android rotation of ImageView. 我正在使用ImageView的android旋转。 When I run Project, animation in onCreate() its work fine, but when I try to start Animation on Click button its not working. 当我运行Project时,onCreate()中的动画可以正常工作,但是当我尝试在“单击”按钮上启动动画时,它无法正常工作。

How can I fix it? 我该如何解决?

XML Code 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 Class Code 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);
        }
    });

}

}

Do the following steps and it will work definitely 1) give Id to your xml relativelayout (parent layout) [i have given just relative] 2) Do the below code : 执行以下步骤,它肯定可以正常工作:1)为您的xml relativelayout(父级布局)提供ID [我已经给出了相对关系] 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();

    }
});

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM