不使用XML动画文件旋转

时间:2013-11-17 00:50:53

标签: android xml

我想在不使用xml文件的情况下旋转图像。

这是我的xml动画代码:

<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="10000"
android:fillAfter="false"
android:startOffset="0"
android:interpolator="@android:anim/linear_interpolator"
/>

2 个答案:

答案 0 :(得分:1)

试试这个:

View needRotateView;// That needRotateView must make sure had measured
    float w = needRotateView.getWidth();
    float h = needRotateView.getHeight();
    int type = Animation.RELATIVE_TO_SELF;//sorry,if not work find,try the follow type value
    // int value_type = Animation.RELATIVE_TO_PARENT;
    // int value_type = Animation.ABSOLUTE;
    RotateAnimation anim = new RotateAnimation(0, 360, type, w / 2, type,h / 2);
    anim.setDuration(1000 * 10);
    anim.setFillAfter(false);
    anim.setStartOffset(0);
    anim.setInterpolator(new LinearInterpolator());

答案 1 :(得分:0)

一种真正简单的方法是使用ProgressBar项并将其设置为使用drawable:

将ProgressBar小部件添加到布局中(我的是在RelativeLayout中):

<ProgressBar
    android:id="@+id/mp4_progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:layout_marginLeft="160dp"
    android:indeterminate="true"
    android:indeterminateDrawable="@drawable/progress_spinner_anim" />

最后一行指向可绘制的XML,如下所示:

// progress_spinner_anim.xml
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="1080" />

该文件指向您的图像文件(我的是drawable / spinner.png)。