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

来源:互联网 发布:如何设置访客网络 编辑:程序博客网 时间:2024/06/04 21:19
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.csdn.net/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);        }    }}