在小部件中动画显示 .png 图像

7

问题描述


我正在为Android编写一个简单的小部件应用程序,在我的小部件布局中,我有一个ImageViewRefresh控件,我已经设置了刷新图像图片(下面是绿色图片)。

问题


在某些时候,我会按下我的小部件中的ImageViewRefresh按钮,应用程序开始从互联网下载一些内容,当应用程序在后台下载数据时,我想进行一些动画,比如旋转我的图像(下面是绿色图片)。我能做到吗?

研究


我阅读了一些关于图像动画的帖子,但我只能找到应用程序中.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
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html - zapl
@zapl 我认为这就是我想要的,我稍后会尝试这段代码并让你知道!!! 非常感谢!!! - Viktor Apoyan
@zapl 我必须检查这个是否适用于小部件 :) - Viktor Apoyan
@zapl 但我认为我不能在小部件中使用这个,因为我怎么能得到我的imageView控件?我不能使用RemoteViews来做到这一点。或者我可以吗? - Viktor Apoyan
1
嗯,看起来AnimationDrawable不能与远程视图一起使用。https://dev59.com/ZXE85IYBdhLWcg3w43sW - zapl
2个回答

4

编辑:提前道歉,但我认为我的答案可能会偏离问题:

  • 经测试,系统不会自动旋转可绘制对象,但有些样式可以更改以实现旋转(老实说,我不记得是什么样式了,这是两年前的Eclair),但你可以尝试找一下。
  • 下面的答案是可行的(已经测试过),但不适用于自定义可绘制对象,它们不会旋转。
  • 关于自定义动画对象,请参考这里:ProgressBar/ProgressDialog的自定义可绘制对象
  • 但正如其中一条评论所提到的那样,应用程序小部件不应运行动画效果:有办法在主屏幕小部件上设置动画吗?

原帖:

不要尝试自己对小部件进行动画处理

使用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>

我认为这甚至可能是在小部件中使ImageView动画化的通用解决方案。聪明。 - Paul Lammertsma
@Budius,我认为这是一个好的解决方案。你能提供一些代码吗?这样答案会更加清晰,我可以接受它。非常感谢!!! - Viktor Apoyan
肯定会起作用。我想代码已经全部在那里了,我可以扩展它,但这将是所有样板文件。编辑:这甚至不是代码,只是XML。 - Budius
1
是的,它会。我发布了该代码,知道它是为应用程序小部件而设计的。您可以在Android开发者网站上查看可以在小部件上使用哪些视图:http://developer.android.com/guide/topics/appwidgets/index.html - Budius
1
不要啊啊啊啊啊……系统会自动旋转进度条的,你不需要自己旋转或者添加任何动画。请看我的回答第一行:“不要试图自己为小部件添加动画效果。” - Budius
显示剩余7条评论

-1
在Android中动画图像的最佳方法是使用AnimationDrawable系统。为此,您需要在drawable文件夹之一中拥有类似下面的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>

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

然后像这样导入并启动动画:

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(rotation);

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