2D翻转
16lz
2021-01-24
看到android实现的一个效果视频,就学习一下:
一、2D翻转:
呵,我来说就是一张图片先收缩,另一张图片在展开.
二、实现效果:
三、实现源码:
Main:
package com.example.card2d;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.ScaleAnimation;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {private ImageView imgeA;private ImageView imgeB;//创建动画private ScaleAnimation sato1 =new ScaleAnimation(1, 0, 1, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);private ScaleAnimation sato2 =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 arg0) {// TODO Auto-generated method stubif(imgeA.getVisibility() == View.VISIBLE){imgeA.startAnimation(sato1);}else{imgeB.startAnimation(sato1);}}}); } private void showImgA(){ imgeA.setVisibility(View.VISIBLE); imgeB.setVisibility(View.INVISIBLE); } private void showImgB(){ imgeA.setVisibility(View.INVISIBLE); imgeB.setVisibility(View.VISIBLE); } private void initView(){ imgeA =(ImageView) findViewById(R.id.viA); imgeB =(ImageView) findViewById(R.id.viB); showImgA(); sato1.setDuration(500); sato2.setDuration(500); sato1.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation arg0) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animation arg0) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation arg0) {// TODO Auto-generated method stubif(imgeA.getVisibility() == View.VISIBLE){imgeA.setAnimation(null);showImgB();imgeB.startAnimation(sato2);}else{imgeB.setAnimation(null);showImgA();imgeA.startAnimation(sato2);}}}); } }
布局文件:
<FrameLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viA" android:src="@drawable/c1" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viB" android:src="@drawable/c2" /></FrameLayout>
四、分析:
源码很简单,看看就懂了.
执行过程:
当鼠标点击的时候,imgeA执行动画1,imgeA进行收缩,当动画1结束时,执行showImgB(),使得imgeA隐藏,imgeB显示,而imgeB的显示效果,就要imgeB执行动画2.
当鼠标第二次点击的时候,imgeB执行动画1,进行图片收缩,当动画结束时,执行showImgA(),使得imgeA显示,imgeB隐藏,而imgeA的显示效果,就要imgeA执行动画2.
更多相关文章
- Android拍照,照片会自己旋转
- (4.2.0)GitHub开源项目收集
- Android:popupwindow实现底部弹窗实例
- 把这份关于Android(安卓)Binder原理一系列笔记研究完,进大厂是个
- android手势实现图片的拖动和缩放效果
- Flutter事件分发源码剖析
- Android应用程序进程启动源码解析
- Android(安卓)UI组件框架AndroidMaterialDesign详解
- [Android开发实战]Android手势密码(支付宝手势密码)实现(支持2.x)