Android 心跳动画

本文介绍了一个简单的Android应用程序,用于实现一个不断变化的心跳效果动画。通过使用AlphaAnimation类创建透明度渐变动画,并在ImageView上播放,实现了从半透明到完全可见的循环渐变效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码  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>

 

转载于:https://www.cnblogs.com/monkey0928/p/8892801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值