Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#org.springsource.ide.eclipse.gradle.core.preferences.GradleImportPreferences
#Sun Jan 11 18:49:20 CET 2015
addResourceFilters=true
afterTasks=afterEclipseImport;
beforeTasks=cleanEclipse;eclipse;
enableAfterTasks=true
enableBeforeTasks=true
enableDSLD=false
enableDependendencyManagement=true
projects=MaterialDesign;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences
#Sun Jan 11 18:49:02 CET 2015
build.family.org.gradle.tooling.model.eclipse.HierarchicalEclipseProject=;MaterialDesign;MaterialDesignDemo;
org.springsource.ide.eclipse.gradle.rootprojectloc=
4 changes: 2 additions & 2 deletions MaterialDesign/.classpath
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="lib" path="libs/nineoldandroids-2.4.0.jar"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
Expand Down
3 changes: 2 additions & 1 deletion MaterialDesign/.project
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.springsource.ide.eclipse.gradle.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences
#Sun Jan 11 18:49:24 CET 2015
org.springsource.ide.eclipse.gradle.linkedresources=
org.springsource.ide.eclipse.gradle.rootprojectloc=..
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#org.springsource.ide.eclipse.gradle.core.actions.GradleRefreshPreferences
#Sun Jan 11 18:49:23 CET 2015
addResourceFilters=true
afterTasks=afterEclipseImport;
beforeTasks=cleanEclipse;eclipse;
enableAfterTasks=true
enableBeforeTasks=true
enableDSLD=false
useHierarchicalNames=false
8 changes: 4 additions & 4 deletions MaterialDesign/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName '1.0'
versionCode 2
versionName '1.1'
}
}

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

bintray {
user = hasProperty('BINTRAY_USER') ? BINTRAY_USER : "_"
key = hasProperty('BINTRAY_KEY') ? BINTRAY_PASSWORD : "_"
user = hasProperty('BINTRAY_USER') ? BINTRAY_USER : ""
key = hasProperty('BINTRAY_KEY') ? BINTRAY_PASSWORD : ""

configurations = ["archives"]
pkg {
Expand Down
2 changes: 1 addition & 1 deletion MaterialDesign/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-8
target=android-19
android.library=true
31 changes: 5 additions & 26 deletions MaterialDesign/src/com/gc/materialdesign/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gc.materialdesign.utils;

import android.content.res.Resources;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;

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

/**
* Convert Dp to Pixel
* 将dp转换为pixel
*/
public static int dpToPx(float dp, Resources resources){
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
return (int) px;
}

/**
* @param value
* @return 将dip或者dp转为float
*/
public static float dipOrDpToFloat(String value) {
if (value.indexOf("dp") != -1) {
value = value.replace("dp", "");
}
else {
value = value.replace("dip", "");
}
return Float.parseFloat(value);
}


/**
* 这里看似是得到控件相对的坐标,但是如果这个滑动条在可以上下滚动的布局中就会出现问题。
* 因为这里的坐标都是死的,在上下滚动的view中父控件的top仍旧不变,但实际上是应该获得动态数值的。
* @param myView
* @return
*/
public static int getRelativeTop(View myView) {
Rect bounds = new Rect();
myView.getGlobalVisibleRect(bounds);
return bounds.top;
// if (myView.getParent() == myView.getRootView())
if(myView.getId() == android.R.id.content)
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());
}

public static int getRelativeLeft(View myView) {
Expand Down
183 changes: 144 additions & 39 deletions MaterialDesign/src/com/gc/materialdesign/views/Button.java
Original file line number Diff line number Diff line change
@@ -1,74 +1,179 @@
package com.gc.materialdesign.views;

import com.gc.materialdesign.R;
import com.gc.materialdesign.utils.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import com.gc.materialdesign.R;
import com.gc.materialdesign.utils.Utils;
public abstract class Button extends CustomView {

public abstract class Button extends RippleView {
final static String ANDROIDXML = "http://schemas.android.com/apk/res/android";

// Complete in child class
int minWidth;
int minHeight;
int background;
float rippleSpeed = 10f;
int rippleSize = 3;
Integer rippleColor;
OnClickListener onClickListener;
int backgroundColor = Color.parseColor("#1E88E5");

public Button(Context context, AttributeSet attrs) {
super(context, attrs);
onInitAttributes(attrs);
setDefaultProperties();
setAttributes(attrs);
beforeBackground = backgroundColor;
if(rippleColor==null)
rippleColor = makePressColor();
}

protected void setDefaultProperties() {
// Min size
setMinimumHeight(Utils.dpToPx(minHeight, getResources()));
setMinimumWidth(Utils.dpToPx(minWidth, getResources()));
// Background shape
setBackgroundResource(background);
setBackgroundColor(backgroundColor);
}


// Set atributtes of XML to View
abstract protected void setAttributes(AttributeSet attrs);

// ### RIPPLE EFFECT ###

float x = -1, y = -1;
float radius = -1;

@Override
protected void onInitDefaultValues() {
backgroundColor = Color.parseColor("#2196f3");// 默认的背景色,蓝色
///beforeBackground = backgroundColor;// error
public boolean onTouchEvent(MotionEvent event) {
invalidate();
if (isEnabled()) {
isLastTouch = true;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
radius = getHeight() / rippleSize;
x = event.getX();
y = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
radius = getHeight() / rippleSize;
x = event.getX();
y = event.getY();
if (!((event.getX() <= getWidth() && event.getX() >= 0) && (event
.getY() <= getHeight() && event.getY() >= 0))) {
isLastTouch = false;
x = -1;
y = -1;
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if ((event.getX() <= getWidth() && event.getX() >= 0)
&& (event.getY() <= getHeight() && event.getY() >= 0)) {
radius++;
} else {
isLastTouch = false;
x = -1;
y = -1;
}
}else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
isLastTouch = false;
x = -1;
y = -1;
}
}
return true;
}

protected void onInitAttributes(AttributeSet attrs) {
setAttributes(attrs);

@Override
protected void onFocusChanged(boolean gainFocus, int direction,
Rect previouslyFocusedRect) {
if (!gainFocus) {
x = -1;
y = -1;
}
}

// ### RIPPLE EFFECT ###

/**
* @return 涟漪的bitmap
*/

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// super.onInterceptTouchEvent(ev);
return true;
}

public Bitmap makeCircle() {
// 画涟漪时要考虑到按钮的边界区域,不要把按钮的阴影边界也填满了
Bitmap output = Bitmap.createBitmap(
getWidth() - Utils.dpToPx(6, getResources()),
getHeight() - Utils.dpToPx(7, getResources()), Config.ARGB_8888);
return makeCircleFromBitmap(output);
getWidth() - Utils.dpToPx(6, getResources()), getHeight()
- Utils.dpToPx(7, getResources()), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawARGB(0, 0, 0, 0);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(rippleColor);
canvas.drawCircle(x, y, radius, paint);
if (radius > getHeight() / rippleSize)
radius += rippleSpeed;
if (radius >= getWidth()) {
x = -1;
y = -1;
radius = getHeight() / rippleSize;
if (onClickListener != null)
onClickListener.onClick(this);
}
return output;
}

// Set color of background

/**
* Make a dark color to ripple effect
*
* @return
*/
protected int makePressColor() {
int r = (this.backgroundColor >> 16) & 0xFF;
int g = (this.backgroundColor >> 8) & 0xFF;
int b = (this.backgroundColor >> 0) & 0xFF;
r = (r - 30 < 0) ? 0 : r - 30;
g = (g - 30 < 0) ? 0 : g - 30;
b = (b - 30 < 0) ? 0 : b - 30;
return Color.rgb(r, g, b);
}

@Override
public void setOnClickListener(OnClickListener l) {
onClickListener = l;
}

// Set color of background
public void setBackgroundColor(int color) {
backgroundColor = color;
if (isEnabled()) {
this.backgroundColor = color;
if (isEnabled())
beforeBackground = backgroundColor;
}
try {
LayerDrawable layer = (LayerDrawable) getBackground();
// 每个按钮的框架都是由drawable中的xml文件制定的,xml文件中都有一个item的id叫:shape_bacground
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
/**
* 给这个图片设置背景色,因为图片的主体是透明的所以可以直接显示背景色
* 效果就是一个透明但有阴影的框架下有了背景色,这样的方式可以方便的设置不同颜色的按钮,让按钮看起来还是浑然一体
*/
GradientDrawable shape = (GradientDrawable) layer
.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
/**
* 当重新设定背景色后,要检查涟漪颜色。如果已经设定了涟漪颜色,那么就用之前的。如果没设定就重新生成
*/
if (!settedRippleColor) {
rippleColor = makePressColor(255);
}
rippleColor = makePressColor();
} catch (Exception ex) {
// Without bacground
}
}

abstract public TextView getTextView();


public void setRippleSpeed(float rippleSpeed) {
this.rippleSpeed = rippleSpeed;
}

public float getRippleSpeed() {
return this.rippleSpeed;
}
}
Loading