Layout Animation is blank - android

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

Related

Android: AnimationDrawable cast error

I was following the Google provided example of how to use AnimationDrawable with an ImageView. You can find it here: http://developer.android.com/guide/topics/graphics/drawable-animation.html
imageView.setBackgroundResource(R.drawable.animation);
AnimationDrawable animation = (AnimationDrawable)imageView.getBackground();
animation.start();
When I run it I get the error:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Google seems to think this should work, but if you cannot cast a BitmapDrawable to AnimationDrawable I am not sure how this is supposed to work?
I figured out the solution to this problem.
imageView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
animation.start();
I have no idea why Google's documentation says to use the background, but using setImageDrawable and getDrawable works. Honestly it makes more sense it would work this way than the other way anyways.
I had the same problem. I know this thread is some month old, but maybe somebody what to read about my experience.
I dont know why, but google doesnt accept Spacemarks like "_" in his Picturenames while using it for animation. I uses names like "loading_frame1", and it doesnt work. I changed the names to something like "loadingframe1" and it works....
Before:
<?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/loading_frame1" android:duration="100" />
<item android:drawable="#drawable/loading_frame2" android:duration="100" />
<item android:drawable="#drawable/loading_frame3" android:duration="100" />
<item android:drawable="#drawable/loading_frame4" android:duration="100" />
<item android:drawable="#drawable/loading_frame5" android:duration="100" />
<item android:drawable="#drawable/loading_frame6" android:duration="100" />
<item android:drawable="#drawable/loading_frame7" android:duration="100" />
<item android:drawable="#drawable/loading_frame8" android:duration="100" />
<item android:drawable="#drawable/loading_frame9" android:duration="100" />
<item android:drawable="#drawable/loading_frame10" android:duration="100" />
<item android:drawable="#drawable/loading_frame11" android:duration="100" />
<item android:drawable="#drawable/loading_frame12" android:duration="100" />
<item android:drawable="#drawable/loading_frame13" android:duration="100" />
<item android:drawable="#drawable/loading_frame14" android:duration="100" />
<item android:drawable="#drawable/loading_frame15" android:duration="100" />
<item android:drawable="#drawable/loading_frame16" android:duration="100" />
<item android:drawable="#drawable/loading_frame17" android:duration="100" />
<item android:drawable="#drawable/loading_frame18" android:duration="100" />
<item android:drawable="#drawable/loading_frame19" android:duration="100" />
<item android:drawable="#drawable/loading_frame20" android:duration="100" />
</animation-list>
After:
<?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/loadingframe1" android:duration="100" />
<item android:drawable="#drawable/loadingframe2" android:duration="100" />
<item android:drawable="#drawable/loadingframe3" android:duration="100" />
<item android:drawable="#drawable/loadingframe4" android:duration="100" />
<item android:drawable="#drawable/loadingframe5" android:duration="100" />
<item android:drawable="#drawable/loadingframe6" android:duration="100" />
<item android:drawable="#drawable/loadingframe7" android:duration="100" />
<item android:drawable="#drawable/loadingframe8" android:duration="100" />
<item android:drawable="#drawable/loadingframe9" android:duration="100" />
<item android:drawable="#drawable/loadingframe10" android:duration="100" />
<item android:drawable="#drawable/loadingframe11" android:duration="100" />
<item android:drawable="#drawable/loadingframe12" android:duration="100" />
<item android:drawable="#drawable/loadingframe13" android:duration="100" />
<item android:drawable="#drawable/loadingframe14" android:duration="100" />
<item android:drawable="#drawable/loadingframe15" android:duration="100" />
<item android:drawable="#drawable/loadingframe16" android:duration="100" />
<item android:drawable="#drawable/loadingframe17" android:duration="100" />
<item android:drawable="#drawable/loadingframe18" android:duration="100" />
<item android:drawable="#drawable/loadingframe19" android:duration="100" />
<item android:drawable="#drawable/loadingframe20" android:duration="100" />
</animation-list>
And here the LoadingAnimation.class Listing
package com.justkidding.animation;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class LoadingAnimation extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_animation);
}
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView animation = (ImageView)findViewById(R.id.aniimage);
animation.setBackgroundResource(R.drawable.loading_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.loading_animation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Google's code works. The "can not be cast issue" which lead me here, was because I was not paying attention and put my animation.xml in res.anim instead of res.drawable.
However I agree using setImageDrawable and getDrawable works better.
About this problem, I had a little oversight on the detail on google's example code in the documentation and this might be the case for a couple of persons using the guide.
There is an separate xml file that holds the drawables, indicating the transitions and has the tag:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust3" android:duration="200" />>
</animation-list>
The above file is named rocket_thrust, and it is this same file that is set as backgroundDrawable in the following lines:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
Give a try at this and be sure that documentation has no errors.
Best of luck.
Complete process to do animation is :
1. Create XML layout with imageView
and
2. Create XML file for animation suppose drawable/animation.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/twit" android:duration="120"></item>
<item android:duration="120" android:drawable="#drawable/a111"></item>
<item android:duration="120" android:drawable="#drawable/a2"></item>
<item android:duration="120" android:drawable="#drawable/a3"></item>
<item android:duration="120" android:drawable="#drawable/a4"></item>
<item android:duration="120" android:drawable="#drawable/a5"></item>
<item android:duration="120" android:drawable="#drawable/a6"></item>
</animation-list>
now
3. Create Main Activity
Then Type this code
public class AnimationMe extends AppCompatActivity {
private ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logoo);
imgView = (ImageView) findViewById(R.id.imgView);
// the frame-by-frame animation defined as a xml file within the drawable folder
/*imgView.setBackgroundResource(R.drawable.animation);*/
imgView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
// It's not possible to start the animation during the onCreate.
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animationDrawable = (AnimationDrawable)imgView.getDrawable();
if(hasFocus)
{
animationDrawable.start();
}
else
{
animationDrawable.stop();
}
}
}
*
Note : ImageView have background as drawable and give a name of
animation.xml not for a particular image and then call with
imageview.getDrawable in AnimationDrawable.
---- You can't Run Animation in onCreate Method. Set drawable property in Imageview in onCreate() but call AnimationDrawable method out of
block of onCreate().
*
Sure it will work !
Just to add more answer to this page based on my experience because Stackoverflow seems has a very limited answer for this issue
I my case I tried to animate my background layout which using drawable for rounded radius. I got logcat error
java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Turn out I must set the background attribute on my layout file to this drawable file
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
And then call this code on my Main Activity
val backgroundAnim = info_layout?.background as AnimationDrawable
backgroundAnim.start()
My mistake is previously I put #drawable/rounded_corner on layout file as background attribute.
Hope this can help somebody, as I spend 3 hours just to solve this issue.

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).

Why animated drawable doesn't appear

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 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>

Categories

Resources