使用android.view.animation.ScaleAnimation实现图片的前后翻转效果。
主要使用到的类还有android.view.animation.Animation.AnimationListener,android.view.animation.Animation,android.widget.ImageView等;
最主要的两个动画定义:
// 展开动画
ScaleAnimation anim1 = new ScaleAnimation(0, 1, 1, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
// 收缩动画
ScaleAnimation anim2 = new ScaleAnimation(1, 0, 1, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
// 为动画添加的事件监听器
anim2.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if(img2.getVisibility() == View.VISIBLE){
img2.setAnimation(null);
showImag1();
img1.startAnimation(anim1);
}else{
img1.setAnimation(null);
showImag2();
img2.startAnimation(anim1);
}
}
});
// 设置图片显示的两个函数
private void showImag1(){
img1.setVisibility(View.VISIBLE);
img2.setVisibility(View.INVISIBLE);
}
private void showImag2(){
img1.setVisibility(View.INVISIBLE);
img2.setVisibility(View.VISIBLE);
}
图片用的是个人图片,就不上图了。