Skip to content

Commit 79091d1

Browse files
author
Kaler
committed
Fix bugs
1.make setattributes in customView class 2.slider max's defalut value is 100.(fix bug) 3.you can set checkboxSize in CheckBox class 4.you can set thumbSize in Switch class ……
1 parent 2cd53c6 commit 79091d1

File tree

10 files changed

+395
-357
lines changed

10 files changed

+395
-357
lines changed

MaterialDesign/src/com/gc/materialdesign/utils/Utils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@ public class Utils {
99

1010
/**
1111
* Convert Dp to Pixel
12+
* 将dp转换为pixel
1213
*/
1314
public static int dpToPx(float dp, Resources resources){
1415
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
1516
return (int) px;
1617
}
1718

19+
/**
20+
* @param value
21+
* @return 将dip或者dp转为float
22+
*/
23+
public static float dipOrDpToFloat(String value) {
24+
if (value.indexOf("dp") != -1) {
25+
value = value.replace("dp", "");
26+
}
27+
else {
28+
value = value.replace("dip", "");
29+
}
30+
return Float.parseFloat(value);
31+
}
32+
33+
1834
public static int getRelativeTop(View myView) {
1935
// if (myView.getParent() == myView.getRootView())
2036
if(myView.getId() == android.R.id.content)

MaterialDesign/src/com/gc/materialdesign/views/Card.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
package com.gc.materialdesign.views;
22

33
import com.gc.materialdesign.R;
4-
import com.gc.materialdesign.utils.Utils;
54

65
import android.content.Context;
7-
import android.graphics.Bitmap;
8-
import android.graphics.Bitmap.Config;
9-
import android.graphics.Canvas;
106
import android.graphics.Color;
11-
import android.graphics.Paint;
12-
import android.graphics.Rect;
13-
import android.graphics.Typeface;
147
import android.graphics.drawable.GradientDrawable;
158
import android.graphics.drawable.LayerDrawable;
169
import android.util.AttributeSet;
17-
import android.util.Log;
18-
import android.view.Gravity;
19-
import android.view.MotionEvent;
20-
import android.widget.FrameLayout;
21-
import android.widget.LinearLayout;
22-
import android.widget.RelativeLayout;
2310
import android.widget.TextView;
24-
import android.widget.Toast;
25-
import android.widget.RelativeLayout.LayoutParams;
2611

2712
public class Card extends CustomView {
2813

@@ -40,7 +25,11 @@ public Card(Context context, AttributeSet attrs) {
4025
// Set atributtes of XML to View
4126
protected void setAttributes(AttributeSet attrs){
4227

43-
setBackgroundResource(R.drawable.background_button_rectangle);
28+
if(!isInEditMode()) {
29+
setBackgroundResource(R.drawable.background_button_rectangle);
30+
}
31+
32+
4433
//Set background Color
4534
// Color by resource
4635
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1);
@@ -59,11 +48,16 @@ protected void setAttributes(AttributeSet attrs){
5948
// Set color of background
6049
public void setBackgroundColor(int color){
6150
this.backgroundColor = color;
62-
if(isEnabled())
51+
if(isEnabled()) {
6352
beforeBackground = backgroundColor;
53+
}
6454
LayerDrawable layer = (LayerDrawable) getBackground();
65-
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
66-
shape.setColor(backgroundColor);
55+
GradientDrawable shape = null;
56+
if (!isInEditMode()) {
57+
shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
58+
shape.setColor(backgroundColor);
59+
}
60+
6761
}
6862

6963
}
Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.gc.materialdesign.views;
22

3-
import com.gc.materialdesign.R;
4-
import com.gc.materialdesign.utils.Utils;
5-
63
import android.content.Context;
74
import android.graphics.Bitmap;
85
import android.graphics.BitmapFactory;
@@ -17,9 +14,10 @@
1714
import android.view.View;
1815
import android.widget.RelativeLayout;
1916

20-
public class CheckBox extends CustomView {
17+
import com.gc.materialdesign.R;
18+
import com.gc.materialdesign.utils.Utils;
2119

22-
int backgroundColor = Color.parseColor("#4CAF50");
20+
public class CheckBox extends CustomView {
2321

2422
Check checkView;
2523

@@ -30,62 +28,55 @@ public class CheckBox extends CustomView {
3028

3129
public CheckBox(Context context, AttributeSet attrs) {
3230
super(context, attrs);
31+
backgroundColor = Color.parseColor("#4CAF50");
32+
minWidth = 48;
33+
minHeight = 48;
3334
setAttributes(attrs);
3435
}
3536

3637
// Set atributtes of XML to View
3738
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);
5142
} 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);
5744
}
45+
setBackgroundAttributes(attrs);
5846

59-
boolean check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
60-
"check", false);
47+
boolean check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "check", false);
6148
if (check) {
6249
post(new Runnable() {
6350

6451
@Override
6552
public void run() {
6653
setChecked(true);
6754
setPressed(false);
68-
changeBackgroundColor(getResources().getColor(
69-
android.R.color.transparent));
55+
changeBackgroundColor(getResources().getColor(android.R.color.transparent));
7056
}
7157
});
7258
}
7359

60+
float size = 20;
61+
String checkBoxSize = attrs.getAttributeValue(MATERIALDESIGNXML, "checkBoxSize");
62+
if (checkBoxSize != null) {
63+
size = Utils.dipOrDpToFloat(checkBoxSize);
64+
}
65+
7466
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()));
7769
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
7870
checkView.setLayoutParams(params);
7971
addView(checkView);
80-
8172
}
8273

8374
@Override
8475
public boolean onTouchEvent(MotionEvent event) {
8576
if (isEnabled()) {
8677
isLastTouch = true;
8778
if (event.getAction() == MotionEvent.ACTION_DOWN) {
88-
changeBackgroundColor((check) ? makePressColor() : Color
79+
changeBackgroundColor((check) ? makePressColor(70) : Color
8980
.parseColor("#446D6D6D"));
9081
} else if (event.getAction() == MotionEvent.ACTION_UP) {
9182
changeBackgroundColor(getResources().getColor(
@@ -114,7 +105,7 @@ protected void onDraw(Canvas canvas) {
114105
if (press) {
115106
Paint paint = new Paint();
116107
paint.setAntiAlias(true);
117-
paint.setColor((check) ? makePressColor() : Color
108+
paint.setColor((check) ? makePressColor(70) : Color
118109
.parseColor("#446D6D6D"));
119110
canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2,
120111
paint);
@@ -129,46 +120,30 @@ private void changeBackgroundColor(int color) {
129120
shape.setColor(color);
130121
}
131122

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-
147123
@Override
148124
public void setBackgroundColor(int color) {
149125
backgroundColor = color;
150-
if (isEnabled())
151-
beforeBackground = backgroundColor;
126+
if (isEnabled()) {
127+
beforeBackground = backgroundColor;
128+
}
152129
changeBackgroundColor(color);
153130
}
154131

155132
public void setChecked(boolean check) {
156133
this.check = check;
157134
setPressed(false);
158-
changeBackgroundColor(getResources().getColor(
159-
android.R.color.transparent));
135+
changeBackgroundColor(getResources().getColor(android.R.color.transparent));
160136
if (check) {
161137
step = 0;
162138
}
163139
if (check)
164140
checkView.changeBackground();
165-
166141
}
167142

168-
public boolean isCheck() {
143+
public boolean isChecked() {
169144
return check;
170145
}
171-
146+
172147
// Indicate step in check animation
173148
int step = 0;
174149

@@ -179,7 +154,9 @@ class Check extends View {
179154

180155
public Check(Context context) {
181156
super(context);
182-
setBackgroundResource(R.drawable.background_checkbox_uncheck);
157+
if (!isInEditMode()) {
158+
setBackgroundResource(R.drawable.background_checkbox_uncheck);
159+
}
183160
sprite = BitmapFactory.decodeResource(context.getResources(),
184161
R.drawable.sprite_check);
185162
}
@@ -192,7 +169,9 @@ public void changeBackground() {
192169
.findDrawableByLayerId(R.id.shape_bacground);
193170
shape.setColor(backgroundColor);
194171
} else {
195-
setBackgroundResource(R.drawable.background_checkbox_uncheck);
172+
if (!isInEditMode()) {
173+
setBackgroundResource(R.drawable.background_checkbox_uncheck);
174+
}
196175
}
197176
}
198177

@@ -211,19 +190,30 @@ protected void onDraw(Canvas canvas) {
211190
}
212191
Rect src = new Rect(40 * step, 0, (40 * step) + 40, 40);
213192
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+
}
215196
invalidate();
216197

217198
}
218199

219200
}
220201

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+
221211
public void setOncheckListener(OnCheckListener onCheckListener) {
222212
this.onCheckListener = onCheckListener;
223213
}
224214

225215
public interface OnCheckListener {
226-
public void onCheck(boolean check);
216+
public void onCheck(boolean isChecked);
227217
}
228218

229219
}

0 commit comments

Comments
 (0)