【问题标题】:Android: animate circular progress drawableAndroid:动画循环进度drawable
【发布时间】:2014-06-18 08:34:14
【问题描述】:

我需要一个看起来像旋转进度圈的drawable。例如,我想在 GridView 等内的 ImageView 中使用该可绘制对象。

因此,根据其他帖子,我从文件夹 \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>

然后,在我的布局中,我使用了这个 ImageView,它使用了 progress-drawable。

<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 定义而不使用代码来制作动画?

【问题讨论】:

    标签: android android-layout animation drawable android-drawable


    【解决方案1】:

    有一个名为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。当我从错误的角度开始时,这很有趣。
    • 虽然不是一个坏主意,但作为开发人员,我遇到过几种设备,它们根本不会显示 ProgressBar。它在布局中,设置为可见,但没关系。我开始寻找 Android 不可靠的 ProgressBar 的替代品...
    【解决方案2】:

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

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

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

    【讨论】:

    • 是的,但在那个解决方案中,我需要一些代码行。另一个答案的 ProgressBar 更适合我的需要。
    【解决方案3】:

    感谢progress_medium_holo.xml。 在我的情况下,一个标准的 ProgressBar(对于 API 23)是不可见的,我尝试了很多变体,只有水平条样式的进度条是可见的。然后我从文件夹 sdk\platforms\android-xx\data\res\drawable 复制了 progress_medium_holo.xml 和链接的drawables,最终得到:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多