Android Frame Animation only plays first frame - android

When I launch the emulator, the animation only plays the first frame of the animation, sits for the duration of the timer and then starts the activity.
This is my Drawable 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/title1" android:duration="200" />
<item android:drawable="#drawable/title2" android:duration="200" />
<item android:drawable="#drawable/title3" android:duration="200" />
<item android:drawable="#drawable/title4" android:duration="200" />
<item android:drawable="#drawable/title5" android:duration="200" />
<item android:drawable="#drawable/title6" android:duration="200" />
<item android:drawable="#drawable/title7" android:duration="200" />
<item android:drawable="#drawable/title8" android:duration="200" />
<item android:drawable="#drawable/title9" android:duration="200" />
<item android:drawable="#drawable/title10" android:duration="200" />
<item android:drawable="#drawable/title11" android:duration="200" />
</animation-list>
This is my layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/gyro"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
This is my java file
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Start the animation (looped playback by default).
ImageView splash = (ImageView) findViewById(R.id.gyro);
splash.setBackgroundResource(R.drawable.title);
AnimationDrawable splashAnimation = (AnimationDrawable) splash.getBackground();
splashAnimation.start();
Thread logoTimer = new Thread(){
public void run(){
try{
int logoTimer = 0;
while(logoTimer <10000){
sleep(100);
logoTimer = logoTimer + 100;
}//end of while loop
//mpsplash.stop();
startActivity(new Intent("tv.bScienceFiction.scrip.or.scrap.CLEARSCREEN"));
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}//end of finally
}//end of run
};//end of new Thread
logoTimer.start();
}

Start it from the UI thread after you exit onCreate
splash.postDelayed(new Runnable() {
public void run() {
splashAnimation.start();
}
}, 200);

Related

Animate the widget app Imageview in which i want to do rotate animation of the image view

I want to develop an widget application in which i want to do some rotate animation of the image view i am confused with with widget provider class i am trying but i am unable to do it if any body don please solve my problem. I am posting my code completely
public class Widget extends AppWidgetProvider{
String act;
private Handler start_handler ;
private AnimationDrawable analogClockBee_anim = null;
private ImageView clock4_bee_Image = null;
private Animation animation_bee = null;
RemoteViews myViews;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
act=intent.getAction();
animation_bee = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation_bee.setRepeatCount(-1);
animation_bee.setDuration(35000);
animation_bee.setInterpolator(context, android.R.anim.linear_interpolator);
// clock4_bee_Image = (ImageView) findViewById(R.id.clock_bee_rotate_anim);
clock4_bee_Image.setBackgroundResource(R.drawable.bee_animation);
analogClockBee_anim = (AnimationDrawable)clock4_bee_Image.getBackground();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(act)) {
myViews=new RemoteViews(context.getPackageName(), R.layout.widget);
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_ID), myViews);
}
myViews.setImageViewResource(R.id.clock_bee_rotate_anim, R.layout.widget);
start_handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what == 0)
{
analogClockBee_anim.start();
clock4_bee_Image.startAnimation(animation_bee);
}
}
};
new Thread(new Runnable()
{
#Override
public void run()
{
// TODO Auto-generated method stub
try {
Thread.sleep(500);
Log.i("inside handler", "5");
start_handler.sendEmptyMessage(0);
Log.i("inside handler", "6");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
my manifest code
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".Widget" android:label="My Custom Clock Widget">
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget" />
</receiver>
</application>
</manifest>
rotate animation in anim folder
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:interpolator="#android:anim/linear_interpolator"
>
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="350"/>
</set>
animation drawable folder xml file
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bee_anim"
android:oneshot="false">
<item android:drawable="#drawable/bee_0" android:duration="50" />
<item android:drawable="#drawable/bee_1" android:duration="50" />
<item android:drawable="#drawable/bee_2" android:duration="50" />
<item android:drawable="#drawable/bee_3" android:duration="50" />
<item android:drawable="#drawable/bee_4" android:duration="50" />
<item android:drawable="#drawable/bee_5" android:duration="50" />
<item android:drawable="#drawable/bee_6" android:duration="50" />
<item android:drawable="#drawable/bee_7" android:duration="50" />
<item android:drawable="#drawable/bee_8" android:duration="50" />
<item android:drawable="#drawable/bee_9" android:duration="50" />
</animation-list>
manifest referred xml file app widget provider
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:minHeight="146dp"
android:updatePeriodMillis="0"
android:initialLayout="#layout/widget"/>
layout widget xml file which contain analog clock widget and image view to be animated
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Widget"
android:gravity="center" >
<AnalogClock
android:id="#+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hand_hour="#drawable/clock_4_hour"
android:hand_minute="#drawable/clock_4_min"
android:dial="#drawable/clock_4_bg"/>
<ImageView
android:id="#+id/clock_bee_rotate_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/analogClock1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:background="#drawable/bee_0" />
</RelativeLayout>
please some one solve my problem i have strucked in this problem.thanks in advance

How to animate an Animation in Android

i want to ask that i want to make this animation as single image and wanted to apply some other view animation on it like move or alpha etc. how this can be done ? any idea ? simply i wanted to apply view animation on drawable animation.
i have some drawable like s1,s2,s3,s4 to s16 the XML is
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/s1" android:duration="200" />
<item android:drawable="#drawable/s2" android:duration="200" />
<item android:drawable="#drawable/s3" android:duration="200" />
<item android:drawable="#drawable/s12" android:duration="200" />
<item android:drawable="#drawable/s13" android:duration="200" />
<item android:drawable="#drawable/s14" android:duration="200" />
<item android:drawable="#drawable/s15" android:duration="200" />
<item android:drawable="#drawable/s16" android:duration="200" />
</animation-list>
and java code is
anim = (ImageView) findViewById(R.id.black);
anim.setBackgroundResource(R.drawable.smurf);
AnimationDrawable hello = (AnimationDrawable) anim.getBackground();
hello.start();
here smurf in drawable is the xml file for drawable animation.
we can do that animation but we have to maintain handler for that.
private int index = 0;
private Handler handler = new Handler();
private AnimationDrawable drawable;
Runnable runnable = new Runnable() {
#Override
public void run() {
imageView.setImageDrawable(drawable.getFrame(index));
index++;
if (index == drawable.getNumberOfFrames()) {
index = 0;
}
handler.postDelayed(runnable, drawable.getDuration(index));
}
};
in your onclick or where you want to start animation write like this:
handler.postDelayed(runnable, drawable.getDuration(index));
and your onDestroy() remove the handeler call backs like this:
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}

App crashes when setting ImageView source to AnimationDrawable

Im trying to set the source of an ImageView to an AnimationDrawable but no matter what way I do it the app always crashes at that point. heres the animation resource:
<?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/f11" android:duration="200" />
<item android:drawable="#drawable/f2" android:duration="200" />
<item android:drawable="#drawable/f3" android:duration="200" />
<item android:drawable="#drawable/f4" android:duration="200" />
<item android:drawable="#drawable/f5" android:duration="200" />
<item android:drawable="#drawable/f6" android:duration="200" />
<item android:drawable="#drawable/f7" android:duration="200" />
<item android:drawable="#drawable/f8" android:duration="200" />
<item android:drawable="#drawable/f9" android:duration="200" />
<item android:drawable="#drawable/f10" android:duration="200" />
<item android:drawable="#drawable/f11" android:duration="200" />
<item android:drawable="#drawable/f12" android:duration="200" />
<item android:drawable="#drawable/f13" android:duration="200" />
<item android:drawable="#drawable/f14" android:duration="200" />
</animation-list>
in my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/banim"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="#anim/anim"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/play"
android:src="#drawable/play"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/achievements"
android:src="#drawable/achievements"
/>
</LinearLayout>
</FrameLayout>
heres my activity:
public class Menu extends Activity {
AnimationDrawable animationDrawable;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ImageView gif = (ImageView)findViewById(R.id.banim);
animationDrawable = (AnimationDrawable)gif.getBackground();
animationDrawable.start();
final Intent intent = new Intent(this, MyActivity.class);
final Intent intent1 = new Intent(this, Achievements.class);
final ImageView bplay = (ImageView)findViewById(R.id.play);
final ImageView bachieve = (ImageView)findViewById(R.id.achievements);
final View.OnClickListener buttonClickListener;
buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view == bplay){
startActivity(intent);
}else if (view == bachieve){
startActivity(intent1);
}
}
};
bplay.setOnClickListener(buttonClickListener);
bachieve.setOnClickListener(buttonClickListener);
}
}
Ive tried setting it programmatically with
gif.setImageResource(R.anim.anim);
it crashes at that point. I tried moving anim to the drawable folder but still no luck.
can someone help me?
You must run your animation drawable from another thread other than the main thread, then update your code like this code snippet and it will work.
In Your activity do the follolwing:
public class Menu extends Activity {
AnimationDrawable animationDrawable;
ImageView gif ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
// the fix is here
gif = (ImageView)findViewById(R.id.banim);
gif.setImageResource(R.anim.anim);
Starter starter = new Starter();
gif.post(starter);
final Intent intent = new Intent(this, MyActivity.class);
final Intent intent1 = new Intent(this, Achievements.class);
final ImageView bplay = (ImageView)findViewById(R.id.play);
final ImageView bachieve = (ImageView)findViewById(R.id.achievements);
final View.OnClickListener buttonClickListener;
buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view == bplay){
startActivity(intent);
}else if (view == bachieve){
startActivity(intent1);
}
}
};
bplay.setOnClickListener(buttonClickListener);
bachieve.setOnClickListener(buttonClickListener);
}
class Starter implements Runnable {
public void run() {
// Get the background, which has been compiled to an
// AnimationDrawable
// object.
AnimationDrawable frameAnimation = (AnimationDrawable) gif
.getBackground();
frameAnimation.setOneShot(false);
// Start the animation (looped playback by default).
frameAnimation.start();
}
}
}

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

image animation in android

I have to repeat the image sequence I am using with Thread and AnimationDrawable but it is not working continuously. I don't want to stop this animation until next activity is started through button click event.
Here is my java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);{
final ImageView splashImage=(ImageView)findViewById(R.id.heartFunction);
splashImage.setBackgroundResource(R.drawable.slide_right);
splashAnimation = (AnimationDrawable) splashImage.getBackground();
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if ( isFocused ) {
//isFocused = false;
splashAnimation.start();
var=false;
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(SPLASH_DISPLAY_LENGTH);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
slide_right.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/heartcolored0" android:duration="200" />
<item android:drawable="#drawable/heartcolored2" android:duration="200" />
<item android:drawable="#drawable/heartcolored4" android:duration="200" />
<item android:drawable="#drawable/heartcolored5" android:duration="200" />
<item android:drawable="#drawable/heartcolored6" android:duration="200" />
<item android:drawable="#drawable/heartcolored7" android:duration="200" />
<item android:drawable="#drawable/heartcolored8" android:duration="200" />
<item android:drawable="#drawable/heartcolored9" android:duration="200" />
<item android:drawable="#drawable/heartcolored10" android:duration="200" />
<item android:drawable="#drawable/heartcolored11" android:duration="200" />
<item android:drawable="#drawable/heartcolored12" android:duration="200" />
<item android:drawable="#drawable/heartcolored13" android:duration="200" />
</animation-list>
If you want your animation to contiuously run then you need to set android:oneshot="false"
You were saying before to only run through once.
If you want an animation to run until you click the screen to go to the next activity. Start the animation when the onWindowFocusChanged function
#Override
public void onWindowFocusChanged(boolean hasFocus){
splashanimation.start();
}
Then use an onTouchEvent to catch the touch, start a new activity and finish the old activity.
#Override
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Intent i = new Intent(Anim.this, Main.class);
startActivity(i);
finish();
}
return true;
}
Hope this helps, your question is very hard to read/understand.

Categories

Resources