image animation in android - 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.

Related

Repeat animation when button is press

I have an attack animation that I want to repeat animation whenever the button for attack is clicking all I can come up is one show of animation only,when the button is press again the animation won't repeat again
here is my animation code
imgAttack = (ImageView) findViewById(R.id.imgAttack);
imgAttack.setBackgroundResource(R.drawable.attack_anim);
attackanimation=(AnimationDrawable)imgAttack.getBackground();
btnAtk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
press=+1;
MaxHealth-=100;
swordAtk.start();
attackanimation.start();
health.setProgress(MaxHealth);
if(MaxHealth==0) {
health.setProgress(0);
Gold=Gold+1;
txtGold.setText("Gold:"+Gold);
SharedPreferences prefs = RpgActivity.this.getSharedPreferences(getString( R.string.PREF_FILE),MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(getString(R.string.SDR),Gold);
editor.apply();
}
}
});
here is my xml for attack_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:drawable="#drawable/attak0001" android:duration="100" />
<item android:drawable="#drawable/attak0002" android:duration="100" />
<item android:drawable="#drawable/attak0003" android:duration="100" />
<item android:drawable="#drawable/attak0004" android:duration="100" />
<item android:drawable="#drawable/attak0005" android:duration="100" />
<item android:drawable="#drawable/attak0006" android:duration="100" />
<item android:drawable="#drawable/attak0007" android:duration="100" />
</animation-list>
You can do something like this before attackAnimation.start()
attackAnimation.stop();
attackAnimation.selectDrawable(0);
and after that write attackAnimation.start().

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

Android Frame Animation only plays first frame

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

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

Android AnimationDrawable starts, and when done hangs on first frame

I'm trying to get the AnimationDrawable on my Android app to repeat.
I configured the android:oneshot on false. (tried both with java and with XML)
Still, whenever the animation is played, once it's done, it goes back to the first frame, and stops
This is my XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/network_scanning" android:oneshot="false">
<item android:drawable="#drawable/network_wireless_0" android:duration="300" />
<item android:drawable="#drawable/network_wireless_1" android:duration="300" />
<item android:drawable="#drawable/network_wireless_2" android:duration="300" />
<item android:drawable="#drawable/network_wireless_3" android:duration="300" />
<item android:drawable="#drawable/network_wireless_4" android:duration="300" />
</animation-list>
and this is the code to start the animation:
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
LinearLayout network = (LinearLayout) findViewById(R.id.wifi_anim);
AnimationDrawable anim = (AnimationDrawable) network.getBackground();
if (hasFocus)
{
anim.setOneShot(false);
anim.setCallback(network);
anim.setVisible(true, true);
anim.start();
}
}
hi you can get solution from here
http://docs.mono-android.net/Android.Graphics.Drawables.AnimationDrawable

Categories

Resources