view同时更新backgroundcolor和corner

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lihenair/article/details/79065582

因为corner在view里不是一个属性,不像iOS那样可以直接修改,所以需要适当的修改一笑已经有的实现。
Step 1

// 自定义属性,表示背景中的corner
public void setRadius(int radius) {
    this.radius = radius;
}

@Override
public void setBackgroundColor(int color) {
//        super.setBackgroundColor(color);
    //整个核心是这里
    GradientDrawable drawable = new GradientDrawable();
    drawable.setColor(color);
    drawable.setCornerRadius(radius);
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setStroke(2, Color.BLACK);
    setBackground(drawable);
}

Step 2

// 背景色设置动画
ObjectAnimator bg = ObjectAnimator.ofArgb(bgView, "BackgroundColor", 0xffff00ff, 0xffffff00, 0xffff00ff);
bg.setDuration(3000);
bg.setEvaluator(new ArgbEvaluator());
// 自定义view半径动画
ObjectAnimator radius = ObjectAnimator.ofInt(bgView, "radius", 100, 10);
radius.setDuration(8000);
// 动画集合
AnimatorSet set = new AnimatorSet();
set.playTogether(radius, bg);
set.setDuration(5000);
set.start();

猜你喜欢

转载自blog.csdn.net/lihenair/article/details/79065582