This question is already been asked,but i didn't find any suitable answers and so I'm posting the question.
I've a ProgressBar animation which has multiple images and loads these images like a progressbar. I'm able to run it successfully in 4.0 and higher versions but it doesn't work in 2.3.How to make it work in lower versions?
My Animation Drawable XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
android:pivotX="50%"
android:pivotY="50%" >
<item android:drawable="#drawable/p1_wheel" android:duration="200"/>
<item android:drawable="#drawable/p2_wheel" android:duration="200"/>
<item android:drawable="#drawable/p3_wheel" android:duration="200"/>
<item android:drawable="#drawable/p4_wheel" android:duration="200"/>
<item android:drawable="#drawable/p5_wheel" android:duration="200"/>
<item android:drawable="#drawable/p6_wheel" android:duration="200"/>
<item android:drawable="#drawable/p7_wheel" android:duration="200"/>
<item android:drawable="#drawable/p8_wheel" android:duration="200"/>
<item android:drawable="#drawable/p9_wheel" android:duration="200"/>
<item android:drawable="#drawable/p10_wheel" android:duration="200"/>
<item android:drawable="#drawable/p11_wheel" android:duration="200"/>
<item android:drawable="#drawable/p12_wheel" android:duration="200"/>
</animation-list>
My XML File
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
My Java File
package com.example.progressbarsample;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.DrawableContainer;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
ImageView progressbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbar = (ImageView) findViewById(R.id.imageView1);
progressbar.setBackgroundResource(R.drawable.progressbar);
final AnimationDrawable frame = (AnimationDrawable) progressbar.getBackground();
progressbar.post(new Runnable() {
#Override
public void run() {
frame.start();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
At Last I found a solution to this.
progressbar = (ImageView) findViewById(R.id.imageView1);
progressbar.setBackgroundResource(R.drawable.progressbar);
final AnimationDrawable frame = (AnimationDrawable)getResources.getDrawable(R.drawable.progressbar); \\progressbar is the anim.xml file
progressbar.setBackgroundDrawable(frame);
progressbar.post(new Runnable() {
#Override
public void run() {
frame.start();
}
});
}
try animating after the view is visible like this:
#Override
public void onWindowFocusChanged (bool hasFocus)
{
super.onWindowFocusChanged (hasFocus);
//get the animation from your view
AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground();
//set it visible and start animation
explosionAnimation.setVisible (true, true);
explosionAnimation.start ();
}
The method onWindowFocusChanged from Activity is called after the view is already visible and it gets the focus. So, you will be able to animate your image.
I've tried without setVisible but only a static image was shown. The same happens when I started the animation from onResume, onStart, etc.
The code above works fine for me, including Android 2.3. I'm currently working with mono (with Xamarin), but I hope it's gonna work for you in Java.
I have worked with Drawable Animations and it works for me here my Code:
mImageView.setBackgroundResource(R.drawable.anim);
AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground();
explosionAnimation.start();
But i think its becausse of the post, try the code without using the progressbar.post
Related
I follow the answer from this question: link
But I get this on my app:
As you can see on the ActionBar's right side there are two refresh icons (they were actually rotating when I took the screenshot). But I just want one to be there. I'm quite sure the problem is one of them is the item from the Menu XML file and the other is the ImageView from the actionbar_inderterminate_progress.xml file.
Here is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mypackage.android.design.appdesgin.MainActivity" >
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:visible="false" />
<item android:id="#+id/action_create_local_backup"
android:title="Search"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
android:visible="false" />
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="always"
android:title="Refresh">
</item>
<TextView
android:id="#+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
And here is my actionbar_inderterminate_progress.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/indeterminate_progress"
style="#android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_menu_refresh" />
My animation XML file:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" />
And my java code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
refreshItem = item;
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void refresh() {
/* Attach a rotating ImageView to the refresh item as an ActionView */
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView = (ImageView) inflater.inflate(R.layout.refresh_button, null);
Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.animator.rotate_refresh);
rotation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(rotation);
refreshItem.setActionView(imageView);
}
Can anyone point me where and what am I doing wrong to make it only display one animated refresh button?
I was inflating the refresh button two times, one in MyActivity class and the other one on the Fragment that requires the refresh button.
I'm using the setBackgroundDrawable method to change the background of the button on my Activity. The onClickListener also has an Intent to open up a new Activity. However, when I return to the past Activity by hitting the physical back button, the button with the onClickListener assigned to it still has the onClick background set. If I return to the previous Activity by using the back button in the Action Bar, it works correctly. I tried to use a selector XML, but Android Studio gives me render errors, and it doesn't load when I compile.
Here is the MainActivity.java:
package com.jordandebarth.supercalculator;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton pythag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33b5e5")));
final ImageButton pythag = (ImageButton) findViewById(R.id.pythag_button);
pythag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pythag.setBackgroundResource(R.drawable.pythag_button_selector);
Intent pythagIntent = new Intent(MainActivity.this, PythagoreanActivity.class);
startActivity(pythagIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml:
<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:background="#e5e5e5"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:layout_width="fill_parent"
android:background="#drawable/pythag_button"
android:layout_height="wrap_content"
android:id="#+id/pythag_button"
android:focusable="true"/>
</RelativeLayout>
user selector in xml file :
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_hover" android:state_pressed="true"></item>
<item android:drawable="#drawable/btn_hover" android:state_focused="true"></item>
<item android:drawable="#drawable/btn" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#drawable/btn_hover" android:state_enabled="false"></item>
</selector>
layout.xml
<Button
android:id="#+id/btnOk"
android:background="#drawable/button_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="5dp"
android:text="#string/yes" />
I want to create animated drawable from 14 png images.
I added the 14 images to all drawable- folders, and created a animated-list like below, but nothing appear, what is the problem ?
circle.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/f1" android:duration="50" />
<item android:drawable="#drawable/f2" android:duration="50" />
<item android:drawable="#drawable/f3" android:duration="50" />
<item android:drawable="#drawable/f4" android:duration="50" />
<item android:drawable="#drawable/f5" android:duration="50" />
<item android:drawable="#drawable/f6" android:duration="50" />
<item android:drawable="#drawable/f7" android:duration="50" />
<item android:drawable="#drawable/f8" android:duration="50" />
<item android:drawable="#drawable/f9" android:duration="50" />
<item android:drawable="#drawable/f10" android:duration="50" />
<item android:drawable="#drawable/f11" android:duration="50" />
<item android:drawable="#drawable/f12" android:duration="50" />
<item android:drawable="#drawable/f13" android:duration="50" />
<item android:drawable="#drawable/f14" android:duration="50" />
</animation-list>
layout 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="vertical" >
<Button
android:id="#+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<ImageView
android:id="#+id/imgCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
java code:
package pit.opensource.animation;
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 CircleAnimationActivity extends Activity {
/** Called when the activity is first created. */
Button btnStart;
ImageView imgCircle;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart = (Button)findViewById(R.id.btnStart);
imgCircle = (ImageView) findViewById(R.id.imgCircle);
imgCircle.setBackgroundResource(R.drawable.circle);
AnimationDrawable ani = (AnimationDrawable) imgCircle.getBackground();
ani.start();
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// ani.start();
}
});
}
}
The background should be animation, you should put circle.xml to res/anim, and change
imgCircle.setBackgroundResource(R.drawable.circle);
to
imgCircle.setBackgroundResource(R.anim.circle);
Try following code to start the animation
imgCircle.post(new Runnable() {
#Override
public void run() {
AnimationDrawable ani = (AnimationDrawable) imgCircle.getBackground();
ani.start();
}
});
or implement move the animation start to onWindowFocusChanged
public void onWindowFocusChanged(boolean flag) {
super.onWindowFocusChanged(flag);
AnimationDrawable anim = (AnimationDrawable) imgCircle.getBackground();
anim.start();
}
There could be third reasons.
First reason is OutOfMemoryError. You need to compress your images.
Solution
Second reason is android:oneshot parameter in your circle.xml. This animation runs for just 14 frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop.
Change
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
with
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
Third Reason is 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.
Change your MainActivity.java with this code:
package pit.opensource.animation;
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 CircleAnimationActivity extends Activity {
Button btnStart;
ImageView imgCircle;
AnimationDrawable ani;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgCircle = (ImageView) findViewById(R.id.imgCircle);
imgCircle.setBackgroundResource(R.drawable.circle);
ani = (AnimationDrawable) imgCircle.getBackground();
addListenerOnButton();
}
public void addListenerOnButton() {
btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
ani.start();
}
});
}
}
How to start an activity after the animation has ended.
I have added android:oneshot="true" in the xml but how to start a new activity
after this animation has stopped.I have attached the entire code below.
Please let me know how to start new activity.
package com.appsolut.example.animation;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class Animation extends Activity {
ImageView animation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation =
(AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
public void onStart() {
{
super.onStart();
animation = (ImageView)findViewById(R.id.imageAnimation);
animation.setBackgroundResource(R.drawable.animation);
}
}
}
animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item android:drawable="#drawable/img00000" android:duration="500" />
<item android:drawable="#drawable/img00001" android:duration="500" />
<item android:drawable="#drawable/img00002" android:duration="500" />
<item android:drawable="#drawable/img00003" android:duration="500" />
<item android:drawable="#drawable/img00004" android:duration="500" />
<item android:drawable="#drawable/img00005" android:duration="500" />
<item android:drawable="#drawable/img00006" android:duration="500" />
<item android:drawable="#drawable/img00007" android:duration="500" />
<item android:drawable="#drawable/img00008" android:duration="500" />
<item android:drawable="#drawable/img00009" android:duration="500" />
<item android:drawable="#drawable/img00010" android:duration="500" />
</animation-list>
Use and AnimationListener on your animation to do whatever you want on its onAnimationEnd() method.
After your animation call create a new Intent (From the Intent class).
Intent myIntent = new Intent(context, newActivity.class);
startActivity(myIntent);
Context can be getContext() or getBaseContext()
This should work. Also dont't forget to declare your new activity in the AndroidManifest or the application will crash!
Hope this helped.
Use the method hasEnded() to check if ended and then if it has then call your intent to open up the new activity.
It should work
Best Regards
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>