1

I have an app that lets the user select a radio-button from a list. When the radio-button is selected, a custom dialog appears looking like this: enter image description here

I want to display a animation that shows a hamster running while the actual image is downloaded using an aynctask class. After the requested image is finished downloading, it should be displayed on the screen(replacing the hamster animation).

So, my question is: how can can I achieve this? I tried using the gif frame loader, but that doesn't work in the dialog, however, I can display a rotating image, but I want the loading bar to be a little more fun, so I chose the running hamster. The above methods are from here

Also, the user should be able to click the close button, even though the image hasn't loaded(in case the image url is broken).

Edit: This is the example that doesn't work:

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActiviy extends Activity implements OnClickListener{
Button start, stop;
ImageView imageView;   
AnimationDrawable frameAnimation;   

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  

    imageView = (ImageView)findViewById(R.id.blankImageView);

    start = (Button)findViewById(R.id.start);
    stop = (Button)findViewById(R.id.stop);

    imageView.setBackgroundResource(R.drawable.spin_animation);   
    frameAnimation = (AnimationDrawable) imageView.getBackground();  

    start.setOnClickListener(this);
    stop.setOnClickListener(this);

    //does not start the animation
    frameAnimation.start();
}

public void onClick(View v) {
    switch(v.getId()) {
    case R.id.start:
        frameAnimation.start();
        break;
    case R.id.stop:
        frameAnimation.stop();   
        break;
    }
}
}

//the main.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<ImageView android:layout_width="wrap_content"  
    android:id="@+id/blankImageView"  
    android:layout_height="wrap_content"/> 

<Button android:layout_width="wrap_content" 
    android:layout_height="wrap_content"        
    android:id="@+id/start"  
    android:text="start"/> 

<Button android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    android:id="@+id/stop"  
    android:text="stop"/>         
</LinearLayout>

//the spin_animation.xml file in the res/drawable folder

<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    android:oneshot="false">  
    <item android:drawable="@drawable/frame1" android:duration="60" />  
    <item android:drawable="@drawable/frame2" android:duration="60" />  
    <item android:drawable="@drawable/frame3" android:duration="60" />  
    <item android:drawable="@drawable/frame4" android:duration="60" />  
</animation-list>

//and the frames

How can I make the image animate without clicking the start button?

Ovidiu Birgu
  • 439
  • 2
  • 6
  • 24
  • Did you have a problem getting an animation-list to work? That seems like the best option. – MikeIsrael Dec 12 '11 at 18:02
  • 2
    read the example above, and you'll understand what I mean – Ovidiu Birgu Dec 13 '11 at 09:15
  • I am not sure I totally understand the code it seems there are a start and stop button from the activity, is that activity your popup? If so I think this thread will help you out http://stackoverflow.com/questions/2785336/starting-frame-by-frame-animation if it is still causing issues by placing in the onStart, try the onWindowFocusChanged of the activity. If you could repost with a bit fuller amount of code including the calling activity, might shed some more light. – MikeIsrael Dec 14 '11 at 09:10
  • 2
    When the app is first started, I want the gif to be playing. I added the start and stop buttons to show that it works in the onClickListener methods. But it should run when the app starts, because I call frameAnimation.start(); . – Ovidiu Birgu Dec 14 '11 at 10:14
  • So this is going to be like your splash screen until the app loads? If so try running the animation from the onStart method, if that doesn't work then try from the onWindowFocusChanged method (be careful there, that method will be called a few times in the lifecycle of the activity) – MikeIsrael Dec 14 '11 at 14:15
  • thanks, the onWindowFocusChanged did the trick, but now, how can I start the animation when I click a button and the dialog is displayed? – Ovidiu Birgu Dec 14 '11 at 14:47

0 Answers0