1. 程式人生 > >view同時更新backgroundcolor和corner

view同時更新backgroundcolor和corner

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