安卓:如何为圆形进度条添加动画效果

4
我需要一个看起来像旋转进度圆的可绘制对象。我想在GridView中的ImageView等控件中使用该可绘制对象。
因此,根据其他帖子的说法,我从文件夹\sdk\platforms\android-xx\data\res\drawable中获取了progress_medium_holo.xml,它的样子是这样的。我也复制了被该可绘制对象使用的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>

在我的布局中,我使用了这个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" />

静态进度圆

但结果是一个静态的圆形进度条。有没有一种方法通过XML定义来实现动画效果,而不需要编写代码?

3个回答

6

有一个名为ProgressBarView可以完全满足您的需求。只需使用它来替换您原来的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" />

所以我不需要drawable。当我从错误的一面开始时,这很有趣。 - Matthias
虽然不是一个坏主意,但作为一名开发人员,我遇到过几个设备根本不会显示ProgressBar。它在布局中设置为可见,但并没有关系。我开始寻找替代Android不可靠的ProgressBar... - JamisonMan111

5
为了创建一个定制的进度指示器(在你的情况下是旋转进度圆),请按照以下步骤操作:
  1. 创建一个布局文件(.xml)或使用一个图片(drawable)来制作旋转圆。将它放在res文件夹的drawable文件夹中。
  2. 将此drawable用作加载指示器布局中imageview的源。
  3. 通过充气并调用以下startLoadingAnimation()方法来使用此imageview。同样,只需在其上调用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();
}

anim_rotate.xml文件放置在res文件夹中的anim文件夹中。

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

1
是的,但在那个解决方案中我需要一些代码行。另一个答案中的进度条更适合我的需求。 - Matthias

0
感谢 progress_medium_holo.xml。 在我的情况下,标准的 ProgressBar(适用于 API 23)不可见,我尝试了许多变化,只有水平条样式的进度条可见。然后我复制了 progress_medium_holo.xml,并从文件夹 sdk\platforms\android-xx\data\res\drawable 中链接了可绘制对象,最终得到了:
<ProgressBar
    android:id="@+id/progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:indeterminateDrawable="@drawable/progress_medium_holo"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接