看到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 stub
if(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() {
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
if(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.