直接上代码 MainActivity
1 public class MainActivity extends AppCompatActivity { 2 3 private ImageView ivHart; //图片信息 4 AlphaAnimation alphaAnimation = null; //心跳动画 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 12 ivHart = (ImageView) findViewById(R.id.ivHart); 13 14 shadeAnim(ivHart); 15 } 16 17 @Override 18 protected void onResume() { 19 super.onResume(); 20 if (alphaAnimation != null) { 21 alphaAnimation.start(); 22 } 23 } 24 25 @Override 26 protected void onPause() { 27 super.onPause(); 28 if (alphaAnimation != null) { 29 alphaAnimation.cancel(); 30 } 31 } 32 33 /** 34 * 心跳渐变动画 35 * 36 * @param view 执行该动画的view对象 37 */ 38 private void shadeAnim(View view) { 39 alphaAnimation = new AlphaAnimation(0.1f, 1.0f); 40 alphaAnimation.setDuration(2000); 41 alphaAnimation.setRepeatCount(-1); 42 alphaAnimation.setRepeatMode(Animation.REVERSE); 43 alphaAnimation.start(); 44 view.setAnimation(alphaAnimation); 45 }
布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:background="@drawable/yy" 9 tools:context="com.hanbao.myapplication.MainActivity"> 10 11 12 13 <ImageView 14 android:id="@+id/ivHart" 15 android:layout_width="wrap_content" 16 android:layout_height="match_parent" 17 android:scaleType="centerCrop" 18 android:src="@drawable/x"/> 19 20 21 </RelativeLayout>