码迷,mamicode.com
首页 > 移动开发 > 详细

android圆角矩形背景

时间:2015-01-13 14:21:11      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

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
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.MotionEvent;
 
public class RoundRectDradable extends Drawable{
    private static final float DEFAULT_RADIUS = 6.f;
    private Paint mPaint = new Paint();
    private RoundRectShape mShape;
    private float[] mOuter;
    private int mColor;
    private int mPressColor;
    private float mTopLeftRadius = DEFAULT_RADIUS;
    private float mTopRightRadius = DEFAULT_RADIUS;
    private float mBottomLeftRadius = DEFAULT_RADIUS;
    private float mBottomRightRadius = DEFAULT_RADIUS;
    public RoundRectDradable() {
        mColor = Color.WHITE;
        mPressColor = Color.WHITE;
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
    }
     
    public float getTopLeftRadius() {
        return mTopLeftRadius;
    }
 
    public void setTopLeftRadius(float topLeftRadius) {
        this.mTopLeftRadius = topLeftRadius;
    }
 
    public float getTopRightRadius() {
        return mTopRightRadius;
    }
 
    public void setTopRightRadius(float topRightRadius) {
        this.mTopRightRadius = topRightRadius;
    }
 
    public float getBottomLeftRadius() {
        return mBottomLeftRadius;
    }
 
    public void setBottomLeftRadius(float bottomLeftRadius) {
        this.mBottomLeftRadius = bottomLeftRadius;
    }
 
    public float getBottomRightRadius() {
        return mBottomRightRadius;
    }
 
    public void setBottomRightRadius(float bottomRightRadius) {
        this.mBottomRightRadius = bottomRightRadius;
    }
     
    public int getPressColor() {
        return mPressColor;
    }
 
    public void setPressColor(int pressColor) {
        this.mPressColor = pressColor;
    }
 
    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        refreshShape();
        mShape.resize(bounds.right - bounds.left, bounds.bottom - bounds.top);
    }
     
    private void refreshShape(){
        mOuter = new float[]{mTopLeftRadius, mTopLeftRadius
                , mTopRightRadius, mTopRightRadius
                , mBottomLeftRadius, mBottomLeftRadius
                , mBottomRightRadius, mBottomLeftRadius};
        mShape = new RoundRectShape(mOuter, null, null);
    }
     
    public void setColor(int color){
        mColor = color;
        mPaint.setColor(color);
    }
     
    @Override
    public void draw(Canvas canvas) {
        mShape.draw(canvas, mPaint);
    }
 
    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }
     
    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }
 
    @Override
    public int getOpacity() {
        return mPaint.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
import android.graphics.Rect;
import android.graphics.drawable.StateListDrawable;
 
public class StateRoundRectDrawable extends StateListDrawable{
    private static final float DEFAULT_RADIUS = 6.f;
    private float mTopLeftRadius = DEFAULT_RADIUS;
    private float mTopRightRadius = DEFAULT_RADIUS;
    private float mBottomLeftRadius = DEFAULT_RADIUS;
    private float mBottomRightRadius = DEFAULT_RADIUS;
    private int mNormalColor;
    private int mPressedColor;
    private RoundRectDradable mNormalDradable;
    private RoundRectDradable mPressedDradable;
    public StateRoundRectDrawable(int normalCorlor, int pressColor) {
        this.mNormalColor = normalCorlor;
        this.mPressedColor = pressColor;
    }
     
    @Override
    protected void onBoundsChange(Rect bounds) {
        if(mNormalDradable == null){
            mNormalDradable = new RoundRectDradable();
            mNormalDradable.setTopLeftRadius(mTopLeftRadius);
            mNormalDradable.setTopRightRadius(mTopRightRadius);
            mNormalDradable.setBottomLeftRadius(mBottomLeftRadius);
            mNormalDradable.setBottomRightRadius(mBottomRightRadius);
            mNormalDradable.setColor(mNormalColor);
            mNormalDradable.onBoundsChange(bounds);
        }
        if(mPressedDradable == null){
            mPressedDradable = new RoundRectDradable();
            mPressedDradable.setTopLeftRadius(mTopLeftRadius);
            mPressedDradable.setTopRightRadius(mTopRightRadius);
            mPressedDradable.setBottomLeftRadius(mBottomLeftRadius);
            mPressedDradable.setBottomRightRadius(mBottomRightRadius);
            mPressedDradable.setColor(mPressedColor);
            mPressedDradable.onBoundsChange(bounds);
        }
        this.addState(new int[]{-android.R.attr.state_pressed}, mNormalDradable);
        this.addState(new int[]{android.R.attr.state_pressed}, mPressedDradable);
    }
     
    public float getTopLeftRadius() {
        return mTopLeftRadius;
    }
 
    public void setTopLeftRadius(float topLeftRadius) {
        this.mTopLeftRadius = topLeftRadius;
    }
 
    public float getTopRightRadius() {
        return mTopRightRadius;
    }
 
    public void setTopRightRadius(float topRightRadius) {
        this.mTopRightRadius = topRightRadius;
    }
 
    public float getBottomLeftRadius() {
        return mBottomLeftRadius;
    }
 
    public void setBottomLeftRadius(float bottomLeftRadius) {
        this.mBottomLeftRadius = bottomLeftRadius;
    }
 
    public float getBottomRightRadius() {
        return mBottomRightRadius;
    }
 
    public void setBottomRightRadius(float bottomRightRadius) {
        this.mBottomRightRadius = bottomRightRadius;
    }
 
    public int getNormalColor() {
        return mNormalColor;
    }
 
    public void setNormalColor(int normalColor) {
        this.mNormalColor = normalColor;
    }
 
    public int getPressedColor() {
        return mPressedColor;
    }
 
    public void setPressedColor(int pressedColor) {
        this.mPressedColor = pressedColor;
    }
     
}

android圆角矩形背景

标签:

原文地址:http://blog.csdn.net/u014311077/article/details/42675957

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!