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) {
}
});
}
}
添加动画监听
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) {
}
});
}
}
相关文章推荐
- Android中的ViewRootImpl类源码解析
- Android SdCard写入权限
- Android初级教程理论知识(第九章多媒体编程)
- Android初级教程理论知识(第九章多媒体编程)
- Android FragmentStatePagerAdapter的写法
- Android PDF截取封面:PDF缩略图预览效果
- Android控件(1)--ActionBarDrawerToggle、DrawerLayout、ActionBar 使用说明
- android开发教程之listview使用方法
- android studio安卓项目出现Error: Default Activity Not Found错误无法编译的解决方案
- Android虚线
- Android日志与DDMS
- 【Android】PropertyAnimation属性动画
- android 使对话框同步显示
- Java/Android中的PBKDF2加密认证
- Java/Android中SHA1和SHA256加密
- Android开发学习之路-Volley源码解析
- Java/Android中的MD5加密加盐方法
- Android基于Google Zxing实现二维码/条形码扫描、生成二维码/条形码
- android-AccessibilityManager
- Android自定义ActionMode背景色