Instantly share code, notes, and snippets.
Created
November 5, 2014 06:23
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save akoul889/1276365ab6756920ce97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gc.materialdesign.views; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
import com.gc.materialdesign.R; | |
import com.gc.materialdesign.utils.Utils; | |
/** | |
* Created by akshaykoul on 11/5/14. | |
*/ | |
public class ButtonFlatIcon extends Button{ | |
int sizeIcon = 24; | |
ImageView icon; // Icon of float button | |
Drawable drawableIcon; | |
public ButtonFlatIcon(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setBackgroundResource(R.drawable.background_button_float); | |
setDefaultProperties(); | |
icon = new ImageView(context); | |
if(drawableIcon != null) { | |
try { | |
icon.setBackground(drawableIcon); | |
} catch (NoSuchMethodError e) { | |
icon.setBackgroundDrawable(drawableIcon); | |
} | |
} | |
LayoutParams params = new LayoutParams(Utils.dpToPx(sizeIcon, getResources()),Utils.dpToPx(sizeIcon, getResources())); | |
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); | |
icon.setLayoutParams(params); | |
addView(icon); | |
} | |
protected void setDefaultProperties(){ | |
minHeight = 36; | |
minWidth = 88; | |
rippleSize = 3; | |
// Min size | |
setMinimumHeight(Utils.dpToPx(minHeight, getResources())); | |
setMinimumWidth(Utils.dpToPx(minWidth, getResources())); | |
setBackgroundResource(R.drawable.background_transparent); | |
} | |
protected void setAttributes(AttributeSet attrs){ | |
//Set background Color | |
// Color by resource | |
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1); | |
if(bacgroundColor != -1){ | |
setBackgroundColor(getResources().getColor(bacgroundColor)); | |
}else{ | |
// Color by hexadecimal | |
String background = attrs.getAttributeValue(ANDROIDXML,"background"); | |
if(background != null) | |
setBackgroundColor(Color.parseColor(background)); | |
} | |
// Icon of button | |
int iconResource = attrs.getAttributeResourceValue(MATERIALDESIGNXML,"iconFloat",-1); | |
if(iconResource != -1) | |
drawableIcon = getResources().getDrawable(iconResource); | |
// boolean animate = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,"animate", false); | |
// if(animate){ | |
// post(new Runnable() { | |
// | |
// @Override | |
// public void run() { | |
// float originalY = ViewHelper.getY(ButtonFlatIcon.this)-Utils.dpToPx(24, getResources()); | |
// ViewHelper.setY(ButtonFlatIcon.this,ViewHelper.getY(ButtonFlatIcon.this)+getHeight()*3); | |
// ObjectAnimator animator = ObjectAnimator.ofFloat(ButtonFlatIcon.this, "y", originalY); | |
// animator.setInterpolator(new BounceInterpolator()); | |
// animator.setDuration(1500); | |
// animator.start(); | |
// } | |
// }); | |
// } | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if (x != -1) { | |
Paint paint = new Paint(); | |
paint.setAntiAlias(true); | |
paint.setColor(makePressColor()); | |
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); | |
} | |
} | |
invalidate(); | |
} | |
/** | |
* Make a dark color to ripple effect | |
* @return | |
*/ | |
@Override | |
protected int makePressColor(){ | |
return Color.parseColor("#88DDDDDD"); | |
} | |
// public void setText(String text){ | |
// textButton.setText(text.toUpperCase()); | |
// } | |
// | |
// // Set color of background | |
// public void setBackgroundColor(int color){ | |
// textButton.setTextColor(color); | |
// } | |
@Override | |
public TextView getTextView() { | |
return null; | |
} | |
public ImageView getIcon() { | |
return icon; | |
} | |
public void setIcon(ImageView icon) { | |
this.icon = icon; | |
} | |
public Drawable getDrawableIcon() { | |
return drawableIcon; | |
} | |
public void setDrawableIcon(Drawable drawableIcon) { | |
this.drawableIcon = drawableIcon; | |
try { | |
icon.setBackground(drawableIcon); | |
} catch (NoSuchMethodError e) { | |
icon.setBackgroundDrawable(drawableIcon); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment