7

Problem Description


I am writing a simple Widget application for Android, In my Widget I Layout I have ImageViewRefresh control on which I have set Refresh Image Picture (Green Image Below).

Question


At some point I press on a ImageViewRefresh button in my Widget and application start to download some content from the Internet, while application downloading data in a background I want to make some animation, like rotate my image (Green Image Below). Can I do that ?

Researches


I have read some posts about Image animation, but I can find only animation of .gif pictures in application, is where a way to rotate image for example make some rotated images and change them or something else.

Code Example

Here is a part of code from my layout my image is not rotating. Why ? (my image is 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" />

Image which I want to rotate.

Rotate Image

Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • 2
    http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html – zapl Dec 19 '12 at 11:23
  • @zapl I think this is what I want, I will try this code later and let you know !!! Thanks a lot !!! – Viktor Apoyan Dec 19 '12 at 11:25
  • @zapl I must check is this work on a Widget or not :) – Viktor Apoyan Dec 19 '12 at 11:25
  • @zapl but I think that I can't use this in Widget as How I can Get there my imageView control ? I can't do that using RemoteViews. Or I can ? – Viktor Apoyan Dec 19 '12 at 11:29
  • 1
    hmm, it seems that `AnimationDrawable` does not work with remote views http://stackoverflow.com/questions/2565023/is-there-a-way-to-animate-on-a-home-widget – zapl Dec 19 '12 at 11:34

2 Answers2

4

edit: I'll apologise in advance but I believe my answer might have mislead from the question:

  • as tested, the system does not automatic rotates the drawable, but there are styles you can change to do it (I honestly don't remember, it's been 2 years ago on Eclair), but you can try to find it.
  • the answer below works (as it was tested) but NOT for custom drawables it won't rotate them.
  • for a custom animation drawable refer to here: Custom Drawable for ProgressBar/ProgressDialog
  • but as mentioned by one of the comments, app widgets are not supposed to run animations Is there a way to animate on a Home Widget?

original post:

Do not try to animate the widget yourself

Use a ProgressBar set it to be indeterminate and use setIndeterminateDrawable(Drawable d); to set the image you want to be rotating. (or just leave the native one that looks very nice too)

edit: here's what the code would look 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);

here's what the XML for something like this would look like:

<?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>
Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144
  • I think this might even be a generic solution for getting ImageViews to animate in a widget. Clever. – Paul Lammertsma Dec 19 '12 at 11:42
  • @Budius I think this is good solution. Can you provide some part of code. In order answer be quiet clean and I can accept it. Thanks a lot !!! – Viktor Apoyan Dec 19 '12 at 11:43
  • sure it will work. And I reckon the code is all in there already, I can expand but it will be all boiler plate stuff. Edit: it's not even code, it's just the XML – Budius Dec 19 '12 at 11:46
  • 1
    yes it will. I posted the code knowing it's for an App Widget. On the Android Developers site you can check which views you can use on a Widget http://developer.android.com/guide/topics/appwidgets/index.html – Budius Dec 19 '12 at 11:53
  • @Budius last question as I never work with progress bar in this case ... My question is and How I must change images ? – Viktor Apoyan Dec 19 '12 at 11:54
  • I don't understand your question. The code is there on my answer, just go and try for yourself. – Budius Dec 19 '12 at 11:55
  • @Budius I mean if I have 10 .png images I must do something like this `View v = LayoutInflater.from(context).inflate(R.layout.widget, null); ProgressBar pb = (ProgressBar) v.findViewById(R.id.progressBar1); pb.setIndeterminateDrawable(R.drawable.widget_processing1); pb.setIndeterminateDrawable(R.drawable.widget_processing2); pb.setIndeterminateDrawable(R.drawable.widget_processing3); pb.setIndeterminateDrawable(R.drawable.widget_processing4);` – Viktor Apoyan Dec 19 '12 at 11:58
  • 1
    NNOOOOOOOoooooo........ the system automatically rotates the progress bar indeterminate for you. You don't have to rotate or animate or anything. Read the 1st line of my answer: `Do not try to animate the widget yourself.` – Budius Dec 19 '12 at 11:59
  • @Budius OOOOOOOOOooo :) Thanks a lot and sorry for stupid question, I will try and you know – Viktor Apoyan Dec 19 '12 at 12:01
  • @Budius this method doesn't work ! Must I set gif image ? as when I set png image nothing happens ? it not rotate ! – Viktor Apoyan Dec 19 '12 at 17:17
  • try not setting anything at all. Nothing. Just let the system use the default. Also make sure that you have `android:indeterminate="true"` on your XML – Budius Dec 19 '12 at 17:20
  • @Budius, yes it work then I not set any image ? by default it rotate its standard image, but not mine – Viktor Apoyan Dec 19 '12 at 17:24
-1

The best way to animate an image in Android is by using the AnimationDrawable system. To do this, you need an xml similar to the one below in one of your drawable folder.

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

Where image1, image2 and image3 are different drawables in your resources, each representing a different state of your image.

To create the images, you can simply open your image with Gimp or Photoshop, and Rotate it a few degrees and export it in a new image, and repeat.

Alternatively, you can use the following code to rotate the ImageView. First create a "anim" folder under your res folder, and add a rotate.xml file with the following content :

<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 start the animation like this :

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(rotation);
XGouchet
  • 10,002
  • 10
  • 48
  • 83