Android, set button background color with animation? - android

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!

Related

Repeating FadeOut on TextView

I have a TextView that acts as a warning label when a user tries to do something they shouldn't. I want it to blink a few times before staying there at the end.
I have blinking once working fine, but it will not repeat the animation. Is there something wrong with my animation?(because i have read the other questions about android animation and reportedly they work)
private void blink(int count){
if(count>0) {
AnimationSet anime = new AnimationSet(true);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(600);
Animation ani = new AlphaAnimation(1.0f, 0.0f);
ani.setDuration(600);
ani.setStartOffset(2000);
anime.addAnimation(anim);
anime.addAnimation(ani);
//anime.setStartOffset(0);
//anime.setStartTime(0);
//anime.setRepeatCount(Animation.INFINITE);
anime.setRepeatCount(count);
anime.setRepeatMode(Animation.RESTART);
anime.setFillAfter(true);
tWarningText.startAnimation(anime);
}
}
clearAnimation is called when some buttons are pressed, because tWarningText is, well, a warning text, I want it to stay there until they do something about it.
Instead of clearing animation after definite time, you can limit repeat count. This should also work fine.
public void blink(View view, int count) {
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50);
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(count);
view.startAnimation(anim);
}
Usage:
blink(textView, 10);
referred from sany's answer.

Need to interchange the textview when middle button click

First consider my layout 1). chennai 2). Route 3).Bangalore
When my middle button(Route) clicks need to interchange the text like 1).Bangalore 3).Chennai
When the text is interchanging need to animate the button as well as text
Note. i have looked this concept in sample app but no idea to do
Please suggest or provide related link
Thanks
refer this sample code:-
Explanation :-There are two buttons in layout 1).Edit 2).Done one above other( Edit:- Visible and Done:- visibility Gone)
After calling the below function the button will animate (rotate around x-axis) and its text will change to Done. Whats happening behind the scene is that the visibility of Edit becomes gone and visibility of Done becomes visible .
public void flipit() {
Edit = (Button) form.findViewById(R.id.next2);
Done = (Button) form.findViewById(R.id.next);
Interpolator accelerator = new AccelerateInterpolator();
Interpolator decelerator = new DecelerateInterpolator();
final Button visibleButton;
final Button invisibleButton;
if (Edit.getVisibility() == View.GONE) {
visibleButton = Done;
invisibleButton = Edit;
} else {
visibleButton = Edit;
invisibleButton = Done;
}
ObjectAnimator vToI = ObjectAnimator.ofFloat(visibleButton, "rotationY", 0f, 90f);
vToI.setDuration(500);
vToI.setInterpolator(accelerator);
final ObjectAnimator Itov = ObjectAnimator.ofFloat(invisibleButton, "rotationY", -90f, 0f);
Itov.setDuration(500);
Itov.setInterpolator(decelerator);
vToI.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator anim) {
visibleButton.setVisibility(View.GONE);
Itov.start();
invisibleButton.setVisibility(View.VISIBLE);
}
});
vToI.start();
}

Android ImageView changing alpha Animation

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.

flashing background

I have a LinearLayout with a few Buttons and TextViews. I want my background to flash at timed intervals, say from red to white to red and so on. Right now, I am trying this code, but it gives me a null pointer exception.
LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50);
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
ll.startAnimation(anim); // shows null pointer exception at this line
Please help me where am I going wrong?
You have specified the wrong View id here findViewById(R.layout.activity_main). It should be something like:
findViewById(R.id.your_view_id);
Also, make sure to call setContentView(R.layout.activity_main) right after super.onCreate
EDIT:
Here is the code that allows you to change only the background color with any colors you want. It looks like AnimationDrawable.start() doesn't work if called from Activity.onCreate, so we have to use Handler.postDelayed here.
final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
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);
layout.setBackgroundDrawable(drawable);
handler.postDelayed(new Runnable() {
#Override
public void run() {
drawable.start();
}
}, 100);
Try this
LinearLayout ll = (LinearLayout) findViewById(R.id.activity_main);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50);
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
ll.startAnimation(anim);
and If activity_main is your XML file name then
setContentView(R.layout.activity_main);
and use your layout id here
LinearLayout ll = (LinearLayout) findViewById(R.id.linear_layout_id);

i want to animate button then change the value of it, but couldnt in android

i want to animate button(ie; rotation, translation) then change the text of a button. unfortunately, it always first changes the text of the button then do the animation.
How can I achieve my goal?
Pls help me
my code is like this;
AnimationSet set = new AnimationSet(true);
Animation anim1 = new RotateAnimation(0, 360, 500, 750);
anim1.setDuration(3000);
anim1.setFillAfter(true);
set.addAnimation(anim1);
Animation anim2 = new RotateAnimation(0, 360, 1024, 824);
anim2.setDuration(3000);
anim2.setFillAfter(true);
set.addAnimation(anim2);
anim2.setStartOffset(3000);
first.clearAnimation();
set.setFillAfter(true);
first.startAnimation(set);
numbers[0]=min + (int)(Math.random() * ((max - min) + 1));
Your code starts the animation but is not blocking : once the animation is started, the program goes on.
You could try getting an handler and post the change text event at the right time :
Handler mHandler=new Handler();
Runnable lRunnable =new Runnable()
{
public void run()
{
//Your change text code
}
};
mHandler.postDelayed(lRunnable , 3000); // Or any other duration so you have the right effect
A better solution is to add an AnimationListener to the animation, or if you are on JB use the view property animators and the withEndAction() method. You should avoid the old animation framework if possible. It doesn't actually change the properties, it just draw the view with a transformation.
set.setAnimationListener(new AnimationListener() {
public void onAnimationEnd() {
// ...
}
public void onAnimationStart() {
}
public void onAnimationRepeat() {
}
}
But I recommend the view property animations if you can use them. They are much better to work with.

Categories

Resources