Why animated drawable doesn't appear - android

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();
}
});
}
}

Related

Animation Drawable not working in GingerBread

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

frame animation is not working

I am working on apps in which i want to run two frame animation simultaneously but it is not working....
my code is as follow....
ImageView Background, planet, info;
AnimationDrawable infoview, backgroundview;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Background = (ImageView) findViewById(R.id.imageView1);
planet = (ImageView) findViewById(R.id.planet);
//Background.setImageResource(R.drawable.friend_night_sky_31000);
Log.w("debug", "planetanimation started");
planetStart(R.drawable.earth, R.drawable.background);
Log.w("debug", "planetanimation stoped");
info = (ImageView) findViewById(R.id.info);
info.setImageResource(R.drawable.earthinfo);
Log.w("DEBUG", "which is null:image " + infoview + "or" + backgroundview);
}
public void planetStart(final int pid, final int bid){
Thread timer = new Thread(){
#Override
public void run(){
try{
//Thread.sleep(time);
} catch (Exception e){
} finally{
Infoview.this.runOnUiThread(new Runnable(){
public void run(){
planet.setBackgroundResource(pid);
infoview = (AnimationDrawable) planet.getBackground();
infoview.start();
Background.setBackgroundResource(bid);
backgroundview = (AnimationDrawable) Background.getBackground();
backgroundview.start();
Log.w("DEBUG", "which is null:image " + infoview + "or" + backgroundview);
}
});
}
}
};
timer.start();
}
can any one help me why it is not working ?
Edit1 my earth file is as follow
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:drawable="#drawable/earthframe1" android:duration="150" />
<item android:drawable="#drawable/earthframe2" android:duration="150" />
<item android:drawable="#drawable/earthframe3" android:duration="150" />
<item android:drawable="#drawable/earthframe4" android:duration="150" />
<item android:drawable="#drawable/earthframe5" android:duration="150" />
<item android:drawable="#drawable/earthframe6" android:duration="150" />
<item android:drawable="#drawable/earthframe7" android:duration="150" />
<item android:drawable="#drawable/earthframe8" android:duration="150" />
<item android:drawable="#drawable/earthframe9" android:duration="150" />
</animation-list>
and bg file is as follow
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/bg_1" android:duration="150" />
<item android:drawable="#drawable/bgimage2" android:duration="150" />
<item android:drawable="#drawable/bgimage03" android:duration="150" />
<item android:drawable="#drawable/bgimage4" android:duration="150" />
<item android:drawable="#drawable/bgimage5" android:duration="150" />
</animation-list>
you have two solution either u can remove thread from the planetStart method or if u want to go with an exisiting code than give some value like Thread.sleep(1000); in thread sleep i have checked with this value and it works for me one more thing avoid second animation starting before the ending of first animation
Try Below code
package org.sample;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class SampleActivity extends Activity
{
ImageView Background, planet, info;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Background = (ImageView) findViewById(R.id.imageView1);
Background.setBackgroundResource(R.drawable.background);
planet = (ImageView) findViewById(R.id.planet);
planet.setBackgroundResource(R.drawable.earth);
info = (ImageView) findViewById(R.id.info);
info.setImageResource(R.drawable.earthinfo);
AnimationDrawable BackgroundAnimation = (AnimationDrawable) Background.getBackground();
BackgroundAnimation.start();
AnimationDrawable PlanetAnimation = (AnimationDrawable) planet.getBackground();
PlanetAnimation.start();
}
}
You can't start animation in onCreate (and yes, you do this). You should read this carefully (especially last example with paragraph).

How to start an start activity after android animation is over

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

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>

Layout Animation is blank

Nothing comes to the screen. The individual pngs render fine. what is wrong?
<ImageButton
android:id="#+id/btn_loading"
android:src="#drawable/loading_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And next is the file "loading_animation.xml" :
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
android:repeatMode="repeat" >
<item android:drawable="#drawable/load0" android:duration="20" />
<item android:drawable="#drawable/load1" android:duration="20" />
<item android:drawable="#drawable/load2" android:duration="20" />
<item android:drawable="#drawable/load3" android:duration="20" />
<item android:drawable="#drawable/load4" android:duration="20" />
<item android:drawable="#drawable/load5" android:duration="20" />
<item android:drawable="#drawable/load6" android:duration="20" />
<item android:drawable="#drawable/load7" android:duration="20" />
<item android:drawable="#drawable/load8" android:duration="20" />
</animation-list>
You have to start the animation
Use the following code in your java code
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myxml);
ImageView animation = (ImageView) findViewById(R.id.btn_loading);
animation.setBackgroundResource(R.drawable.loading_animation);
AnimationRoutine animationRoutine = new AnimationRoutine();
Timer t = new Timer(false);
t.schedule(animationRoutine, 100);
}
private class AnimationRoutine extends TimerTask {
AnimationRoutine() {
}
public void run() {
ImageView img = (ImageView) findViewById(R.id.btn_loading);
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
frameAnimation.start();
}
}
}

Categories

Resources