Custom Progress Dialog With Squre Image Rotation With AsynTask - android

I have created a custom Loading Progress Dialog. And its working well.
I am rotating 12 square Images here is one of them
But when I want to use it with AsynTask, The animation not working.
My Sample code is below.
Activity Where I Start Loading... Animation and Stop.
MainActivity.java
public class MainActivity extends Activity {
AnimationDrawable loadingViewAnim;
TextView loadigText;
ImageView loadigIcon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadigText = (TextView) findViewById(R.id.textView1);
loadigText.setText("Loading...");
loadigText.setVisibility(View.GONE);
loadigIcon = (ImageView) findViewById(R.id.imageView1);
loadigIcon.setVisibility(View.GONE);
loadigIcon.setBackgroundResource(R.anim.progress_animation_white);
loadingViewAnim = (AnimationDrawable) loadigIcon.getBackground();
}
//When User Touch on Screen The Loading... Animation Starts With Image Rotation
//If I start below code in AsynTask's onPreExecute method it doesn't work
public boolean onTouchEvent(MotionEvent event)
{
loadigText.setVisibility(View.VISIBLE);
loadigIcon.setVisibility(View.VISIBLE);
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
loadingViewAnim.start();
return true;
}
return super.onTouchEvent(event);
}
}
XML layout with Simple Text and Image for Progress Dialog.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/progress_sm_w01" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Loading..."
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
Animation List having 12 Images I am Rotating with Angle & Time Duration
progress_animation_white.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/progress_sm_w01" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w02" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w03" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w04" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w05" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w06" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w07" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w08" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w09" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w10" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w11" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w12" android:duration="50" />
</animation-list>
Here is the result Loading Screen..
My Question is Is there anyway to Achieve the same Loading Animation
with AsynTask.
Your Small Help will appreciated.

Well thanks to #Brontok and #URAndroid for their help. I got solved my problem.
So let me answer my own question, Hoe i achieved that Custom Loading animation
I have added few Image for Image Rotation Animation
Step 1 - in res/drawable folder
progress_sm_w00.png (Default blank transparent image)
progress_sm_w01.png (first animtion position)
progress_sm_w02.png
progress_sm_w03.png
progress_sm_w04.png
progress_sm_w05.png
progress_sm_w06.png
progress_sm_w07.png
progress_sm_w08.png
progress_sm_w09.png
progress_sm_w10.png
progress_sm_w11.png
progress_sm_w12.png (last animation position).
Example : one of these is below i have added
Step 2 - in res/anim folder created animation file name "loading_animation.xml"
<item android:drawable="#drawable/progress_sm_w01" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w02" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w03" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w04" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w05" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w06" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w07" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w08" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w09" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w10" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w11" android:duration="50" />
<item android:drawable="#drawable/progress_sm_w12" android:duration="50" />
</animation-list>
Step 3 - now created Custom Loading View layout in my Screen (Activity) wherever I need to show loading
example.
XML layout for my Facebook login screen
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0D000000"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ImageView
android:id="#+id/imageView111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/progress_sm_w00"
android:visibility="gone" />
<TextView
android:id="#+id/textView111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Searching..."
android:textColor="#ffffff"
android:textSize="12sp"
android:visibility="gone" />
</LinearLayout>
Step 4 - In java code (Activity) I created Loading view and after completing OnCreate method I started Asyn Task so my Animation get work properly.
public class ResultActivity extends Activity {
private static final String TAG = "ResultActivity";
private AnimationDrawable loadingViewAnim=null;
private TextView loadigText = null;
private ImageView loadigIcon = null;
private LinearLayout loadingLayout = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
loadingLayout = (LinearLayout)findViewById(R.id.LinearLayout1);
loadingLayout.setVisibility(View.GONE);
loadigText = (TextView) findViewById(R.id.textView111);
loadigText.setVisibility(View.GONE);
loadigIcon = (ImageView) findViewById(R.id.imageView111);
loadigIcon.setVisibility(View.GONE);
loadigIcon.setBackgroundResource(R.anim.loading_animation);
loadingViewAnim = (AnimationDrawable) loadigIcon.getBackground();
// This line is to start Asyn Task only when OnCreate Method get completed, So Loading Icon Rotation Animation work properly
loadigIcon.post(new Starter());
}
class Starter implements Runnable {
public void run() {
//start Asyn Task here
new LongOperation().execute("");
}
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//ToDo your Network Job/Request etc. here
return "Executed";
}
#Override
protected void onPostExecute(String result) {
//ToDo with result you got from Task
//Stop Loading Animation
loadingLayout.setVisibility(View.GONE);
loadigText.setVisibility(View.GONE);
loadigIcon.setVisibility(View.GONE);
loadingViewAnim.stop();
}
#Override
protected void onPreExecute() {
//Start Loading Animation
loadingLayout.setVisibility(View.VISIBLE);
loadigText.setVisibility(View.VISIBLE);
loadigIcon.setVisibility(View.VISIBLE);
loadingViewAnim.start();
}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
Step 5 - Here is the Result Screen with Progress Loading animation.
Hope this will help you. Cheers!!

Maybe you can use GLIDE, for example
Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.loading2)
.crossFade()
.into(imageView);
Show GIF file with Glide (image loading and caching library)

Related

Run sequence of images / animation within a AsyncTask

I created a custom ProgressDialog which has a lively ImageView. But this giving error for use within a AsyncTask execution.
AsyncTask out of work. I tried to use the animation with a runOnUiThread and not worked. How can I use this animation while running AsyncTask?
layout_progress.xml (only image)
<ImageView
android:id="#+id/ivLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#anim/loading"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>
loading.xml (animation)
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="#+id/animation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/load5_1" android:duration="150" />
<item android:drawable="#drawable/load5_2" android:duration="150" />
<item android:drawable="#drawable/load5_3" android:duration="150" />
<item android:drawable="#drawable/load5_4" android:duration="150" />
<item android:drawable="#drawable/load5_5" android:duration="150" />
<item android:drawable="#drawable/load5_6" android:duration="150" />
<item android:drawable="#drawable/load5_7" android:duration="150" />
<item android:drawable="#drawable/load5_8" android:duration="150" />
<item android:drawable="#drawable/load5_9" android:duration="150" />
<item android:drawable="#drawable/load5_10" android:duration="150" />
</animation-list>
method show of the custom progress dialog
public void show() {
super.show();
setContentView(layoutId);
final ImageView imageView = (ImageView) findViewById(R.id.ivLoading);
final AnimationDrawable anim = (AnimationDrawable) imageView.getDrawable();
final Runnable run = new Runnable() {
#Override
public void run() {
anim.start();
}
};
imageView.post(run);
//test 2 - doesnt work
/*
final Runnable run = new Runnable() {
#Override
public void run() {
anim.start();
imageView.post(this);
}
};
activity.runOnUiThread(run);
*/
}
You should not do this in an AsyncTask. Manipulation of the UI should be done only in the UI thread.
There is a simple example of what you are trying to achieve here:
http://developer.android.com/guide/topics/graphics/drawable-animation.html
Try and start the animation after you are sure that the ImageView has been attached to the view hierarchy. Quoting the link I posted:
It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

Gif in android xml not working

Hi I am looking to create gif animation in xml.I separated gif images and used the below code
myxm.xml
<?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/a" android:duration="100" />
<item android:drawable="#drawable/c" android:duration="100" />
<item android:drawable="#drawable/e" android:duration="100" />
</animation-list>
and in my layout i used the code
<ImageView
android:id="#+id/imageButton1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:src="#anim/myxm" />
But it doesn't works animation.Is it the right way to create gif animation in xml ? please help me thanks in advance :)
You can try the following,
AnimationDrawable frameAnimation;
ImageView view;
view = (ImageView) findViewById(R.id.imageButton1);
// Setting myxm.xml as the background of the image view
view.setBackgroundResource(R.anim.myxm);
// Typecasting the Animation Drawable
frameAnimation = (AnimationDrawable) view.getBackground();
// Called when Activity becomes visible or invisible to the user
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// Starting the animation when in Focus
frameAnimation.start();
} else {
// Stoping the animation when not in Focus
frameAnimation.stop();
}
}

using android animation-list

I'm having a little trouble getting an animated loading spinner to work for a splash page. Nothing shows up when I try to run the following code. Any suggestions? It seems that quite a few people have issues with this on google but I do not understand why mine is failing to work. Thanks!
animationloader.xml
<?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/loadingspinner1" android:duration="200" />
<item android:drawable="#drawable/loadingspinner2" android:duration="200" />
<item android:drawable="#drawable/loadingspinner3" android:duration="200" />
<item android:drawable="#drawable/loadingspinner4" android:duration="200" />
<item android:drawable="#drawable/loadingspinner5" android:duration="200" />
<item android:drawable="#drawable/loadingspinner6" android:duration="200" />
<item android:drawable="#drawable/loadingspinner7" android:duration="200" />
<item android:drawable="#drawable/loadingspinner8" android:duration="200" />
<item android:drawable="#drawable/loadingspinner9" android:duration="200" />
<item android:drawable="#drawable/loadingspinner01" android:duration="200" />
<item android:drawable="#drawable/loadingspinner11" android:duration="200" />
<item android:drawable="#drawable/loadingspinner12" android:duration="200" />
</animation-list>
SplashScreen.java
package com.secure.inmatecanteen;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class SplashScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
//Beginning the loading animation as we attempt to verify registration with SIP
ImageView ivLoader = (ImageView) findViewById(R.id.IVloadinganimation);
ivLoader.setBackgroundResource(R.anim.animationloader);
AnimationDrawable frameAnimation = (AnimationDrawable) ivLoader.getBackground();
frameAnimation.start();
}
}
splashscreen.xml
<?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"
android:orientation="horizontal"
android:background="#android:color/white" >
<ImageView
android:id="#+id/iclogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/iclogo"
android:adjustViewBounds="true"
/>
<ImageView
android:id="#+id/IVloadinganimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
/>
</LinearLayout>
Solved my own problem, You cannot start animations in the oncreate. It has to be in an onclick listener or inside a runnable.
I think the most elegant and versatile option is to extend from the ImageView class:
public class Loader extends ImageView {
public Loader(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public Loader(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Loader(Context context) {
super(context);
init();
}
private void init() {
setBackgroundResource(R.drawable.loader);
final AnimationDrawable frameAnimation = (AnimationDrawable) getBackground();
post(new Runnable(){
public void run(){
frameAnimation.start();
}
});
}
}
The loader.xml located in the drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/loader_1" android:duration="50" />
<item android:drawable="#drawable/loader_2" android:duration="50" />
<item android:drawable="#drawable/loader_3" android:duration="50" />
<item android:drawable="#drawable/loader_4" android:duration="50" />
.....
</animation-list>
Now include in your views something as simple as this:
<com.yourpackage.Loader
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
You can play/start animation from onWindowFocusChanged(boolean hasFocus) method.
Don't set image resource in xml code.
My XML is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:id="#+id/splashLayout"
android:background="#color/colorPrimary"
android:layout_height="match_parent">
<ImageView
android:layout_width="230dp"
android:layout_height="230dp"
android:id="#+id/iv_splash"
android:layout_marginTop="-80dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In Activity i do
public class SplashActivity extends Activity {
ImageView iv_splash;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
iv_splash=(ImageView)findViewById(R.id.iv_splash);
iv_splash.setBackgroundResource(R.drawable.splash);
final AnimationDrawable progressAnimation =(AnimationDrawable)iv_splash.getBackground();
progressAnimation.start();
}
}
Drawable file
<?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/logo" android:duration="400"/>
<item android:drawable="#drawable/logo1" android:duration="400"/>
</animation-list>
It's Working Good :)
Within this handler the animation is not fully attached to the window, so the animations can’t be started; instead, this is usually done as a result to user action (such as a button press) or within the onWindowFocusChangedhandler.
refer: professional android 4 application development
Check GitHub project here I have implemented: Check here
My MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/colorPrimaryDark"
tools:context=".MainActivity">
<ImageView
android:id="#+id/animation_imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:src="#drawable/animation_frame"
android:scaleType="fitCenter"
android:layout_marginBottom="50dp"/>
<TextView
android:layout_below="#+id/animation_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#fff"
android:fontFamily="sans-serif-thin"
android:text="Converting Please wait..."/>
</RelativeLayout>
My animation-list in drawable
<?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/ic_covert_f1" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f2" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f3" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f4" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f5" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f6" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f7" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f8" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f9" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f10" android:duration="90"/>
<item android:drawable="#drawable/ic_covert_f11" android:duration="90"/>
</animation-list>
My MainActivty.java
Always remember to use Runnable to start the animation
public class MainActivity extends AppCompatActivity {
AnimationDrawable progressAnimation;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.animation_imageview);
progressAnimation = (AnimationDrawable)imageView.getDrawable();
progressAnimation.setCallback(imageView);
progressAnimation.setVisible(true, true);
imageView.post(new Starter());
}
class Starter implements Runnable {
public void run() {
progressAnimation.start();
}
}
}
Works Perfectly Good Luck :)
Elegant solution - create your own Animated View which will follow life-cycle rules.
public class AnimatedImageView extends android.support.v7.widget.AppCompatImageView {
public AnimatedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setImageDrawable(ContextCompat.getDrawable(
context, R.drawable.animated_icon));
}
#Override
protected void onAttachedToWindow() {
// It's important to note that the start() method called on
// the AnimationDrawable cannot be called during the onCreate()
// method of your Activity, because the AnimationDrawable
// is not yet fully attached to the window.
super.onAttachedToWindow();
AnimationDrawable tapAnimation = (AnimationDrawable) getDrawable();
tapAnimation.start();
}
}
here is animated_icon.xml
<?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/tap_icon_1" android:duration="500" />
<item android:drawable="#drawable/tap_icon_2" android:duration="500" />
</animation-list>

My animation is not starting?

I am trying a simple frame by frame animation. My animation works on button tap, but I want it should start when the activity starts or load . I have tried onWindowFocusChanged() method also to start animation as per told in docs. I think i am making silly mistake. Anyone has idea.
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btnalarm;
AnimationDrawable AniFrame;
ImageView images;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
images=(ImageView)findViewById(R.id.myImageView);
images.setBackgroundResource(R.drawable.demo_animation);
AniFrame = (AnimationDrawable)images.getBackground();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.v("in focus", "in focus");
AniFrame.start();
}
demo_animation.xml file---->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/a" android:duration="50" />
<item android:drawable="#drawable/b" android:duration="50" />
<item android:drawable="#drawable/c" android:duration="50" />
<item android:drawable="#drawable/d" android:duration="50" />
<item android:drawable="#drawable/e" android:duration="50" />
<item android:drawable="#drawable/f" android:duration="50" />
<item android:drawable="#drawable/h" android:duration="50" />
<item android:drawable="#drawable/i" android:duration="50" />
<item android:drawable="#drawable/j" android:duration="50" />
<item android:drawable="#drawable/k" android:duration="50" />
</animation-list>
Add
AniFram.start() to the end of your onCreate().
Also inflate your R.layout.main.
Example:
LinearLayout layout = (LinearLayout)findViewById(R.id.main);
layout.startAnimation(AniFrame);
Also check your logcat for problems.
EDIT:
Check out this from the Docs Should help alot.
Animating a drawable
Just use anther thread.It will work fine.
public void onCreate(Bundle savedInstanceState) {
//your code
//At last of onCreate add these lines
images.post(new MyAnimation());
}
class MyAnimation implements Runnable{
#Override
public void run(){
AniFrame.start();
}
}
now it will work,just check it.
Try to use setCallback just before you start the animation as following:
AniFrame.setCallback(images);

Frame Animation on button click in Android

My Problem is I have Some Images & I used frame animation to display this images on click event of button but if i click button first time the image is display in sequence & if i click this button another time that time the image is not displayed. following is my code.
Animation.java file:-
public class Animation extends Activity {
Button mBtnOK;
AnimationDrawable frameAnimation;
ImageView imgView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtnOK = (Button) findViewById(R.id.mBtnOK);
mBtnOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
animate();
}
});
}
private void animate() {
imgView = (ImageView) findViewById(R.id.simple_anim);
imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.anim.simple_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) imgView
.getBackground();
frameAnimation.start();
frameAnimation.setOneShot(true);
}
}
Animation file:-
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
<item android:drawable="#drawable/monkey_1" android:duration="50" />
<item android:drawable="#drawable/monkey_2" android:duration="50" />
<item android:drawable="#drawable/monkey_3" android:duration="50" />
<item android:drawable="#drawable/monkey_4" android:duration="50" />
<item android:drawable="#drawable/monkey_5" android:duration="50" />
<item android:drawable="#drawable/monkey_6" android:duration="50" />
<item android:drawable="#drawable/monkey_7" android:duration="50" />
<item android:drawable="#drawable/monkey_8" android:duration="50" />
<item android:drawable="#drawable/monkey_9" android:duration="50" />
<item android:drawable="#drawable/monkey_10" android:duration="50" />
</animation-list>
The only way to restart a frame animation is to use the setVisible() which contains a flag to force the animation to reset to the first frame. If you modify the animating section of code like so:
AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
frameAnimation.setOneShot(true);
frameAnimation.setVisible(true, true);
frameAnimation.start();
The animation should always start from the first frame and run to completion each time you click the button. The animation can also be reset by toggling visibility on the drawable itself, instead of the ImageView that contains it.
HTH
#Dipak,
I have done with the animation using the same way you have done. Try to add this code, hope your error will be resolved. And also one more thing is use thread to run the animation. This will surely run it nicely.
if(frameAnimation.isRunning()) {
frameAnimation.stop();
frameAnimation.start();
}

Categories

Resources