首页 > 代码库 > android圆角矩形背景

android圆角矩形背景

Android平台
使用方便
StateRoundRectDrawable mRoundRectDradable = new StateRoundRectDrawable(int normalCorlor, int pressColor);
mRoundRectDradable.setBottomLeftRadius(0);
mRoundRectDradable.setBottomRightRadius(0); .............

xxxxx.setBackgroundDrawable(mRoundRectDradable);

[1].[代码] [Java]代码 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.ColorFilter;
importandroid.graphics.Paint;
importandroid.graphics.Rect;
importandroid.graphics.drawable.Drawable;
importandroid.graphics.drawable.shapes.RoundRectShape;
importandroid.view.MotionEvent;
 
publicclass RoundRectDradable extendsDrawable{
    privatestatic final float DEFAULT_RADIUS = 6.f;
    privatePaint mPaint = newPaint();
    privateRoundRectShape mShape;
    privatefloat[] mOuter;
    privateint mColor;
    privateint mPressColor;
    privatefloat mTopLeftRadius = DEFAULT_RADIUS;
    privatefloat mTopRightRadius = DEFAULT_RADIUS;
    privatefloat mBottomLeftRadius = DEFAULT_RADIUS;
    privatefloat mBottomRightRadius = DEFAULT_RADIUS;
    publicRoundRectDradable() {
        mColor = Color.WHITE;
        mPressColor = Color.WHITE;
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
    }
     
    publicfloat getTopLeftRadius() {
        returnmTopLeftRadius;
    }
 
    publicvoid setTopLeftRadius(floattopLeftRadius) {
        this.mTopLeftRadius = topLeftRadius;
    }
 
    publicfloat getTopRightRadius() {
        returnmTopRightRadius;
    }
 
    publicvoid setTopRightRadius(floattopRightRadius) {
        this.mTopRightRadius = topRightRadius;
    }
 
    publicfloat getBottomLeftRadius() {
        returnmBottomLeftRadius;
    }
 
    publicvoid setBottomLeftRadius(floatbottomLeftRadius) {
        this.mBottomLeftRadius = bottomLeftRadius;
    }
 
    publicfloat getBottomRightRadius() {
        returnmBottomRightRadius;
    }
 
    publicvoid setBottomRightRadius(floatbottomRightRadius) {
        this.mBottomRightRadius = bottomRightRadius;
    }
     
    publicint getPressColor() {
        returnmPressColor;
    }
 
    publicvoid setPressColor(intpressColor) {
        this.mPressColor = pressColor;
    }
 
    @Override
    protectedvoid onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        refreshShape();
        mShape.resize(bounds.right - bounds.left, bounds.bottom - bounds.top);
    }
     
    privatevoid refreshShape(){
        mOuter = newfloat[]{mTopLeftRadius, mTopLeftRadius
                , mTopRightRadius, mTopRightRadius
                , mBottomLeftRadius, mBottomLeftRadius
                , mBottomRightRadius, mBottomLeftRadius};
        mShape = newRoundRectShape(mOuter, null,null);
    }
     
    publicvoid setColor(intcolor){
        mColor = color;
        mPaint.setColor(color);
    }
     
    @Override
    publicvoid draw(Canvas canvas) {
        mShape.draw(canvas, mPaint);
    }
 
    @Override
    publicvoid setAlpha(intalpha) {
        mPaint.setAlpha(alpha);
    }
     
    @Override
    publicvoid setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }
 
    @Override
    publicint getOpacity() {
        returnmPaint.getAlpha();
    }
}

[2].[代码] [Java]代码 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
importandroid.graphics.Rect;
importandroid.graphics.drawable.StateListDrawable;
 
publicclass StateRoundRectDrawable extendsStateListDrawable{
    privatestatic final float DEFAULT_RADIUS = 6.f;
    privatefloat mTopLeftRadius = DEFAULT_RADIUS;
    privatefloat mTopRightRadius = DEFAULT_RADIUS;
    privatefloat mBottomLeftRadius = DEFAULT_RADIUS;
    privatefloat mBottomRightRadius = DEFAULT_RADIUS;
    privateint mNormalColor;
    privateint mPressedColor;
    privateRoundRectDradable mNormalDradable;
    privateRoundRectDradable mPressedDradable;
    publicStateRoundRectDrawable(intnormalCorlor, intpressColor) {
        this.mNormalColor = normalCorlor;
        this.mPressedColor = pressColor;
    }
     
    @Override
    protectedvoid onBoundsChange(Rect bounds) {
        if(mNormalDradable == null){
            mNormalDradable = newRoundRectDradable();
            mNormalDradable.setTopLeftRadius(mTopLeftRadius);
            mNormalDradable.setTopRightRadius(mTopRightRadius);
            mNormalDradable.setBottomLeftRadius(mBottomLeftRadius);
            mNormalDradable.setBottomRightRadius(mBottomRightRadius);
            mNormalDradable.setColor(mNormalColor);
            mNormalDradable.onBoundsChange(bounds);
        }
        if(mPressedDradable == null){
            mPressedDradable = newRoundRectDradable();
            mPressedDradable.setTopLeftRadius(mTopLeftRadius);
            mPressedDradable.setTopRightRadius(mTopRightRadius);
            mPressedDradable.setBottomLeftRadius(mBottomLeftRadius);
            mPressedDradable.setBottomRightRadius(mBottomRightRadius);
            mPressedDradable.setColor(mPressedColor);
            mPressedDradable.onBoundsChange(bounds);
        }
        this.addState(newint[]{-android.R.attr.state_pressed}, mNormalDradable);
        this.addState(newint[]{android.R.attr.state_pressed}, mPressedDradable);
    }
     
    publicfloat getTopLeftRadius() {
        returnmTopLeftRadius;
    }
 
    publicvoid setTopLeftRadius(floattopLeftRadius) {
        this.mTopLeftRadius = topLeftRadius;
    }
 
    publicfloat getTopRightRadius() {
        returnmTopRightRadius;
    }
 
    publicvoid setTopRightRadius(floattopRightRadius) {
        this.mTopRightRadius = topRightRadius;
    }
 
    publicfloat getBottomLeftRadius() {
        returnmBottomLeftRadius;
    }
 
    publicvoid setBottomLeftRadius(floatbottomLeftRadius) {
        this.mBottomLeftRadius = bottomLeftRadius;
    }
 
    publicfloat getBottomRightRadius() {
        returnmBottomRightRadius;
    }
 
    publicvoid setBottomRightRadius(floatbottomRightRadius) {
        this.mBottomRightRadius = bottomRightRadius;
    }
 
    publicint getNormalColor() {
        returnmNormalColor;
    }
 
    publicvoid setNormalColor(intnormalColor) {
        this.mNormalColor = normalColor;
    }
 
    publicint getPressedColor() {
        returnmPressedColor;
    }
 
    publicvoid setPressedColor(intpressedColor) {
        this.mPressedColor = pressedColor;
    }
     
}

android圆角矩形背景