I tried to start an AlphaAnimation for a ImageView with this code:
ImageView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prueba);
view=(ImageView)findViewById(R.id.icon);
view.setAlpha(0x00);
view.setVisibility(View.VISIBLE);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("Down v", "x:" + e.getX() + " y:" + e.getY());
view.startAnimation(new AlphaAnimation(0x22, 0xFF));
// break;
case MotionEvent.ACTION_MOVE:
Log.i("Move", "x:" + e.getX() + " y:" + e.getY());
break;
case MotionEvent.ACTION_UP:
Log.i("Up", "x:" + e.getX() + " y:" + e.getY());
view.setAlpha(0x00);
break;
case MotionEvent.ACTION_OUTSIDE:
Log.i("Out", "x:" + e.getX() + " y:" + e.getY());
break;
default:
Log.i("None", "nada");
}
return false;
}
});
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Click", "es un click");
}
});
}
this code should be make the ImageView appear when is touched, but nothing happend.
Any solution?
This is the odd thing about the Android animations.
You’re using
setAlpha()to set the alpha of the view to 0. At the same time you’re usingAlphaAnimationto set the alpha value to 1. This seems like it would work. The problem is they work on two different frameworks.setAlpha()was introduced with Honeycomb in conjunction with ObjectAnimators whileAlphaAnimationhas been around since Cupcake (possibly API 1 too, but I forget). ObjectAnimators change the actual property of the View while the AlphaAnimation simply changes how the view is drawn (confused yet? :P).Anyway,
AlphaAnimationdoesn’t effect the alpha property of the view like you think it would. It’s animating the View from 0 to 1, but the property is still 0 thus it’s still invisible.You have two options here.
Keep
setAlpha()the way it is and useObjectAnimatorslike so:This is the easiest and preferred method. It only works in API levels 11+ though. If that’s desired, then go for it.
The other option is to remove
setAlpha()and pre-animate the view to0with a duration of0. CallsetFillAfter(true)on the animation before you start it. This will make sure it stays invisible (it may be true by default, but I don’t remember).I hope that made sense.