自定义不同颜色点赞图片被点击之后动画缩放效果

本文介绍了一种自定义 PeriscopeLayout 的实现方法,该布局可在 Android 应用中模拟点赞时心形图案的动画效果。通过继承 RelativeLayout 并在 Java 代码中创建视图,实现了动态添加不同颜色的心形 ImageView,并运用 AnimatorSet 控制动画,同时介绍了 onMeasure 方法的用途。

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

public class PeriscopeLayout extends RelativeLayout {
    private Drawable[] drawables;
    private int imagHeight;//赞的高度
    private int imageWidth;
    LayoutParams layoutParams;
    private Random random = new Random();// //用于获取随机心的随机数

    /**
     * 是在java代码创建视图的时候被调用,如果是从xml填充的视图,就不会调用这个
     */
    public PeriscopeLayout(Context context) {
        super(context);
        init();
    }

    /**
     * 这个是在xml创建但是没有指定style的时候被调用
     */
    public PeriscopeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 这个是在xml创建但是 有指定style的时候被调用
     */
    public PeriscopeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //初始化显示的图片,暂时使用3 种图片
        drawables = new Drawable[3];
        //getResources().getDrawable 过期的替代方法, ContextCompat.getDrawable(getContext(),R.drawable.heart3);
        // Drawable red = getResources().getDrawable(R.drawable.heart3);
        Drawable red = ContextCompat.getDrawable(getContext(), R.drawable.red);
        Drawable green = ContextCompat.getDrawable(getContext(), R.drawable.green);
        Drawable blue = ContextCompat.getDrawable(getContext(), R.drawable.blue);
        drawables[0] = red;
        drawables[1] = green;
        drawables[2] = blue;
        //获取图的宽高 用于后面的计算
        //注意 我这里3张图片的大小都是一样的,所以我只取了一个
        imagHeight = red.getIntrinsicHeight();
        imageWidth = red.getIntrinsicWidth();
        //定义心型图片出现的位置 水平垂直居中
        layoutParams = new LayoutParams(imagHeight, imageWidth);
        layoutParams.addRule(CENTER_HORIZONTAL, TRUE);
        layoutParams.addRule(CENTER_VERTICAL, TRUE);
    }
    /**
     * http://blog.youkuaiyun.com/pi9nc/article/details/18764863
     * 自定义布局 onMeasure 的作用
     * 获取控件的实际高度
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        imagHeight = getMeasuredHeight();
        imageWidth = getMeasuredWidth();
    }
   //AnimatorSet 提供组织动画的结构,使它们能相关联得运行,用于控制一组动画的执行
    private AnimatorSet getAnimator(View target) {
        //设置动画作用的元素、作用的属性、动画开始、结束、以及中间的任意个属性值
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(target, View.SCALE_X, 1f, 1.5f);
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(target, View.SCALE_Y, 1f, 1.5f);
        AnimatorSet animatorSet = new AnimatorSet();
        //duration 表示动画执行的时间
        animatorSet.setDuration(500);
        //这个时间插值类,其主要使用在动画中,其作用主要是控制目标变量的变化值进行对应的变化
        animatorSet.setInterpolator(new LinearInterpolator());
       /**
         * play(Animator anim):添加一个动画,并返回AnimatorSet.Builder
         *playSequentially(List items):添加一组动画,播放顺序为一一播放
         *playSequentially(Animator… items):添加一组动画,播放顺序为一一播放
         *playTogether(Collection items):添加一组动画,播放顺序为一起播放
         * playTogether(Animator… items):添加一组动画,播放顺序为一起播放
        */
        animatorSet.playTogether(objectAnimator, objectAnimator1);
        // 动画设定目标
        animatorSet.setTarget(target);
        return animatorSet;
    }
    /**
     * 提供外部实现点击效果,只有缩放和变淡的效果
     */
    public void addFavor() {
        ImageView imageView = new ImageView(getContext());
        //随机心型颜色
        imageView.setImageDrawable(drawables[random.nextInt(3)]);
        imageView.setLayoutParams(layoutParams);

        addView(imageView);

        Animator set = getAnimator(imageView);
        set.addListener(new AnimEndListener(imageView));
        set.start();
    }

    private class AnimEndListener extends AnimatorListenerAdapter {
        private View mtarget;

        public AnimEndListener(View target) {
            this.mtarget = target;
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            //因为不停的add 导致子view数量只增不减,所以在view动画结束后remove掉
            removeView(mtarget);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值