Wednesday, October 30, 2013

Setting alpha (transparency), and flashing button animation example.

Setting transparency for a view in android 2.x version is not as easy as calling 1 method, but it still is pretty a trivial task:



final AlphaAnimation alphaAnimation = new AlphaAnimation(0.5F, 0.5F); // Creating new alpha animation, that makes the view half transparent

alphaAnimation.setDuration(0); // Make animation to perform instantly
alphaAnimation.setFillAfter(true); // Persist changes after the animation ends
yourLayout.startAnimation(alphaAnimation); // Call it for any view


As easy as that :)


final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(250); // duration 250 milliseconds
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); // Your button
btn.startAnimation(animation);
btn.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(final View view) {
     // Some of your code on click
     view.clearAnimation()
  }
});


It's not limited for button only, you can se virtually for any view.