Android动画收藏

Android 收藏动画效果可以有很多种实现方式,这里提供一种比较简单的做法,通过使用属性动画和自定义插值器实现。 首先在布局文件中添加一个 ImageView 控件作为收藏按钮,如下: ```xml <ImageView android:id="@+id/img_collect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_collect"/> ``` 然后在代码中为 ImageView 添加点击事件,在点击事件中添加动画效果,如下: ```java // 获取布局文件中的控件 ImageView imgCollect = findViewById(R.id.img_collect); // 为 ImageView 添加点击事件 imgCollect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建一个属性动画,实现旋转和缩放效果 ObjectAnimator anim1 = ObjectAnimator.ofFloat(imgCollect, "rotation", 0, 45); anim1.setDuration(300); anim1.setInterpolator(new MyInterpolator()); ObjectAnimator anim2 = ObjectAnimator.ofFloat(imgCollect, "scaleX", 1, 1.2f, 1); anim2.setDuration(300); anim2.setInterpolator(new MyInterpolator()); ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgCollect, "scaleY", 1, 1.2f, 1); anim3.setDuration(300); anim3.setInterpolator(new MyInterpolator()); // 创建一个 AnimatorSet,按顺序播放动画 AnimatorSet animSet = new AnimatorSet(); animSet.playSequentially(anim1, anim2, anim3); animSet.start(); } }); ``` 其中,MyInterpolator 是自定义的一个插值器类,用于控制动画的变化速度,可以根据个人喜好进行调整。这里是一个简单的实现: ```java public class MyInterpolator implements Interpolator { @Override public float getInterpolation(float input) { return (float) Math.pow(input, 2); } } ``` 这样就可以实现一个简单的收藏按钮动画效果了。需要注意的是,代码中的图片资源需要自行添加,并根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值