Android 心跳动画

时间:2023-03-09 06:23:59
Android  心跳动画

Android  心跳动画

直接上代码  MainActivity

 public class MainActivity extends AppCompatActivity {

     private ImageView ivHart;  //图片信息
AlphaAnimation alphaAnimation = null; //心跳动画 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ivHart = (ImageView) findViewById(R.id.ivHart); shadeAnim(ivHart);
} @Override
protected void onResume() {
super.onResume();
if (alphaAnimation != null) {
alphaAnimation.start();
}
} @Override
protected void onPause() {
super.onPause();
if (alphaAnimation != null) {
alphaAnimation.cancel();
}
} /**
* 心跳渐变动画
*
* @param view 执行该动画的view对象
*/
private void shadeAnim(View view) {
alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(2000);
alphaAnimation.setRepeatCount(-1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
alphaAnimation.start();
view.setAnimation(alphaAnimation);
}

布局文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/yy"
tools:context="com.hanbao.myapplication.MainActivity"> <ImageView
android:id="@+id/ivHart"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/x"/> </RelativeLayout>