实现点赞和收藏效果

来源:互联网 发布:大拿韩代 淘宝 编辑:程序博客网 时间:2024/05/29 10:10

实现点赞和收藏的效果

github地址:https://github.com/Taonce/support 点击打开链接


效果:

布局代码:
<ImageView    android:id="@+id/ivCollect"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:scaleType="fitCenter"    android:layout_gravity="center"    android:layout_margin="20dp"    android:src="@mipmap/blackheart"    android:background="?android:attr/selectableItemBackgroundBorderless"    />

动画效果:
public class ScaleAnimatorUtils {    //设置缩放动画,淡入和淡出    public static void setScale(View view) {        //创建一个AnimatorSet对象        AnimatorSet set = new AnimatorSet();        //设置动画的时间和效果        ObjectAnimator animator_x = ObjectAnimator.ofFloat(view, "scaleX", 1.5f, 1.2f, 1f, 0.5f, 0.7f, 1f);        ObjectAnimator animator_y = ObjectAnimator.ofFloat(view, "scaleY", 1.5f, 1.2f, 1f, 0.5f, 0.7f, 1f);        set.play(animator_x).with(animator_y);        //设置他的持续时间        set.setDuration(500);        //启动动画        set.start();    }}

点击效果:
@Overridepublic void onClick(View view) {    switch (view.getId()) {        case R.id.ivCollect:            if (ivCollect.isSelected() == false) {                ivCollect.setImageResource(R.mipmap.redheart);                ivCollect.setSelected(true);                ScaleAnimatorUtils.setScale(ivCollect);                Toast.makeText(MainActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();            }            else {                ivCollect.setImageResource(R.mipmap.blackheart);                ivCollect.setSelected(false);                ScaleAnimatorUtils.setScale(ivCollect);                Toast.makeText(MainActivity.this,"取消收藏",Toast.LENGTH_SHORT).show();            }            break;    }}

这里我们主要就是用到了ObjectAnimotor对象,调用他的ofFloat (Object target, String propertyName, float... values)方法,设置目标View和动画参数。