Skip to content

Commit 1c574cf

Browse files
author
ivan.navas
committed
Fix some bug and performance improvement
1 parent c8c0b64 commit 1c574cf

34 files changed

+1350
-1311
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#org.springsource.ide.eclipse.gradle.core.preferences.GradleImportPreferences
2+
#Sun Jan 11 18:49:20 CET 2015
3+
addResourceFilters=true
4+
afterTasks=afterEclipseImport;
5+
beforeTasks=cleanEclipse;eclipse;
6+
enableAfterTasks=true
7+
enableBeforeTasks=true
8+
enableDSLD=false
9+
enableDependendencyManagement=true
10+
projects=MaterialDesign;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences
2+
#Sun Jan 11 18:49:02 CET 2015
3+
build.family.org.gradle.tooling.model.eclipse.HierarchicalEclipseProject=;MaterialDesign;MaterialDesignDemo;
4+
org.springsource.ide.eclipse.gradle.rootprojectloc=

MaterialDesign/.classpath

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3+
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
34
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
4-
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
55
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
6-
<classpathentry kind="lib" path="libs/nineoldandroids-2.4.0.jar"/>
6+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
77
<classpathentry kind="src" path="src"/>
88
<classpathentry kind="src" path="gen"/>
99
<classpathentry kind="output" path="bin/classes"/>

MaterialDesign/.project

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
</buildCommand>
2828
</buildSpec>
2929
<natures>
30-
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
30+
<nature>org.springsource.ide.eclipse.gradle.core.nature</nature>
3131
<nature>org.eclipse.jdt.core.javanature</nature>
32+
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
3233
</natures>
3334
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences
2+
#Sun Jan 11 18:49:24 CET 2015
3+
org.springsource.ide.eclipse.gradle.linkedresources=
4+
org.springsource.ide.eclipse.gradle.rootprojectloc=..
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#org.springsource.ide.eclipse.gradle.core.actions.GradleRefreshPreferences
2+
#Sun Jan 11 18:49:23 CET 2015
3+
addResourceFilters=true
4+
afterTasks=afterEclipseImport;
5+
beforeTasks=cleanEclipse;eclipse;
6+
enableAfterTasks=true
7+
enableBeforeTasks=true
8+
enableDSLD=false
9+
useHierarchicalNames=false

MaterialDesign/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ android {
2525
defaultConfig {
2626
minSdkVersion 8
2727
targetSdkVersion 21
28-
versionCode 1
29-
versionName '1.0'
28+
versionCode 2
29+
versionName '1.1'
3030
}
3131
}
3232

@@ -35,8 +35,8 @@ ext.issueUrl = 'https://github.com/navasmdc/MaterialDesignLibrary/issues'
3535
ext.gitUrl = 'https://github.com/navasmdc/MaterialDesignLibrary.git'
3636

3737
bintray {
38-
user = hasProperty('BINTRAY_USER') ? BINTRAY_USER : "_"
39-
key = hasProperty('BINTRAY_KEY') ? BINTRAY_PASSWORD : "_"
38+
user = hasProperty('BINTRAY_USER') ? BINTRAY_USER : ""
39+
key = hasProperty('BINTRAY_KEY') ? BINTRAY_PASSWORD : ""
4040

4141
configurations = ["archives"]
4242
pkg {

MaterialDesign/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
1212

1313
# Project target.
14-
target=android-8
14+
target=android-19
1515
android.library=true

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.gc.materialdesign.utils;
22

33
import android.content.res.Resources;
4-
import android.graphics.Rect;
54
import android.util.TypedValue;
65
import android.view.View;
76

@@ -10,38 +9,18 @@ public class Utils {
109

1110
/**
1211
* Convert Dp to Pixel
13-
* 将dp转换为pixel
1412
*/
1513
public static int dpToPx(float dp, Resources resources){
1614
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
1715
return (int) px;
1816
}
1917

20-
/**
21-
* @param value
22-
* @return 将dip或者dp转为float
23-
*/
24-
public static float dipOrDpToFloat(String value) {
25-
if (value.indexOf("dp") != -1) {
26-
value = value.replace("dp", "");
27-
}
28-
else {
29-
value = value.replace("dip", "");
30-
}
31-
return Float.parseFloat(value);
32-
}
33-
34-
35-
/**
36-
* 这里看似是得到控件相对的坐标,但是如果这个滑动条在可以上下滚动的布局中就会出现问题。
37-
* 因为这里的坐标都是死的,在上下滚动的view中父控件的top仍旧不变,但实际上是应该获得动态数值的。
38-
* @param myView
39-
* @return
40-
*/
4118
public static int getRelativeTop(View myView) {
42-
Rect bounds = new Rect();
43-
myView.getGlobalVisibleRect(bounds);
44-
return bounds.top;
19+
// if (myView.getParent() == myView.getRootView())
20+
if(myView.getId() == android.R.id.content)
21+
return myView.getTop();
22+
else
23+
return myView.getTop() + getRelativeTop((View) myView.getParent());
4524
}
4625

4726
public static int getRelativeLeft(View myView) {
Lines changed: 144 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,179 @@
11
package com.gc.materialdesign.views;
22

3+
import com.gc.materialdesign.R;
4+
import com.gc.materialdesign.utils.Utils;
5+
36
import android.content.Context;
47
import android.graphics.Bitmap;
5-
import android.graphics.Bitmap.Config;
8+
import android.graphics.Canvas;
69
import android.graphics.Color;
10+
import android.graphics.Paint;
11+
import android.graphics.Rect;
12+
import android.graphics.Bitmap.Config;
713
import android.graphics.drawable.GradientDrawable;
814
import android.graphics.drawable.LayerDrawable;
915
import android.util.AttributeSet;
16+
import android.view.MotionEvent;
1017
import android.widget.TextView;
1118

12-
import com.gc.materialdesign.R;
13-
import com.gc.materialdesign.utils.Utils;
19+
public abstract class Button extends CustomView {
1420

15-
public abstract class Button extends RippleView {
21+
final static String ANDROIDXML = "http://schemas.android.com/apk/res/android";
22+
23+
// Complete in child class
24+
int minWidth;
25+
int minHeight;
26+
int background;
27+
float rippleSpeed = 10f;
28+
int rippleSize = 3;
29+
Integer rippleColor;
30+
OnClickListener onClickListener;
31+
int backgroundColor = Color.parseColor("#1E88E5");
1632

1733
public Button(Context context, AttributeSet attrs) {
1834
super(context, attrs);
19-
onInitAttributes(attrs);
35+
setDefaultProperties();
36+
setAttributes(attrs);
37+
beforeBackground = backgroundColor;
38+
if(rippleColor==null)
39+
rippleColor = makePressColor();
40+
}
41+
42+
protected void setDefaultProperties() {
43+
// Min size
44+
setMinimumHeight(Utils.dpToPx(minHeight, getResources()));
45+
setMinimumWidth(Utils.dpToPx(minWidth, getResources()));
46+
// Background shape
47+
setBackgroundResource(background);
48+
setBackgroundColor(backgroundColor);
2049
}
2150

51+
52+
// Set atributtes of XML to View
53+
abstract protected void setAttributes(AttributeSet attrs);
54+
55+
// ### RIPPLE EFFECT ###
56+
57+
float x = -1, y = -1;
58+
float radius = -1;
59+
2260
@Override
23-
protected void onInitDefaultValues() {
24-
backgroundColor = Color.parseColor("#2196f3");// 默认的背景色,蓝色
25-
///beforeBackground = backgroundColor;// error
61+
public boolean onTouchEvent(MotionEvent event) {
62+
invalidate();
63+
if (isEnabled()) {
64+
isLastTouch = true;
65+
if (event.getAction() == MotionEvent.ACTION_DOWN) {
66+
radius = getHeight() / rippleSize;
67+
x = event.getX();
68+
y = event.getY();
69+
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
70+
radius = getHeight() / rippleSize;
71+
x = event.getX();
72+
y = event.getY();
73+
if (!((event.getX() <= getWidth() && event.getX() >= 0) && (event
74+
.getY() <= getHeight() && event.getY() >= 0))) {
75+
isLastTouch = false;
76+
x = -1;
77+
y = -1;
78+
}
79+
} else if (event.getAction() == MotionEvent.ACTION_UP) {
80+
if ((event.getX() <= getWidth() && event.getX() >= 0)
81+
&& (event.getY() <= getHeight() && event.getY() >= 0)) {
82+
radius++;
83+
} else {
84+
isLastTouch = false;
85+
x = -1;
86+
y = -1;
87+
}
88+
}else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
89+
isLastTouch = false;
90+
x = -1;
91+
y = -1;
92+
}
93+
}
94+
return true;
2695
}
27-
28-
protected void onInitAttributes(AttributeSet attrs) {
29-
setAttributes(attrs);
96+
97+
@Override
98+
protected void onFocusChanged(boolean gainFocus, int direction,
99+
Rect previouslyFocusedRect) {
100+
if (!gainFocus) {
101+
x = -1;
102+
y = -1;
103+
}
30104
}
31-
32-
// ### RIPPLE EFFECT ###
33-
34-
/**
35-
* @return 涟漪的bitmap
36-
*/
105+
106+
@Override
107+
public boolean onInterceptTouchEvent(MotionEvent ev) {
108+
// super.onInterceptTouchEvent(ev);
109+
return true;
110+
}
111+
37112
public Bitmap makeCircle() {
38-
// 画涟漪时要考虑到按钮的边界区域,不要把按钮的阴影边界也填满了
39113
Bitmap output = Bitmap.createBitmap(
40-
getWidth() - Utils.dpToPx(6, getResources()),
41-
getHeight() - Utils.dpToPx(7, getResources()), Config.ARGB_8888);
42-
return makeCircleFromBitmap(output);
114+
getWidth() - Utils.dpToPx(6, getResources()), getHeight()
115+
- Utils.dpToPx(7, getResources()), Config.ARGB_8888);
116+
Canvas canvas = new Canvas(output);
117+
canvas.drawARGB(0, 0, 0, 0);
118+
Paint paint = new Paint();
119+
paint.setAntiAlias(true);
120+
paint.setColor(rippleColor);
121+
canvas.drawCircle(x, y, radius, paint);
122+
if (radius > getHeight() / rippleSize)
123+
radius += rippleSpeed;
124+
if (radius >= getWidth()) {
125+
x = -1;
126+
y = -1;
127+
radius = getHeight() / rippleSize;
128+
if (onClickListener != null)
129+
onClickListener.onClick(this);
130+
}
131+
return output;
43132
}
44-
45-
// Set color of background
133+
134+
/**
135+
* Make a dark color to ripple effect
136+
*
137+
* @return
138+
*/
139+
protected int makePressColor() {
140+
int r = (this.backgroundColor >> 16) & 0xFF;
141+
int g = (this.backgroundColor >> 8) & 0xFF;
142+
int b = (this.backgroundColor >> 0) & 0xFF;
143+
r = (r - 30 < 0) ? 0 : r - 30;
144+
g = (g - 30 < 0) ? 0 : g - 30;
145+
b = (b - 30 < 0) ? 0 : b - 30;
146+
return Color.rgb(r, g, b);
147+
}
148+
46149
@Override
150+
public void setOnClickListener(OnClickListener l) {
151+
onClickListener = l;
152+
}
153+
154+
// Set color of background
47155
public void setBackgroundColor(int color) {
48-
backgroundColor = color;
49-
if (isEnabled()) {
156+
this.backgroundColor = color;
157+
if (isEnabled())
50158
beforeBackground = backgroundColor;
51-
}
52159
try {
53160
LayerDrawable layer = (LayerDrawable) getBackground();
54-
// 每个按钮的框架都是由drawable中的xml文件制定的,xml文件中都有一个item的id叫:shape_bacground
55-
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
56-
/**
57-
* 给这个图片设置背景色,因为图片的主体是透明的所以可以直接显示背景色
58-
* 效果就是一个透明但有阴影的框架下有了背景色,这样的方式可以方便的设置不同颜色的按钮,让按钮看起来还是浑然一体
59-
*/
161+
GradientDrawable shape = (GradientDrawable) layer
162+
.findDrawableByLayerId(R.id.shape_bacground);
60163
shape.setColor(backgroundColor);
61-
/**
62-
* 当重新设定背景色后,要检查涟漪颜色。如果已经设定了涟漪颜色,那么就用之前的。如果没设定就重新生成
63-
*/
64-
if (!settedRippleColor) {
65-
rippleColor = makePressColor(255);
66-
}
164+
rippleColor = makePressColor();
67165
} catch (Exception ex) {
68166
// Without bacground
69167
}
70168
}
71169

72170
abstract public TextView getTextView();
73-
171+
172+
public void setRippleSpeed(float rippleSpeed) {
173+
this.rippleSpeed = rippleSpeed;
174+
}
175+
176+
public float getRippleSpeed() {
177+
return this.rippleSpeed;
178+
}
74179
}

0 commit comments

Comments
 (0)