在窗口小部件中动画.png图像

Vik*_*yan 7 android android-widget android-animation android-imageview

问题描述


我正在为Android 编写一个简单的Widget应用程序,在我的Widget I Layout中,我可以ImageViewRefresh控制我已经设置了Refresh Image Picture(下面的绿色图像).


在某些时候,我按下ImageViewRefresh 我的Widget中按钮,应用程序开始从Internet下载一些内容,而应用程序在后台下载数据我想做一些动画,比如旋转我的图像(下面的绿色图像).我能这样做吗?

研究


我已经阅读了一些关于图像动画的帖子,但我只能在应用程序中找到.gif图片的动画,这是一种旋转图像的方法,例如制作一些旋转的图像并更改它们或其他东西.

代码示例

这是layout我的图像代码的一部分不旋转.为什么?(我的形象很简单.png图片)

<ProgressBar
        android:id="@+id/progressBarRefresh"
        android:layout_width="36dp"
        android:indeterminateDrawable="@drawable/arrow_refresh"
        android:layout_height="36dp"
        android:layout_alignTop="@+id/imageViewArrowNext"
        android:layout_marginRight="70dp"
        android:layout_toLeftOf="@+id/textViewAutherName"
        android:indeterminate="true" />
Run Code Online (Sandbox Code Playgroud)

我要旋转的图像.

旋转图像

XGo*_*het -1

在 Android 中为图像添加动画效果的最佳方法是使用 AnimationDrawable 系统。为此,您需要在可绘制文件夹之一中添加类似于以下内容的 xml。

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/image1" android:duration="200" />
    <item android:drawable="@drawable/image2" android:duration="200" />
    <item android:drawable="@drawable/image3" android:duration="200" />
</animation-list>
Run Code Online (Sandbox Code Playgroud)

其中 image1、image2 和 image3 是资源中的不同可绘制对象,每个都代表图像的不同状态。

要创建图像,您只需使用 Gimp 或 Photoshop 打开图像,然后将其旋转几度并将其导出到新图像中,然后重复。

或者,您可以使用以下代码来旋转 ImageView。首先在你的res文件夹下创建一个“anim”文件夹,并添加一个包含以下内容的rotate.xml文件:

<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="1000"
    android:startOffset="0"
/>
Run Code Online (Sandbox Code Playgroud)

然后导入并启动动画,如下所示:

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(rotation);
Run Code Online (Sandbox Code Playgroud)