Animate .png image in widgets

Description of the problem


I am writing a simple widget application for Android. In my Widget layout, I have an ImageViewRefresh control on which I set Refresh Image Picture (Green image below).

Question


At some point, I click the ImageViewRefresh button in my widget , and the application starts downloading some content from the Internet, and when loading applications in the background I want to do some animation, for example, rotate my image (Green image below). Can I do it?

Research


I read a few posts about image animation, but I can only find .gif image animations in the application where you can rotate the image, for example, make some rotated images and change them or something else.

Code example

Here is a piece of code from my layout my image does not rotate. What for? (my image is a simple .png image)

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

The image I want to rotate.

Rotate image

+6
source share
2 answers

edit: I will apologize in advance, but I believe that my answer may be misleading to the question:

  • as verified, the system does not automatically rotate popped, but there are styles that you can change to do this (I honestly donโ€™t remember, it was 2 years ago in Eclair), but you can try to find it.
  • the answer below works (as it was tested), but NOT for custom drawings it will not rotate them.
  • for custom animation can be found here: Custom Drawable for ProgressBar / ProgressDialog
  • but, as one of the comments mentioned, application widgets should not run animations. Is there a way to animate on a home widget?

original post:

Do not try to animate the widget yourself

Use the ProgressBar parameter so that it is undefined and use setIndeterminateDrawable(Drawable d); to set the image you want to rotate. (or just leave your own, which also looks very good)

change this is what the code looks like:

 // 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); 

this is what XML would look like for something like this:

 <?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> 
+4
source

The best way to animate images in Android is to use the AnimationDrawable system. To do this, you need an xml similar to the one below in one of your drop-down folders.

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

If image1, image2 and image3 are different resources in your resources, each of which represents a different state of your image.

To create images, you can simply open your image using Gimp or Photoshop and rotate it several degrees and export to a new image and repeat.

Alternatively, you can use the following code to rotate the ImageView. First create the โ€œanimโ€ folder in the res folder and add the rotate.xml file with the following contents:

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

then import and run the animation as follows:

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

All Articles