旋转imageview卡住了

时间:2013-11-17 23:30:27

标签: android imageview rotateanimation

我想创建一个旋转20度(如时钟)和20度后旋转的简单图像。我有这个简单的布局:

<?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>

和此活动

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);
    }
}

我做错了什么,我的形象没有旋转? 我已经尝试了很多教程而且没有工作:(

2 个答案:

答案 0 :(得分:1)

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

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

如果你想继续旋转二十,你还需要提供第一个参数,即它将设置动画的度数。

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);

答案 1 :(得分: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);