您的位置:首页 > 移动开发 > Android开发

Android 2D翻转

2016-06-22 22:39 369 查看
创建动画

添加动画监听

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.card2d.MainActivity">

<ImageView
android:id="@+id/ivA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image_a"/>

<ImageView
android:id="@+id/ivB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image_b"/>

</FrameLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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 View.OnClickListener() {
@Override
public void onClick(View v) {
if (imageA.getVisibility() == View.VISIBLE) {
imageA.startAnimation(sato0);
} else {
imageB.startAnimation(sato0);
}
}
});
}

private void showImageA() {
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);
showImageA();
sato0.setDuration(500);
sato1.setDuration(500);

sato0.setAnimationListener(new Animation.AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
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 animation) {

}

});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: