AnimationDrawable doesn't autostart on Lollipop - android

I have an Android App where looped AnimationDrawbale where autostarted. With latest Lollipop it doesn't autostart no more.
There is a simple ImageView where the src address the next drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<level-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="#drawable/ic_weather_sun" />
<item android:maxLevel="1" android:drawable="#drawable/ic_weather_cloudsun" />
<item android:maxLevel="2" android:drawable="#drawable/ic_weather_cloud" />
<item android:maxLevel="3" android:drawable="#drawable/ic_weather_rain" />
<item android:maxLevel="4" android:drawable="#drawable/ic_weather_rainstorm" />
</level-list>
each item of the level list is another drawable xml.
Some of them are animation-list (Animationdrawable), others are layer-list with one or more layer composed by an animation-list. Here an example:
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<animation-list android:oneshot="false">
<item android:drawable="#drawable/ic_weather_layer_rain1" android:duration="100" />
<item android:drawable="#drawable/ic_weather_layer_rain2" android:duration="100" />
<item android:drawable="#drawable/ic_weather_layer_rain3" android:duration="100" />
<item android:drawable="#drawable/ic_weather_layer_rain4" android:duration="100" />
</animation-list>
</item>
<item android:drawable="#drawable/ic_weather_layer_cloudcolor1" />
<item android:drawable="#drawable/ic_weather_layer_cloud2" />
<item android:drawable="#drawable/ic_weather_layer_cloud1" />
</layer-list>
The ImageView level is set invisible at activity start and it is simply selected in this way.
final ImageView meteo = (ImageView)findViewById(R.id.image_meteo);
meteo.setVisibility(View.VISIBLE);
meteo.setImageLevel( weatherIdx );
Any idea on th reason? And how I should manage that?
I cannot address all AnimationDrawables, because there are several of them in a not known structure.
Thanks

Here is a class based on AppCompatImageView that will autostart both the source and background AnimationDrawables.
public class AnimationImageView extends AppCompatImageView
{
public AnimationImageView(Context context)
{
super(context);
}
public AnimationImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public AnimationImageView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
#Override
public void setBackgroundResource(#DrawableRes int resId)
{
super.setBackgroundResource(resId);
startAnimation(getBackground());
}
#Override
public void setBackgroundDrawable(Drawable background)
{
super.setBackgroundDrawable(background);
startAnimation(background);
}
#Override
public void setImageResource(#DrawableRes int resId)
{
super.setImageResource(resId);
startAnimation(getDrawable());
}
#Override
public void setImageDrawable(#Nullable Drawable drawable)
{
super.setImageDrawable(drawable);
startAnimation(drawable);
}
protected void startAnimation(Drawable drawable)
{
if (drawable instanceof AnimationDrawable)
{
((AnimationDrawable) drawable).start();
}
}
}
You can use it in your layouts as follows (change com.sample.ui.views to match the package where you will place the class above):
<com.sample.ui.views.AnimationImageView
android:id="#+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_src_animation"
android:background="#drawable/my_bg_animation"/>

should just be able to cast it to an animation drawable and then click start.
((AnimationDrawable) imageview.getDrawable()).start();
not sure why there is a change in behavior on 5.0 but this does not change behavior on lower APIs

Related

Pressed State Not Being Selected in ImageButton Tint List

I have a view class TintSelectorImageButton that applies a tint using DrawCompat::setTintList:
public class TintSelectorImageButton extends ImageButton{
// Actual resource values seem to be fairly large and positive, so -1 should be a safe sentinel
protected static final int NO_TINT = -1;
public TintSelectorImageButton(Context context, AttributeSet attrs){
super(context, attrs);
TypedArray args = context.obtainStyledAttributes(attrs, R.styleable.TintSelectorImageButton);
if(args.hasValue(R.styleable.TintSelectorImageButton_tintList)){
int colorStates = args.getResourceId(R.styleable.TintSelectorImageButton_tintList, NO_TINT);
if(colorStates != NO_TINT){
ColorStateList colorStateRes = ContextCompat.getColorStateList(context, colorStates);
Drawable wrappedDrawable = DrawableCompat.wrap(getDrawable().mutate());
DrawableCompat.setTintList(wrappedDrawable, colorStateRes);
setImageDrawable(wrappedDrawable);
}
}
args.recycle();
}
}
I am providing it a ColorStateList through the defined xml property that looks like this one:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="#color/sheet_toolbar_disabled"
android:state_enabled="false"/>
<item
android:color="#color/sheet_nav_clicked"
android:state_pressed="true"/>
<item
android:color="#color/sheet_toolbar_enabled"/>
</selector>
Unfortunately, the pressed state is not actually showing when I touch the button, but click events are being passed properly to the provided View.OnClickListener:
mPrevSheetButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showSheet(mPrevSheetUid);
}
});
To get things to work for now, I just took the image and colored it in an image editor and used this selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/button_prev_light_blue"
android:state_pressed="true"/>
<item
android:drawable="#drawable/button_prev_disabled"
android:state_enabled="false"/>
<item
android:drawable="#drawable/button_prev"/>
</selector>
Oddly, the drawable selector reaches the "pressed" state without issue.
To make this work with a ColorStateList change the background of your ImageButton to ?android:attr/selectableItemBackground or ?android:attr/selectableItemBackgroundBorderless.
For example,
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:src="#drawable/button" />

Selector background does not work?

Use Selector as view's background, like following codes :
my_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/cell_bg_e" />
<item android:state_checked="true" android:drawable="#drawable/cell_bg_e" />
<item android:state_selected="true" android:drawable="#drawable/cell_bg_e" />
<item android:drawable="#drawable/cell_bg_n_trans" />
</selector>
MyView.java
public class MyView extends LinearLayout
{
public MyView(Context context, CharSequence text, Drawable drawable) {
super(context);
setBackgroundResource(R.drawable.my_selector);
}
}
it works on all the devices except for some certain 800x480 resolution device(lick htc g12)
why ?
You should ensure the view you have set this selector drawable as a background of is clickable.
You can do this in code:
public MyView(Context context, CharSequence text, Drawable drawable) {
super(context);
setClickable(true);
setBackgroundResource(R.drawable.my_selector);
}
Alternatively in XML:
<MyView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/my_selector"
android:clickable="true"
android:orientation="horizontal"/>

Changing TextView background color on click android

I'm trying to change the background of a textview when clicked.
For instance, if the textview is click it the background changes to yellow and remains yellow until it is click again. Then it returns to its default background.
Currently textview the background changes on pressed down, but returns to default on release.
I have search the internet for solutions and look at most of all the solution on stackoverflow, still no solution.
Drawable/selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/circle_on" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/circle_on" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#drawable/circle_off"/>
</selector>
Drawable/circle_on:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="2dp"
android:color="#color/Gray" >
</stroke>
<solid android:color="#color/LightBlue" />
</shape>
Drawable/circle_off:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="2dp"
android:color="#color/Gray" >
</stroke>
<solid android:color="#color/WhiteSmoke" />
</shape>
TextView:
<TextView
style="#style/RoundText"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/repeat_selector"
android:clickable="true"
android:text="Sun" >
</TextView>
Text Style:
<style name="RoundText">
<item name="android:textColor">#555555</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textSize">15sp</item>
<item name="android:textStyle">bold</item>
<item name="android:fontFamily">sans-serif-thin</item>
</style>
Can someone please tell me what I'm doing wrong
Thanks.
MY Solution:
public class PlanTextView extends TextView {
private boolean _stateChanged;
private boolean _selected;
public boolean is_stateChanged() {
return _stateChanged;
}
public void set_stateChanged(boolean _stateChanged) {
this._stateChanged = _stateChanged;
}
public boolean is_selected() {
return _selected;
}
public void set_selected(boolean _selected) {
this._selected = _selected;
}
public PlanTextView(Context context) {
super(context);
}
public PlanTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PlanTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
<com.plan.views.PlanTextView
android:id="#+id/mon"
style="#style/RoundText"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circle_off"
android:clickable="true"
android:onClick="PlanOnClick"
android:text="mon" >
</com.plan.views.PlanTextView>
Activity
public void PlanOnClick(View v) {
PlanTextView view = (PlanTextView)v;
if (view.is_stateChanged()) {
view.setBackgroundResource(R.drawable.circle_off);
view.set_selected(false);
} else {
view.setBackgroundResource(R.drawable.circle_on);
view.set_selected(true);
}
view.set_stateChanged(!view.is_stateChanged());
}
Apart from the above answers,try this code snippet too.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:endColor="#AD1F2D" android:startColor="#AD1F2D" />
</shape>
</item>
<item android:state_focused="true">
<shape>
<gradient android:endColor="#ff4b46" android:startColor="#ff4b46" />
</shape>
</item>
<item>
<shape>
<gradient android:endColor="#ff4b46" android:startColor="#ff4b46" />
</shape>
</item>
</selector>
I hope this will useful for everyone.
If the textview is clicked the background changes to yellow and remains yellow until it is click again. Then it returns to its default background.
It's a matter of logic as you need to keep in your click listener the current click state.(blind coding):
textView.setOnClickClickListener(new View.OnClickListener() {
private boolean stateChanged;
public void onClick(View view) {
if(stateChanged) {
// reset background to default;
textView.setBackgroundDrawable(circleOffDrawable);
} else {
textView.setBackgroundDrawable(circleOnDrawable);
}
stateChanged = !stateChanged;
}
});
To improve the answer, you should keep stateChanged flag in the activity and retain its value across activity recreations - if the user rotates the activity. (Store the flag in onSaveInstanceState and restore in onCreate if its parameter is not null.)
use a onclicklistner() for your Textview
In your listener use
txt.setBackgroundColor(Color.RED);
For example
if(count==1)
{
txt.setBackgroundColor(Color.YELLOW);
count=0;
}
else
if(count==0)
{
txt.setBackgroundColor(Color.RED);
count==1;
}
In the onCreate() method,
LinearLayout(or Whatever layout you are using) ll = (LinearLayout)findViewById(R.id.mainlay);
and set the listener on textview:
TextView tv1 = (TextView)findViewById(R.id.maintext);
tv1.setOnClickListener(this);
And finally on click:
#Override
public void onClick(View v) {
ll.setBackgroundColor(whatever color);
or If you want to change the text background color,
tv1.setBackground(whatevercolor);
}
Hope this helps.
In case anyone needs it (Kotlin). This can toggle TextView's background and text color on click.
ToggleTextView.kt
class ToggleTextView : AppCompatTextView {
private var mfilterSelected = false
constructor(context: Context, attrs: AttributeSet?, defStyle: Int): super(context, attrs, defStyle) {}`
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context, checkableId: Int) : super(context) {}
constructor(context: Context) : super(context) {}
fun isFilterSelected(): Boolean {
return mfilterSelected
}
fun toggleFilterState() {
if (mfilterSelected) {
background = resources.getDrawable(R.drawable.toggle_1)
setTextColor(resources.getColor(R.color.gray))
mfilterSelected = false
} else {
background = resources.getDrawable(R.drawable.toggle_2)
setTextColor(resources.getColor(R.color.white))
mfilterSelected = true
}
}
}
--toggle_1.xml--
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<stroke android:width="1dp" android:color="#color/gray" />
<corners android:radius="10dp" />
<solid android:color="#color/white" />
</shape>
--toggle_2.xml--
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="10dp" />
<solid android:color="#color/orange" />
</shape>
Click listener in your Activity/Fragment:
yourTextView?.setOnClickListener {
yourTextView.toggleFilterState()
}
Usage in our xml layout file:
<ToggleTextView
android:id="#+id/yourTextView"
android:background="#drawable/toggle_1"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"/>

ProgressBar not an instance of LayerDrawable

I am trying to implement my own custom progressbar using 9 patch images. I am looking at the Sound ProgressBar example.
http://www.jagsaund.com/blog/2011/11/6/customizing-your-progress-bar-part-one.html
I have two problems that I have been unable to figure out why its happening:
No matter what I do my progressbar is not an instance of
layerdrawable, I am not sure why this is happening so I am unable to
get reference to the layer I want and change it grammatically.
The 9 patch image starts off stretched to the entire width of the container (relativelayout) not sure how to tell it to not
automatically scale so I can control its width grammatically.
Here are the following code snipets:
Drawable progressbar.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/progressbar_bg" >
</item>
<item android:id="#+id/progress">
<bitmap
android:src="#drawable/progressbar"
android:tileMode="repeat" />
</item>
</layer-list>
Style.xml
<style name="Widget">
</style>
<style name="Widget.ProgressBar">
<item name="android:indeterminateOnly">true</item>
<item name="android:indeterminateBehavior">repeat</item>
<item name="android:indeterminateDuration">3500</item>
<item name="android:minWidth">48dip</item>
<item name="android:maxWidth">48dip</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
<style name="Widget.ProgressBar.RegularProgressBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#drawable/progressbar</item>
<item name="android:indeterminateDrawable">#android:drawable/progress_indeterminate_horizontal</item>
<item name="android:minHeight">1dip</item>
<item name="android:maxHeight">10dip</item>
</style>
Custom View inside XML layout file:
<com.custom.ahmad.views.NfcProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/timeOutProgress"
android:layout_above="#+id/tvStart"
style="#style/Widget.ProgressBar.RegularProgressBar" />
Finally The custom Progressbar class:
public class NfcProgressBar extends ProgressBar {
private String text = "";
private int textColor = Color.BLACK;
private float textSize = 15;
public NfcProgressBar(Context context) {
super(context);
}
public NfcProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NfcProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected synchronized void onDraw(Canvas canvas) {
Drawable progressDrawable = this.getProgressDrawable();
Log.i("Testing", "progressDrawable : " + ((progressDrawable != null) ? progressDrawable : "null"));
Log.i("Testing", "progressDrawable instanceof LayerDrawable : " + ((progressDrawable instanceof LayerDrawable)? "true" : "false"));
if (progressDrawable != null && progressDrawable instanceof LayerDrawable) {
// Get the layer and do some programming work.
}
}

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>

Categories

Resources