旋转动画android

ore*_*ion 21 animation android

我正在尝试做一个旋转图像动画.我需要像在进度条中那样围绕自己旋转图标,但我得到的是围绕圆圈旋转的图像.这是我的动画代码

<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2500"
android:repeatCount="infinite"
android:repeatMode="restart"
/>
Run Code Online (Sandbox Code Playgroud)

我在哪里错了?谢谢

K_A*_*nas 68

这是布局main.xml的源代码:

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

    <Button
        android:id="@+id/testButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="animationText" 
        android:onClick="AnimClick"/>

    <ImageView
        android:id="@+id/testImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:contentDescription="image_desc"
        android:scaleType="fitCenter"
        android:src="@drawable/cat2" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

要实现旋转动画,我们可以通过XML或Java代码定义动画.如果我们想在xml中编写动画,我们需要在/ res/anim文件夹下创建一个动画xml文件.在这里,我们创建一个名为的xml文件rotate_around_center_point.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <rotate
        android:duration="2500"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>
Run Code Online (Sandbox Code Playgroud)

这是我的活动:

public class MainActivity extends Activity implements OnClickListener {

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

        Button btn = (Button) findViewById(R.id.testButton);
        btn.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point);
        animationTarget.startAnimation(animation);

    }


}
Run Code Online (Sandbox Code Playgroud)


Jes*_*rix 32

您可以尝试使用以下代码,而不是使用XML:

RotateAnimation rotate = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
        0.5f);

rotate.setDuration(4000);
rotate.setRepeatCount(Animation.INFINITE);
yourView.setAnimation(rotate);
Run Code Online (Sandbox Code Playgroud)

  • 尝试添加rotate.setInterpolator(new LinearInterpolator()); (7认同)

ore*_*ion 5

好吧,我到了错误的地方.我给了我的图像填充,导致它每次旋转时移动一点.无论如何我删除了填充,现在它正常工作.