<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_a" android:src="@drawable/stefanie01" android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="match_parent"/> <ImageView android:id="@+id/iv_b" android:src="@drawable/stefanie02" android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout>
1 package com.lyp.www.a2d; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.animation.Animation; 7 import android.view.animation.ScaleAnimation; 8 import android.widget.FrameLayout; 9 import android.widget.ImageView; 10 11 public class MainActivity extends AppCompatActivity { 12 13 private FrameLayout flRoot; 14 private ImageView ivA; 15 private ImageView ivB; 16 17 boolean flag = true; 18 19 private ScaleAnimation anim1= 20 new ScaleAnimation(0,1,1,1, Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f); 21 private ScaleAnimation anim2= 22 new ScaleAnimation(1,0,1,1, Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f); 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 initView(); 30 flRoot.setOnClickListener(new View.OnClickListener() { 31 @Override 32 public void onClick(View v) { 33 if (flag) { 34 ivA.startAnimation(anim2); 35 flag=false; 36 }else { 37 ivB.startAnimation(anim2); 38 flag=true; 39 } 40 } 41 }); 42 43 } 44 45 private void showImageA() { 46 ivB.setVisibility(View.INVISIBLE); 47 ivA.setVisibility(View.VISIBLE); 48 } 49 50 private void showImageB() { 51 ivA.setVisibility(View.INVISIBLE); 52 ivB.setVisibility(View.VISIBLE); 53 } 54 55 private void initView() { 56 flRoot = (FrameLayout) findViewById(R.id.root); 57 ivA = (ImageView) findViewById(R.id.iv_a); 58 ivB = (ImageView) findViewById(R.id.iv_b); 59 anim1.setDuration(600); 60 anim2.setDuration(600); 61 62 anim2.setAnimationListener(new Animation.AnimationListener() { 63 @Override 64 public void onAnimationStart(Animation animation) { 65 66 } 67 68 @Override 69 public void onAnimationEnd(Animation animation) { 70 if (flag){ 71 ivB.setAnimation(null); 72 showImageA(); 73 ivA.startAnimation(anim1); 74 }else { 75 ivA.setAnimation(null); 76 showImageB(); 77 ivB.startAnimation(anim1); 78 } 79 } 80 81 @Override 82 public void onAnimationRepeat(Animation animation) { 83 84 } 85 }); 86 } 87 }
时间: 2024-10-08 13:27:41