0

I want to create a simple image which rotates 20 degrees (like clock) and 20 degrees back. I have this simple layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/witewall_3">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/pet"
        android:src="@drawable/animal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

and this activity

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView image=(ImageView) findViewById(R.id.pet);

        RotateAnimation anim = new RotateAnimation(0f, 0f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);

        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(700);

        image.startAnimation(anim);
    }
}

what am I doing wrong and my image is not rotating? i've tried a lot of tutorials and nothink worked :(

4

2 回答 2

1

您正在从 0 度旋转到 0 度。第一个参数是起点度数,RotationAnimation 类的第二个参数是终点度数。

RotateAnimation anim = new RotateAnimation(0f, 20f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);

如果你想继续旋转 20 度,你还需要提供第一个参数,它是动画的角度。

RotateAnimation anim = new RotateAnimation(20f, 40f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation anim = new RotateAnimation(40f, 60f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
于 2014-03-26T02:21:27.913 回答
0
set degree from 0 to 360 when you define RatateAnimation()

RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
rotateAnimation.setDuration(5000);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setRepeatMode(Animation.INFINITE);
image.setAnimation(rotateAnimation);
于 2015-06-04T06:49:37.597 回答