How to run animation sequences more than single time - android

I am using sequence for animation in my project. I have set onClickListener to that image where I put animation sequences. now when first time I run the code, and click on image, it start run animation correctly but then it never run again on click. I have to again run the code. So how I can run animation sequences more than one time? I have put my codebelow to start animation sequence.
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
animation_door = (AnimationDrawable) image.getBackground();
animation_door.start();
} });

Well, before clicking your button again you have to stop the animation also, so you can try it like this,
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
animation_door = (AnimationDrawable) image.getBackground();
animation_door.stop();
animation_door.start();
}
});

Is your animation set as a oneShot? It's most likely in the end state and you just need to hint it back to the start state.
AnimationDrawable door = (AnimationDrawable) image.getBackground();
door.setOneShot(false);
door.start();
Keep in mind this will most likely lead to a looping animation, which you probably don't want. You could consider using an Animation Set instead since you'll have a bit more control over the animation itself.

Related

android gif animation and button

I want to create an animate img, so when I click a button I can see the animation.
in my project, I've just create buttons and images. In the drawable folder, I put the three png images and a frame_animation.xml, I set the images and the duration. In the java file, I put this code:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
}
});
but when I run the app, the animation doesn't work (I can see only one image, not all the three image)
You have to also START the animation, setting the resource is not enough.
In your case simply add myFrameAnimation.start();
Full example:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
myFrameAnimation.start();
}
});
Seems like you just left it out by accident, since you already got the AnimationDrawable from your resource.

Stop Blinking of TextView

This is a code for blinking textview on a button click..
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
recordShow.startAnimation(anim);
}
i have to stop blinking on another button click...what to do..??
Another approach could be:
1. Declare the Animation and TextView objects globally (outside any methods) in your Activity.
private Animation mAnim;
private TextView mRecordShow;
2. Setup a class that sets your animation properties and starts it. Let this class expect a TextView widget as its parameter.
protected void setBlinkingText(TextView textView) {
mAnim = new AlphaAnimation(0.0f, 1.0f);
mAnim.setDuration(1000); // Time of the blink
mAnim.setStartOffset(20);
mAnim.setRepeatMode(Animation.REVERSE);
mAnim.setRepeatCount(Animation.INFINITE);
textView.startAnimation(mAnim);
}
3. Setup another class that stops your animation on a given text view. Let this class expect a TextView widget as its parameter as well.
protected void removeBlinkingText(TextView textView) {
textView.clearAnimation();
}
4. Now you can use your classes wherever desired, passing it the appropriate text views.
e.g.
(a) In your onClick() method where you want to start the animation, replace all your animation code with:
setBlinkingText(mRecordShow);
(b) wherever you want to stop the animation on that text view, just call:
removeBlinkingText(mRecordShow);
The following assumes you want to stop the blink by clicking the same button. If you want to stop the click using a different button, you can split the if-else in the onClick() below into separate click handlers.
First, move anim outside onClick() and make it a field of the containing class. You need anim to be stored somewhere so you can cancel it later.
Animation anim = new AlphaAnimation(0.0f, 1.0f)
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
Second, create a boolean field in the containing class to keep track of whether the TextView is currently blinking:
boolean mBlinking = false;
Then:
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
if(!mBlinking){
recordShow.startAnimation(anim);
mBlinking = true;
} else{
recordShow.clearAnimation(anim); // cancel blink animation
recordShow.setAlpha(1.0f); // restore original alpha
mBlinking = false;
}
}

Start an animation automatically without the need of click on screen

i know this question has been asked many times before but i could never quite understand as the other codes were quite a bit more complex or different than mine. so like the question says ... how to start animation automatically instead of on click .. ill leave the code below ... thanks in advance.. !!
final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
a.reset();
final ImageView rImage = (ImageView) findViewById(R.id.title);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
rImage.startAnimation(a);
func(); //A call to the function.
}
});
Something like this should work... This will be triggered everytime the activity is "displayed".
#Override
public void onResume() {
super.onResume();
// layout should have bee initialized in onCreate
layout.post(new Runnable() {
#Override
public void run() {
rImage.startAnimation(a);
}
});
}
you can put your code in onResume, or onCreate depends of your needs, also you can set a timer to active this code, there are many ways...
How to start Animation immediately after onCreate?
http://damianflannery.wordpress.com/2011/06/08/start-animation-in-oncreate-or-onresume-on-android/

How to make a page flip like animation when changing between activities?

How can I make a page flip like animation when moving from one activity to other? On some ios applications I saw this, but when I searched for android I could not find any tutorials or code snippets for this.
Please help
Yes it is possible . Please look at this tutorial.
Here's a tutorial on how to add an animation when transistioning between two activities. However, instead of using a translate animation like in the article, you'll want to use a rotate animation.
The flip animation for Activity doesn't exist on Android..sorry!
Here is demo code from sdk:
/**
* <p>Example of using a custom animation when transitioning between activities.</p>
*/
public class Animation extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.fade_animation);
button.setOnClickListener(mFadeListener);
button = (Button)findViewById(R.id.zoom_animation);
button.setOnClickListener(mZoomListener);
}
private OnClickListener mFadeListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, Controls1.class));
// Supply a custom animation. This one will just fade the new
// activity on top. Note that we need to also supply an animation
// (here just doing nothing for the same amount of time) for the
// old activity to prevent it from going away too soon.
overridePendingTransition(R.anim.fade, R.anim.hold);
}
};
private OnClickListener mZoomListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, Controls1.class));
// This is a more complicated animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
};
}
all code is at apidemo/app/ ,:)

Issue with ImageButton.setVisibility()

I'm having a problem when setting the visibility of two image buttons one on top of the other. The idea is to implement a play/pause control. The problem is that the only part where setting the visibility actually works is in the click listeners of the buttons. If I try to change it somewhere else nothing happens. Any idea why is this happening?
playBtn.setOnClickListener(new OnClickListener() {//PLAY BUTTON LISTENER
public void onClick(View v) {
playBtn.setVisibility(ImageButton.GONE);
pauseBtn.setVisibility(ImageButton.VISIBLE);
mp.start();
}});
pauseBtn.setOnClickListener(new OnClickListener() {//PAUSE BUTTON LISTENER
public void onClick(View v) {
pauseBtn.setVisibility(ImageButton.GONE);
playBtn.setVisibility(ImageButton.VISIBLE);
mp.pause();
}});
final class SeekBarTask extends TimerTask {
public SeekBarTask(int duration) {
}
#Override
public void run() {
if(seekBar.getProgress() >= mp.getDuration()) {//IF SONG HAS FINISHED...
pauseBtn.setVisibility(ImageButton.GONE);//THESE ONES
playBtn.setVisibility(ImageButton.VISIBLE);//DOESN'T WORK
mp.stop();
}
else {
seekBar.incrementProgressBy(100);
}
}
}
I would recommend just changing the icon of one ImageButton.
I would think only one of two things could be happening. Either this code never gets hit, or the variables are not referring to the same object instances you're expecting them to. Have you put a breakpoint inside that condition? I would check that a break point even gets hit in there, and then check that the variables are pointing at the correct button instances.
Without seeing the rest of the code I have to ask...why are you checking on a progress bar for a "finished playing" condition versus using the media players on completion callback?
I'm doing something very similar, and I use the MediaPlayer's OnCompletionListener to flip the visibility of my buttons.
I don't remember the details of Android GUI manipulation but could it have to do that you're doing it from another thread and you're not supposed to?
i noticed that setting an ImageButton to View.INVISIBLE is not working when you have set an Animation to it. you have to remove the Animation then make it Invisible. bad pitfall i think...

Categories

Resources