I would like to animate some images.
Could someone tell me why this first piece of code does not work, but the second one does work ?
And if I have to use the second one, how do I STOP the animation into the runnable ?
EDIT : the first code works on android 4.x, but not on 2.2 (both simulator and device)
code 1 (into "onCreate()" ):
ImageView boule = (ImageView)findViewById(R.id.boule);
boule.setImageBitmap(null);
boule.setBackgroundResource(R.anim.anime);
AnimationDrawable animation = (AnimationDrawable)boule.getBackground();
animation.start();
// does not animate anything...
code 2 (also into "onCreate()" ) :
ImageView boule = (ImageView)findViewById(R.id.boule);
boule.setImageBitmap(null);
boule.setBackgroundResource( R.anim.anime );
final AnimationDrawable animation = (AnimationDrawable) boule.getBackground();
boule.post(new Runnable() {
public void run() {
if ( animation != null ) animation.start();
}
});
// OK, it works, but how do I stop this ?
You can try this.. this code works properly
BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.jth);
BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.jthj);
int duration = 10;
final AnimationDrawable ad = new AnimationDrawable();
ad.setOneShot(false);
ad.addFrame(frame1, duration);
ad.addFrame(frame2, duration);
iv.setBackgroundDrawable(ad);
ad.setVisible(true, true);
Put ad.start(); in button onClickListener and ad.stop(); for stop animantion
Related
I have created an object of AnimationDrawable with Bitmap frames.
Next what I want to do is, to perform a function every time the Frame changes in that Animation. But I have no idea how I will be able to achieve this.
Is there any inbuilt or custom method available through which we can get triggered for frame change? Please help!
To create and start the Animation, I have used code as below:
int duration = 200;
Animation myAnim = new AnimationDrawable();
for (Bitmap frame : framesList) {
myAnim.addFrame(new BitmapDrawable(frame), duration);
}
if (android.os.Build.VERSION.SDK_INT <=
android.os.Build.VERSION_CODES.JELLY_BEAN)
imgPicture.setBackgroundDrawable(myAnim);
else
imgPicture.setBackground(myAnim);
myAnim.start();
I have a button that, when clicked, I would like to have the button appear to flash by switching back and forth between two background colors.
This answer uses AlphaAnimation to make a flashing button:
final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
final Button btn = (Button) findViewById(R.id.your_btn);
btn.startAnimation(animation);
But I couldn't get it to work with background color.
Android Studio will auto-complete the following:
animation = new Animation() {
#Override
public void setBackgroundColor(int bg) {
super.setBackgroundColor(bg);
}
};
But I tried applying it to the button (with bg = Color.parseColor("#ffff9434")), but no dice.
Thank you in advance.
EDIT
Also tried the following, but it is deprecated and didn't work (from here)
Button btn = (Button)this.findViewById(R.id.btn1);
//Let's change background's color from blue to red.
ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
TransitionDrawable trans = new TransitionDrawable(color);
//This will work also on old devices. The latest API says you have to use setBackground instead.
btn.setBackgroundDrawable(trans);
trans.startTransition(5000);
ETID 2
Got it working, see answer below
Got it working! Thanks to this post!
final AnimationDrawable drawable = new AnimationDrawable();
final Handler handler = new Handler();
drawable.addFrame(new ColorDrawable(Color.RED), 400);
drawable.addFrame(new ColorDrawable(Color.GREEN), 400);
drawable.setOneShot(false);
btn = (Button) view.findViewById(R.id.flashBtn);
btn.setBackgroundDrawable(drawable);
handler.postDelayed(new Runnable() {
#Override
public void run() {
drawable.start();
}
}, 100);
Works like a charm!
I have four images that need loaded. I want one animation to play, wait 500ms, another one play, wait 500ms, etc.. All the animation does is change the alpha from 255 to 0 and then back to 255. All four imageViews need that animation.
I am having two problems currently.
1.) All of the images play at the same time.
2.) The next time the method is called, the animations don't work.
public void computerLights()
{
ImageView green = (ImageView)findViewById(R.id.imgViewGreen);
ImageView red = (ImageView)findViewById(R.id.imgViewRed);
ImageView blue = (ImageView)findViewById(R.id.imgViewBlue);
ImageView yellow = (ImageView)findViewById(R.id.imgViewYellow);
AlphaAnimation transparency = new AlphaAnimation(1, 0);
transparency.setDuration(500);
transparency.start();
green.startAnimation(transparency);
red.startAnimation(transparency);
blue.startAnimation(transparency);
yellow.startAnimation(transparency);
}
I'm not sure if this is the most elegant solution, but you could achieve this pretty easily with a handler that you can send messages to at 500ms intervals.
private int mLights = new ArrayList<ImageView>();
private int mCurrentLightIdx = 0;
private Handler mAnimationHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
ImageView currentLightIdx = mLights.get(currentLight);
AlphaAnimation transparency = new AlphaAnimation(1, 0);
transparency.setDuration(500);
transparency.start();
currentLight.startAnimation(transparency);
currentLightIdx++;
if(currentLightIdx < mLights.size()){
this.sendMessageDelayed(new Message(), 500);
}
};
public void computerLights()
{
ImageView green = (ImageView)findViewById(R.id.imgViewGreen);
ImageView red = (ImageView)findViewById(R.id.imgViewRed);
ImageView blue = (ImageView)findViewById(R.id.imgViewBlue);
ImageView yellow = (ImageView)findViewById(R.id.imgViewYellow);
mLights.add(green);
mLights.add(red);
mLights.add(blue);
mLights.add(yellow);
mAnimationHandler.sendMessage(new Message());
}
After the first message is sent the handler will continue to send messages every 500ms until all of the animations have been started.
I have an ImageView that is being used to show some frame animation.
The Duration for these images are all set to 100.
However, the ImageView needs to be hidden sometimes. So the animation is stopped and the ImageView is set to GONE.
When it's time to show the ImageView again, its visibility is set to VISIBLE and the animation is started.
HOWEVER - now the animation is really fast; instead of a duration of 100, it looks like 50. But when I check the duration it still says 100 - but it definitely doesn't look like it.
The code to hide and show the ImageView is as follows:
//hide the animation
final AnimationDrawable frameAnim = (AnimationDrawable) animImgView.getBackground();
if (frameAnim.isRunning() == true)
{
frameAnim.stop();
}
frameAnim.setVisible(false, false);
animImgView.setVisibility(View.GONE);
//show animation
animImgView.setVisibility(View.VISIBLE);
final AnimationDrawable frameAnim = (AnimationDrawable) animImgView.getBackground();
frameAnim.setVisible(true, true);
frameAnim.start();
What could be the trouble ?
After some experimentation I found that the best way is to simply implement:
//hide the animation
animImgView.setVisibility(View.GONE);
//show animation
animImgView.setVisibility(View.VISIBLE);
this will preserve the animation speed. My original intent with the animation stop/start was to ensure the CPU wasn't doing more than was needed.
To achieve the original goal of not just changing visibility, but also stopping the animation when invisible:
//hide the animation
animImgView.setVisibility(View.GONE);
//stop animation
final AnimationDrawable frameAnim = (AnimationDrawable) animImgView.getBackground();
if (frameAnim.isRunning() == true)
{
frameAnim.stop();
}
frameAnim.setVisible(false, false);
//start animation
final AnimationDrawable frameAnim = (AnimationDrawable) animImgView.getBackground();
frameAnim.setVisible(true, true);
frameAnim.start();
//show animation
animImgView.setVisibility(View.VISIBLE);
The difference here is that starting and stopping the animation occurs while the ImageView is invisible/gone. For some reason, starting and stopping the animation when it is visible causes the timing issues.
Source: I ran into this problem myself and fixed it using this technique.
Struggling with Android I found next solution:
don't use start() and stop() methods.
Use setVisible(true, true) to start animation:
animImgView.setVisibility(View.VISIBLE);
final AnimationDrawable frameAnim = (AnimationDrawable) animImgView.getBackground();
frameAnim.setVisible(true, true);
and setVisible(false, true) to stop it (note the bold true):
final AnimationDrawable frameAnim = (AnimationDrawable) animImgView.getBackground();
frameAnim.setVisible(false, true);
animImgView.setVisibility(View.GONE);
hi i want to give animation using only one image in drawable folder means in imageview tag of xml file i increase width and height of same image now i want to animate it in one place
i had the code which uses series of images seved in drawale folder and animate it but i have only 1 image ..pls give any suitable example which uses only 1 image and animate that image using imageview height and width increasing..thanks in adv
below is my code..
final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
splashImageView.setBackgroundResource(R.drawable.flag);
final AnimationDrawable frameAnimation = (AnimationDrawable)splashImageView.getBackground();
splashImageView.post(new Runnable(){
#Override
public void run() {
frameAnimation.start();
}
});
final SplashScreen sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
}
};
mSplashThread.start();
}
Try this:
ScaleAnimation scaler = new ScaleAnimation((float) 0.7, (float) 1.0, (float) 0.7, (float) 1.0);
scaler.setDuration(40);
imageView1.startAnimation(scaler);
"become big and small in one place" usually is called scaling.
I didn't really read it (just googled "android scaling images"), but this forum post might help you out. It seems to have a code tutorial:
http://www.anddev.org/resize_and_rotate_image_-_example-t621.html