flashing background - android

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

Related

Android TextView object are visible before ObjectAnimator starts

I am trying to execute a simple animation using ObjectAnimator. Here is the source:
TextView[] introTextViews = new TextView[] {(TextView) findViewById(R.id.intro_textView1_1),
(TextView) findViewById(R.id.intro_textView1_2),
(TextView) findViewById(R.id.intro_textView2_1),
(TextView) findViewById(R.id.intro_textView2_2),
(TextView) findViewById(R.id.intro_textView3_1),
(TextView) findViewById(R.id.intro_textView3_2),
(TextView) findViewById(R.id.intro_textView4_1),
(TextView) findViewById(R.id.intro_textView4_2)};
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator[] animators = new ObjectAnimator[introTextViews.length];
for(int i=0; i<introTextViews.length; i++){
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
animSet.play(animators[0]).with(animators[1]); // Anim1
animSet.play(animators[2]).with(animators[3]).after(animators[0]); //Anim2
animSet.play(animators[4]).with(animators[5]).after(animators[2]); //Anim3
animSet.play(animators[6]).with(animators[7]).after(animators[4]); //Anim4
animSet.setDuration(6000);
animSet.setInterpolator(new AccelerateDecelerateInterpolator());
animSet.start();
The problem is that TextView objects are in the screen before animation been started. While Anim1 is running the TextViewobjects that belong to the Anim2, Anim3, Anim4 are on the screen. When Anim2 time has come TextView objects dissappear and start the animation, while the Anim3 and Anim4 are still visible waiting for their time to come!
I want the TextView objects invisible until they will come in the screen after animation ends.
Finally I did this:
for(int i=0; i<introTextViews.length; i++){
//Add this line just to fix the animation initial X point
introTextViews[i].setX(getResources().getDisplayMetrics().widthPixels+10);
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
I know It is a little "hacky" and I am still waiting for any official answer!
Your solution is good,and there is another solution you can refer:
in your for loop,set all TextViews to invisible by setVisibility(View.Gone),then set AnimatorListener for each Animator,and implement its onAnimationStart,where you set the corresponding TextView visible.

Android, set button background color with animation?

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!

Having trouble positioning android dynamically loaded buttons

I'm creating buttons dynamically in my class, I try to position them using 'offsetLeftAndRight()' or '.leftMargin' and '.topMargin' as follows,
public class instruction extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instruct);
final Button btn = new Button(this);
RelativeLayout.LayoutParams paramsd2 =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsd2.leftMargin = 500;
paramsd2.topMargin = 500;
paramsd2.height = 60;
paramsd2.width = 200;
btn.offsetLeftAndRight(300);
btn.setLayoutParams(paramsd2);
addContentView(btn, paramsd2);
}
But the button always stays in the top left corner, how can I position it, what am I doing wrong?
AddContentView() is not the proper way to add a view in an already set layout.
make your main layout a RelativeLayout (check this in the instruct.xml layout file)
use its id to retreive a reference on it in your onCreate() method using
myRelativeLayout = (RelativeLayout) this.findViewById(R.id.itsId)
then add your button to this layout :
myRelativeLayout.addView(myButton);
the layout params of your button seems fine for positioning so it should work.
Set margin on button rather then layout
MarginLayoutParams marginParams = new MarginLayoutParams(backToMainScreenImageView.getLayoutParams());
marginParams.setMargins(0, 0, (int) UIUtil.getRadialButtonMainMargin(this), 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
backToMainScreenImageView.setLayoutParams(layoutParams);
Try something like this :
paramsd2.setMargin(500, 500, 0, 0);
btn.setLayoutParams(paramsd2);

android frame by frame animation - differences with these two codes

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

Keep view above animation of another view

I have an Activity with a main screen that simply has a title bar (among other things). I have another TextView below the title bar that I have animate from top to bottom into place (it starts out as View.GONE, then I animate it visible and into place after an event from the user).
This works fine and dandy, except the TextView below the title bar animates above the title bar into place. I want it to seem as if the TextView came from underneath the title bar. Both views are in a LinearLayout, so I'm not able to address the z-order like I would in a FrameLayout. Any suggestions?
I eventually solved this by encompassing the textview inside of a LinearLayout, then setting a LayoutAnimationController to the LinearLayout. This causes all the childs to be animated with respect to the parent container, which made the drop-down only to be rendered within the LinearLayout (which worked perfectly). Here is the code I used for the Animation Controller and ListView:
private void addDeleteDropAnimation() {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(150);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(300);
set.addAnimation(animation);
controllerDel = new LayoutAnimationController(set, 0.5f);
vw_delLinearLayout.setLayoutAnimation(controllerDel);
}
Handler handler = new Handler();
new Thread(){
public void run(){
TextView tv = (TextView)findViewById(R.id.xx);
LayoutParams params = (LayoutParams)tv.getLayoutParams();
handler.post(new Runnable(){
public void run(){
tv.setVisible(View.Visible);
}
}
int height = params.height;
for(int i = 0; i < height + 1; i++) {
params.topMargin = i - height;
handler.post(new Runnable(){
public void run(){
tv.requestLayout();
}
}
try{
Thread.sleep(5);
}catch(InterruptedException e){
}
}
}
}.start();
you can try this.

Categories

Resources