Android 2D翻转demo

简单实现由一张图片翻转到另一张图片的效果。

效果图:

技术分享


关键代码

public class MainActivity extends Activity {

	private ImageView imageA;
	private ImageView imageB;

	private ScaleAnimation sato0 = new ScaleAnimation(1, 0, 1, 1,
			Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT,
			0.5f);
	private ScaleAnimation sato1 = new ScaleAnimation(0, 1, 1, 1,
			Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT,
			0.5f);
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		findViewById(R.id.root).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (imageA.getVisibility() == View.VISIBLE) {
					imageA.startAnimation(sato0);
				}else{
					imageB.startAnimation(sato0);
				}
			}
		});
		
	}
	
	private void shwoImageA(){
		imageA.setVisibility(View.VISIBLE);
		imageB.setVisibility(View.INVISIBLE);
	}
	
	private void showImageB(){
		imageA.setVisibility(View.INVISIBLE);
		imageB.setVisibility(View.VISIBLE);
	}
	
	private void initView(){
		imageA = (ImageView) findViewById(R.id.ivA);
		imageB = (ImageView) findViewById(R.id.ivB);
		shwoImageA();
		sato0.setDuration(500);
		sato1.setDuration(500);
		
		sato0.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				if (imageA.getVisibility() == View.VISIBLE) {
					imageA.setAnimation(null);
					showImageB();
					imageB.startAnimation(sato1);
				}else{
					imageB.setAnimation(null);
					shwoImageA();
					imageA.startAnimation(sato1);
				}
			}
		});
	}
}


Demo apk下载地址  http://pan.baidu.com/s/1sjykO4t

Demo 源码下载地址 http://pan.baidu.com/s/1dD3YWLz


欢迎大家关注Android开发者社区微信公众号,官方会不定期给大家带来一些android开发优质试用的demo或框架,提高大家的开发效率,为大家提供保证优质的android demo源码。

扫描一下二维码关注:

技术分享



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。