安卓项目--2D图片翻转
2016-06-26 11:34
344 查看
实行思路:插入两张大小一样的图片,刚开始的时候显示第一张图片,但鼠标点击的时候,第一张图片使用ScaleAnimation类进行缩小动画,缩小动画完成后,第二张图片显示并使用ScaleAnimation类进行放大动画。
XML中插入两张图片,用来翻转。
public class MainActivity extends Activity { private ImageView imageA; private ImageView imageB; //使用ScaleAnimation类 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() { public void onClick(View v) { if(imageA.getVisibility() == View.VISIBLE) { imageA.startAnimation(sato0); } else { imageB.startAnimation(sato0); } } }); } private void showImageA() { //A图显示,B图不显示 imageA.setVisibility(View.VISIBLE); imageB.setVisibility(View.INVISIBLE); } private void showImageB() { //B图不显示,A图显示 imageA.setVisibility(View.INVISIBLE); imageB.setVisibility(View.VISIBLE); } private void initView() { //先找到两个对象 imageA = (ImageView) findViewById(R.id.ivA); imageB = (ImageView) findViewById(R.id.ivB); // showImageA(); //持续时间 sato0.setDuration(500); sato1.setDuration(500); //当第一个动画完成,执行第二个 sato0.setAnimationListener(new AnimationListener() { @Override public void onAnimationEnd(Animation arg0) { // TODO Auto-generated method stub //判断图A是否显示 if(imageA.getVisibility() == View.VISIBLE) { //把第一个动画动作清空 imageA.setAnimation(null); //显示第二个图 showImageB(); //加载第二张图的动画 imageB.startAnimation(sato1); } else { //把第二个动画动作清空 imageB.setAnimation(null); //显示第一个图 showImageA(); //加载第一张图的动画 imageA.startAnimation(sato1); } } @Override public void onAnimationRepeat(Animation arg0) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation arg0) { // TODO Auto-generated method stub } }); }
XML中插入两张图片,用来翻转。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root" tools:context=".MainActivity" > <ImageView android:id="@+id/ivA" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/b" ></ImageView> <ImageView android:id="@+id/ivB" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/c" ></ImageView> </RelativeLayout>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories