Android:动画圆形进度可绘制

Mat*_*ias 3 animation android drawable android-layout android-drawable

我需要一个看起来像旋转进度圈的 drawable。我想在 GridView 等内的 ImageView 中使用的那个 drawable。

因此,根据其他帖子,我从文件夹 \sdk\platforms\android-xx\data\res\drawable 中取出了progress_medium_holo.xml,看起来像这样。我还复制了这个 drawable 使用的 PNG 文件。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
             android:drawable="@drawable/spinner_48_outer_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="0"
             android:toDegrees="1080" />
    </item>
    <item>
        <rotate
             android:drawable="@drawable/spinner_48_inner_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="720"
             android:toDegrees="0" />
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

在我的布局中,我然后使用了这个 ImageView,它使用了进度可绘制。

<ImageView
    android:id="@+id/element_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:src="@drawable/progress_medium_holo" />
Run Code Online (Sandbox Code Playgroud)

静态进度圈

但结果是一个静态的循环进程。有没有一种方法可以通过 XML 定义并且不使用代码来制作动画?

G.T*_*.T. 5

有一个View命名ProgressBar可以完全满足您的要求。只需使用它而不是您的ImageView

<ProgressBar
    android:id="@+id/element_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" />
Run Code Online (Sandbox Code Playgroud)


Rit*_*une 5

为了创建自定义进度指示器(旋转进度圈,在您的情况下)

  1. 创建布局文件 (.xml) 或使用图像(可绘制)来旋转圆。把它放在 res.drawable 文件夹中。
  2. 将此可绘制对象用作布局中图像视图的源以加载指示器。
  3. 通过膨胀使用此图像视图并在其上调用以下 startLoadingAnimation()。同样,您可以在完成工作后通过简单地调用 stopLoadingAnimation() 来停止动画。

您将不得不使用动画来使其旋转。

// start the loading animation

public void startLoadingAnimation(Context context) {
  Animation rotate = AnimationUtils.loadAnimation(context,
    R.anim.anim_rotate);
  rotate.setRepeatMode(Animation.INFINITE);
  mImgView.startAnimation(rotate);
}

// stop the loading animation
public void stopLoadingAnimation(){
  mImgView.clearAnimation();
}
Run Code Online (Sandbox Code Playgroud)

anim_rotate.xml把这个文件放在anim文件夹中res

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