因为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();