Skip to content

Commit 8c228c5

Browse files
author
Kaler
committed
Refactor the code and fix bugs
add setchecked setthumbSize seticonsize setrippleBoundsRadius setrippleSpeed setValue setText setTextColor setIcon setIconDrawable
1 parent 22921ea commit 8c228c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+683
-487
lines changed
Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
34
<declare-styleable name="CustomAttributes">
4-
<!-- indicate if the slider must show number indicator -->
5-
<attr name="showNumberIndicator" format="boolean" />
6-
<!-- in progress view indicate max value of progress -->
7-
<attr name="max" format="integer" />
8-
<!-- in progress view indicate min value of progress -->
9-
<attr name="min" format="integer" />
10-
<!-- in progress view indicate value of progress -->
11-
<attr name="value" format="integer" />
12-
<!-- in progress view indicate value of progress -->
13-
<attr name="progress" format="integer" />
14-
<!-- in switche's view indicate state of view -->
15-
<attr name="check" format="boolean" />
16-
<!-- in float button indicate icon resource -->
17-
<attr name="iconFloat" format="integer" />
18-
<!-- in float button indicate if must start with animation -->
19-
<attr name="animate" format="boolean" />
20-
<!-- Speed of ripple animation -->
21-
<attr name="rippleSpeed" format="float" />
22-
<!-- in float button indicate if must start with animation -->
23-
<attr name="rippleColor" format="color|reference" />
24-
</declare-styleable>
25-
26-
27-
</resources>
5+
6+
<!-- Speed of ripple animation -->
7+
<attr name="rippleSpeed" format="float" />
8+
<!-- in float button indicate if must start with animation -->
9+
<attr name="rippleColor" format="color|reference" />
10+
<!-- indicate if the slider must show number indicator -->
11+
<attr name="showNumberIndicator" format="boolean" />
12+
<!-- in progress view indicate max value of progress -->
13+
<attr name="max" format="integer" />
14+
<!-- in progress view indicate min value of progress -->
15+
<attr name="min" format="integer" />
16+
<!-- in progress view indicate value of progress -->
17+
<attr name="value" format="integer" />
18+
<!-- in progress view indicate value of progress -->
19+
<attr name="progress" format="integer" />
20+
<!-- in switche's view indicate state of view -->
21+
<attr name="checked" format="boolean" />
22+
<attr name="checkBoxSize" format="dimension" />
23+
<attr name="thumbSize" format="dimension" />
24+
<!-- in float button indicate icon resource -->
25+
<attr name="iconFloat" format="integer" />
26+
<!-- in float button indicate icon resource -->
27+
<attr name="iconSize" format="dimension" />
28+
<!-- in float button indicate if must start with animation -->
29+
<attr name="animate" format="boolean" />
30+
<attr name="rippleBorderRadius" format="float" />
31+
<attr name="clickAfterRipple" format="boolean" />
32+
</declare-styleable>
33+
34+
</resources>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public Button(Context context, AttributeSet attrs) {
3030

3131
protected void onInitDefaultValues() {
3232
backgroundColor = Color.parseColor("#1E88E5");// 默认的背景色,蓝色
33-
//backgroundColor = Color.parseColor("#FF88E5");// 默认的背景色,蓝色
3433
if (!isInEditMode()) {
3534
/**
3635
* 默认的资源,这里因为没有初始化,所以需要在子类中初始化这个资源id。
@@ -138,15 +137,15 @@ public void setOnClickListener(OnClickListener l) {
138137
}
139138

140139
// Set color of background
140+
@Override
141141
public void setBackgroundColor(int color) {
142142
backgroundColor = color;
143143
if (isEnabled())
144144
beforeBackground = backgroundColor;
145145
try {
146146
LayerDrawable layer = (LayerDrawable) getBackground();
147147
// 每个按钮的框架都是由drawable中的xml文件制定的,xml文件中都有一个item的id叫:shape_bacground
148-
GradientDrawable shape = (GradientDrawable) layer
149-
.findDrawableByLayerId(R.id.shape_bacground);
148+
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
150149
/**
151150
* 给这个图片设置背景色,因为图片的主体是透明的所以可以直接显示背景色
152151
* 效果就是一个透明但有阴影的框架下有了背景色,这样的方式可以方便的设置不同颜色的按钮,让按钮看起来还是浑然一体

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.Color;
66
import android.graphics.Paint;
77
import android.util.AttributeSet;
8+
import android.widget.TextView;
89

910
import com.gc.materialdesign.R;
1011

@@ -16,6 +17,7 @@ public ButtonFlat(Context context, AttributeSet attrs) {
1617

1718
@Override
1819
protected void onInitDefaultValues(){
20+
textButton = new TextView(getContext());
1921
minHeight = 36;
2022
minWidth = 88;
2123
rippleSpeed = 6f;
@@ -51,7 +53,7 @@ protected void onDraw(Canvas canvas) {
5153
@Override
5254
@Deprecated
5355
public void setBackgroundColor(int color) {
54-
//无效方法
56+
textButton.setTextColor(color);
5557
}
5658

5759

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
public class ButtonFloat extends Button {
2929

30-
protected int sizeIcon;// 内部图片大小
30+
protected int iconSize;// 内部图片大小
3131
protected int sizeRadius;// 图标半径
3232

3333
ImageView icon; // 按钮中的ImageView
@@ -41,12 +41,12 @@ public ButtonFloat(Context context, AttributeSet attrs) {
4141
protected void onInitDefaultValues() {
4242
backgroundResId = R.drawable.background_button_float;
4343
super.onInitDefaultValues();
44-
sizeIcon = 24;
44+
iconSize = 24;
4545
sizeRadius = 28;
4646
rippleSpeed = 3;
4747
rippleSize = 5;
48-
minWidth = sizeRadius * 2;
49-
minHeight = sizeRadius * 2;
48+
minWidth = sizeRadius * 2;// 56dp
49+
minHeight = sizeRadius * 2;// 56dp
5050
}
5151

5252
// 将xml文件中的属性设置到view中
@@ -81,20 +81,20 @@ public void run() {
8181
icon.setBackgroundDrawable(iconDrawable);
8282
}
8383
// 设置按钮中图标的大小
84-
String iconSize = attrs.getAttributeValue(MATERIALDESIGNXML, "drawableIconSize");
85-
if (iconSize != null) {
86-
sizeIcon = (int) Utils.dipOrDpToFloat(iconSize);
84+
String size = attrs.getAttributeValue(MATERIALDESIGNXML, "iconSize");
85+
if (size != null) {
86+
iconSize = (int) Utils.dipOrDpToFloat(size);
8787
}
8888
setIconParams();
89+
addView(icon);
8990
}
9091

9192
private void setIconParams() {
9293
// TODO 自动生成的方法存根
9394
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
94-
Utils.dpToPx(sizeIcon, getResources()), Utils.dpToPx(sizeIcon, getResources()));
95+
Utils.dpToPx(iconSize, getResources()), Utils.dpToPx(iconSize, getResources()));
9596
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
9697
icon.setLayoutParams(params);
97-
addView(icon);
9898
}
9999

100100
@Override
@@ -132,14 +132,27 @@ public Bitmap cropCircle(Bitmap bitmap) {
132132

133133
// GET AND SET
134134

135+
public void isAnimate(boolean isAnimate) {
136+
if (isAnimate) {
137+
post(new Runnable() {
138+
@Override
139+
public void run() {
140+
float originalY = ViewHelper.getY(ButtonFloat.this) - Utils.dpToPx(24, getResources());
141+
ViewHelper.setY(ButtonFloat.this,
142+
ViewHelper.getY(ButtonFloat.this) + getHeight() * 3);
143+
ObjectAnimator animator = ObjectAnimator.ofFloat(ButtonFloat.this, "y", originalY);
144+
animator.setInterpolator(new BounceInterpolator());
145+
animator.setDuration(1500);// 动画持续时间
146+
animator.start();
147+
}
148+
});
149+
}
150+
}
151+
135152
public ImageView getIcon() {
136153
return icon;
137154
}
138155

139-
public void setIcon(ImageView icon) {
140-
this.icon = icon;
141-
}
142-
143156
public Drawable getIconDrawable() {
144157
return iconDrawable;
145158
}
@@ -154,18 +167,16 @@ public void setIconDrawable(Drawable drawableIcon) {
154167
*
155168
* @param size
156169
*/
157-
public void setDrawableIconSize(int size) {
158-
sizeIcon = size;
159-
removeView(icon);// 先移除里面的imageview,然后再add新的ImageView
160-
// TODO:更新代码,进行重新绘制
170+
public void setIconSize(int size) {
171+
iconSize = size;
161172
setIconParams();
162173
}
163174

164175
/**
165176
* @return 按钮中心图片的大小
166177
*/
167-
public int getDrawableIconSize() {
168-
return sizeIcon;
178+
public int getIconSize() {
179+
return iconSize;
169180
}
170181

171182
@Override
Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.gc.materialdesign.views;
22

33
import android.content.Context;
4+
import android.graphics.Color;
45
import android.util.AttributeSet;
5-
import android.widget.RelativeLayout;
66

77
import com.gc.materialdesign.R;
8-
import com.gc.materialdesign.utils.Utils;
98

109
/**
1110
* @tips :很小的圆形按钮,上面可以添加图片
@@ -19,53 +18,19 @@ public ButtonFloatSmall(Context context, AttributeSet attrs) {
1918

2019
@Override
2120
protected void onInitDefaultValues() {
21+
backgroundColor = Color.parseColor("#1E88E5");// 默认的背景色,蓝色
2222
sizeRadius = 20;
23-
sizeIcon = 24;
23+
iconSize = 24;
2424
rippleSpeed = 2;
2525
rippleSize = 10;
26-
minWidth = sizeRadius * 2;
27-
minHeight = sizeRadius * 2;
26+
minWidth = sizeRadius * 2;// 40dp
27+
minHeight = sizeRadius * 2;// 40dp
2828
// Background shape
2929
if (!isInEditMode()) {
3030
setBackgroundResource(R.drawable.background_button_float);// 这是一个圆形的带阴影的图片
3131
}
32-
}
33-
34-
@Override
35-
protected void onInitAttributes(AttributeSet attrs) {
36-
super.onInitAttributes(attrs);
37-
// 设置按钮中图标的大小
38-
String iconSize = attrs.getAttributeValue(MATERIALDESIGNXML, "iconSize");
39-
if (iconSize != null) {
40-
sizeIcon = (int) Utils.dipOrDpToFloat(iconSize);
41-
}
42-
setPropertiesAndParams();
32+
setBackgroundColor(backgroundColor);
4333
}
4434

45-
private void setPropertiesAndParams() {
46-
onInitDefaultValues();
47-
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
48-
Utils.dpToPx(sizeIcon, getResources()), Utils.dpToPx(sizeIcon, getResources()));
49-
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
50-
icon.setLayoutParams(params);
51-
}
52-
53-
/**
54-
* 设置button中图片的大小,默认是居中显示的。 如果图片大小超过了按钮的大小,那么按钮会根据图片进行放大,直到能包含内部图片为止
55-
*
56-
* @param size
57-
*/
58-
public void setIconSize(int size) {
59-
sizeIcon = size;
60-
// TODO:更新代码,进行重新绘制
61-
setPropertiesAndParams();
62-
}
63-
64-
/**
65-
* @return 按钮中心图片的大小
66-
*/
67-
public int getIconSize() {
68-
return sizeIcon;
69-
}
7035

7136
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public ButtonIcon(Context context, AttributeSet attrs) {
1515

1616
@Override
1717
protected void onInitDefaultValues() {
18-
minHeight = 36;
19-
minWidth = 88;
18+
super.onInitDefaultValues();
2019
rippleSpeed = 2;
21-
rippleSize = 2;
22-
setBackground(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
20+
rippleSize = 5;
21+
// Background shape
22+
setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
2323
}
2424

2525
@Override
@@ -37,7 +37,7 @@ protected void onDraw(Canvas canvas) {
3737
if (x != -1) {
3838
Paint paint = new Paint();
3939
paint.setAntiAlias(true);
40-
paint.setColor(makePressColor());
40+
paint.setColor(backgroundColor);
4141
canvas.drawCircle(x, y, radius, paint);
4242
if(radius > getHeight()/rippleSize)
4343
radius += rippleSpeed;
@@ -51,9 +51,5 @@ protected void onDraw(Canvas canvas) {
5151
}
5252
invalidate();
5353
}
54-
55-
protected int makePressColor() {
56-
return backgroundColor;
57-
}
5854

5955
}

0 commit comments

Comments
 (0)