1
1
package com .gc .materialdesign .views ;
2
2
3
- import com .gc .materialdesign .R ;
4
- import com .gc .materialdesign .utils .Utils ;
5
-
6
3
import android .content .Context ;
7
4
import android .graphics .Bitmap ;
8
5
import android .graphics .BitmapFactory ;
17
14
import android .view .View ;
18
15
import android .widget .RelativeLayout ;
19
16
20
- public class CheckBox extends CustomView {
17
+ import com .gc .materialdesign .R ;
18
+ import com .gc .materialdesign .utils .Utils ;
21
19
22
- int backgroundColor = Color . parseColor ( "#4CAF50" );
20
+ public class CheckBox extends CustomView {
23
21
24
22
Check checkView ;
25
23
@@ -30,62 +28,55 @@ public class CheckBox extends CustomView {
30
28
31
29
public CheckBox (Context context , AttributeSet attrs ) {
32
30
super (context , attrs );
31
+ backgroundColor = Color .parseColor ("#4CAF50" );
32
+ minWidth = 48 ;
33
+ minHeight = 48 ;
33
34
setAttributes (attrs );
34
35
}
35
36
36
37
// Set atributtes of XML to View
37
38
protected void setAttributes (AttributeSet attrs ) {
38
-
39
- setBackgroundResource (R .drawable .background_checkbox );
40
-
41
- // Set size of view
42
- setMinimumHeight (Utils .dpToPx (48 , getResources ()));
43
- setMinimumWidth (Utils .dpToPx (48 , getResources ()));
44
-
45
- // Set background Color
46
- // Color by resource
47
- int bacgroundColor = attrs .getAttributeResourceValue (ANDROIDXML ,
48
- "background" , -1 );
49
- if (bacgroundColor != -1 ) {
50
- setBackgroundColor (getResources ().getColor (bacgroundColor ));
39
+ if (!isInEditMode ()) {
40
+ // 设置checkbox的背景色
41
+ setBackgroundResource (R .drawable .background_checkbox );
51
42
} else {
52
- // Color by hexadecimal
53
- // Color by hexadecimal
54
- int background = attrs .getAttributeIntValue (ANDROIDXML , "background" , -1 );
55
- if (background != -1 )
56
- setBackgroundColor (background );
43
+ setBackgroundResource (android .R .drawable .checkbox_on_background );
57
44
}
45
+ setBackgroundAttributes (attrs );
58
46
59
- boolean check = attrs .getAttributeBooleanValue (MATERIALDESIGNXML ,
60
- "check" , false );
47
+ boolean check = attrs .getAttributeBooleanValue (MATERIALDESIGNXML , "check" , false );
61
48
if (check ) {
62
49
post (new Runnable () {
63
50
64
51
@ Override
65
52
public void run () {
66
53
setChecked (true );
67
54
setPressed (false );
68
- changeBackgroundColor (getResources ().getColor (
69
- android .R .color .transparent ));
55
+ changeBackgroundColor (getResources ().getColor (android .R .color .transparent ));
70
56
}
71
57
});
72
58
}
73
59
60
+ float size = 20 ;
61
+ String checkBoxSize = attrs .getAttributeValue (MATERIALDESIGNXML , "checkBoxSize" );
62
+ if (checkBoxSize != null ) {
63
+ size = Utils .dipOrDpToFloat (checkBoxSize );
64
+ }
65
+
74
66
checkView = new Check (getContext ());
75
- RelativeLayout .LayoutParams params = new LayoutParams (Utils . dpToPx ( 20 ,
76
- getResources ()), Utils .dpToPx (20 , getResources ()));
67
+ RelativeLayout .LayoutParams params = new LayoutParams (
68
+ Utils . dpToPx ( size , getResources ()), Utils .dpToPx (size , getResources ()));
77
69
params .addRule (RelativeLayout .CENTER_IN_PARENT , RelativeLayout .TRUE );
78
70
checkView .setLayoutParams (params );
79
71
addView (checkView );
80
-
81
72
}
82
73
83
74
@ Override
84
75
public boolean onTouchEvent (MotionEvent event ) {
85
76
if (isEnabled ()) {
86
77
isLastTouch = true ;
87
78
if (event .getAction () == MotionEvent .ACTION_DOWN ) {
88
- changeBackgroundColor ((check ) ? makePressColor () : Color
79
+ changeBackgroundColor ((check ) ? makePressColor (70 ) : Color
89
80
.parseColor ("#446D6D6D" ));
90
81
} else if (event .getAction () == MotionEvent .ACTION_UP ) {
91
82
changeBackgroundColor (getResources ().getColor (
@@ -114,7 +105,7 @@ protected void onDraw(Canvas canvas) {
114
105
if (press ) {
115
106
Paint paint = new Paint ();
116
107
paint .setAntiAlias (true );
117
- paint .setColor ((check ) ? makePressColor () : Color
108
+ paint .setColor ((check ) ? makePressColor (70 ) : Color
118
109
.parseColor ("#446D6D6D" ));
119
110
canvas .drawCircle (getWidth () / 2 , getHeight () / 2 , getWidth () / 2 ,
120
111
paint );
@@ -129,46 +120,30 @@ private void changeBackgroundColor(int color) {
129
120
shape .setColor (color );
130
121
}
131
122
132
- /**
133
- * Make a dark color to press effect
134
- *
135
- * @return
136
- */
137
- protected int makePressColor () {
138
- int r = (this .backgroundColor >> 16 ) & 0xFF ;
139
- int g = (this .backgroundColor >> 8 ) & 0xFF ;
140
- int b = (this .backgroundColor >> 0 ) & 0xFF ;
141
- r = (r - 30 < 0 ) ? 0 : r - 30 ;
142
- g = (g - 30 < 0 ) ? 0 : g - 30 ;
143
- b = (b - 30 < 0 ) ? 0 : b - 30 ;
144
- return Color .argb (70 , r , g , b );
145
- }
146
-
147
123
@ Override
148
124
public void setBackgroundColor (int color ) {
149
125
backgroundColor = color ;
150
- if (isEnabled ())
151
- beforeBackground = backgroundColor ;
126
+ if (isEnabled ()) {
127
+ beforeBackground = backgroundColor ;
128
+ }
152
129
changeBackgroundColor (color );
153
130
}
154
131
155
132
public void setChecked (boolean check ) {
156
133
this .check = check ;
157
134
setPressed (false );
158
- changeBackgroundColor (getResources ().getColor (
159
- android .R .color .transparent ));
135
+ changeBackgroundColor (getResources ().getColor (android .R .color .transparent ));
160
136
if (check ) {
161
137
step = 0 ;
162
138
}
163
139
if (check )
164
140
checkView .changeBackground ();
165
-
166
141
}
167
142
168
- public boolean isCheck () {
143
+ public boolean isChecked () {
169
144
return check ;
170
145
}
171
-
146
+
172
147
// Indicate step in check animation
173
148
int step = 0 ;
174
149
@@ -179,7 +154,9 @@ class Check extends View {
179
154
180
155
public Check (Context context ) {
181
156
super (context );
182
- setBackgroundResource (R .drawable .background_checkbox_uncheck );
157
+ if (!isInEditMode ()) {
158
+ setBackgroundResource (R .drawable .background_checkbox_uncheck );
159
+ }
183
160
sprite = BitmapFactory .decodeResource (context .getResources (),
184
161
R .drawable .sprite_check );
185
162
}
@@ -192,7 +169,9 @@ public void changeBackground() {
192
169
.findDrawableByLayerId (R .id .shape_bacground );
193
170
shape .setColor (backgroundColor );
194
171
} else {
195
- setBackgroundResource (R .drawable .background_checkbox_uncheck );
172
+ if (!isInEditMode ()) {
173
+ setBackgroundResource (R .drawable .background_checkbox_uncheck );
174
+ }
196
175
}
197
176
}
198
177
@@ -211,19 +190,30 @@ protected void onDraw(Canvas canvas) {
211
190
}
212
191
Rect src = new Rect (40 * step , 0 , (40 * step ) + 40 , 40 );
213
192
Rect dst = new Rect (0 , 0 , this .getWidth () - 2 , this .getHeight ());
214
- canvas .drawBitmap (sprite , src , dst , null );
193
+ if (!isInEditMode ()) {
194
+ canvas .drawBitmap (sprite , src , dst , null );
195
+ }
215
196
invalidate ();
216
197
217
198
}
218
199
219
200
}
220
201
202
+ public void setCheckBoxSize (float size ) {
203
+ removeView (checkView );
204
+ RelativeLayout .LayoutParams params = new LayoutParams (
205
+ Utils .dpToPx (size , getResources ()), Utils .dpToPx (size , getResources ()));
206
+ params .addRule (RelativeLayout .CENTER_IN_PARENT , RelativeLayout .TRUE );
207
+ checkView .setLayoutParams (params );
208
+ addView (checkView );
209
+ }
210
+
221
211
public void setOncheckListener (OnCheckListener onCheckListener ) {
222
212
this .onCheckListener = onCheckListener ;
223
213
}
224
214
225
215
public interface OnCheckListener {
226
- public void onCheck (boolean check );
216
+ public void onCheck (boolean isChecked );
227
217
}
228
218
229
219
}
0 commit comments