问题描述
我正在为 Android 编写一个简单的 Widget 应用程序,在我的 Widget I Layout 中,我有 ImageViewRefresh
控件,我在上面设置了 Refresh Image图片(下面的绿色图片)。
问题
在某些时候,我按下 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" />
我要旋转的图像。
最佳答案
编辑:我会提前道歉,但我相信我的回答可能误导了问题:
- 经测试,系统不会自动旋转可绘制对象,但您可以更改样式来执行此操作(老实说,我不记得了,这是 2 年前在 Eclair 上的),但您可以尝试找到它。
- 下面的答案有效(经过测试)但不适用于自定义可绘制对象,它不会旋转它们。
- 有关自定义动画可绘制对象的信息,请参阅此处:Custom Drawable for ProgressBar/ProgressDialog
- 但正如其中一条评论所述,应用程序小部件不应该运行动画 Is there a way to animate on a Home Widget?
原帖:
不要尝试自己为小部件设置动画
使用 ProgressBar
将其设置为不确定并使用 setIndeterminateDrawable(Drawable d);
设置您想要旋转的图像。 (或者只留下看起来也很漂亮的原生的)
编辑: 代码如下所示:
// in your widget update method:
View v = LayoutInflater.from(context).inflate(R.layout.widget, null);
ProgressBar pb = (ProgressBar) v.findViewById(R.id.progressBar1);
pb.setIndeterminateDrawable(R.drawable.widget_processing);
下面是类似这样的东西的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/textView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:indeterminate="true" />
</RelativeLayout>
关于android - 小部件中的动画 .png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951252/