|
public class CornerButton extends TextViewCustomFont { |
|
|
|
GradientDrawable mDrawable; |
|
private int mBgColor = Color.WHITE; |
|
private int mBgColorSelected = Color.WHITE; |
|
private int mBgColorDisabled = Color.LTGRAY; |
|
private boolean mSelected = false; |
|
private int mCornerRadius = 0; |
|
private int mStrokeWidth = 0; |
|
private int mStrokeColor = Color.TRANSPARENT; |
|
private Drawable mBackgroundDrawable; |
|
|
|
public CornerButton(Context context) { |
|
super(context); |
|
init(null); |
|
} |
|
|
|
public CornerButton(Context context, AttributeSet attrs) { |
|
super(context, attrs); |
|
init(attrs); |
|
} |
|
|
|
public CornerButton(Context context, AttributeSet attrs, int defStyleAttr) { |
|
super(context, attrs, defStyleAttr); |
|
init(attrs); |
|
} |
|
|
|
private void init(AttributeSet attrs) { |
|
mDrawable = new GradientDrawable(); |
|
|
|
// fetch attributes |
|
TypedArray a = getContext().getTheme() |
|
.obtainStyledAttributes(attrs, R.styleable.CornerButton, 0, 0); |
|
try { |
|
mBgColor = a.getColor(R.styleable.CornerButton_bgColor, mBgColor); |
|
mBgColorSelected = BitmapUtils.addColor(mBgColor, 0.85f); |
|
mBgColorDisabled = BitmapUtils.addAlpha(mBgColor, 0.65f); |
|
mBgColorSelected = |
|
a.getColor(R.styleable.CornerButton_bgColorSelected, mBgColorSelected); |
|
mBgColorDisabled = |
|
a.getColor(R.styleable.CornerButton_bgColorDisabled, mBgColorDisabled); |
|
mCornerRadius = a.getDimensionPixelSize(R.styleable.CornerButton_corner, mCornerRadius); |
|
mSelected = a.getBoolean(R.styleable.CornerButton_selected, mSelected); |
|
mStrokeWidth = a.getDimensionPixelSize(R.styleable.CornerButton_cornerStrokeWidth, |
|
mStrokeWidth); |
|
mStrokeColor = a.getColor(R.styleable.CornerButton_cornerStrokeColor, mStrokeColor); |
|
mBackgroundDrawable = a.getDrawable(R.styleable.CornerButton_bgDrawable); |
|
} finally { |
|
a.recycle(); |
|
} |
|
|
|
setClickable(true); |
|
|
|
refresh(); |
|
} |
|
|
|
public void setBackgroundColor(int color) { |
|
setBackgroundColor(color, color); |
|
} |
|
|
|
public void setBackgroundColor(int color, int colorSelected) { |
|
this.mBgColor = color; |
|
this.mBgColorSelected = colorSelected; |
|
refresh(); |
|
} |
|
|
|
public void setCorner(int corner) { |
|
this.mCornerRadius = corner; |
|
refresh(); |
|
} |
|
|
|
@Override |
|
public void setSelected(boolean selected) { |
|
super.setSelected(selected); |
|
this.mSelected = selected; |
|
refresh(); |
|
} |
|
|
|
@Override |
|
public void setEnabled(boolean enabled) { |
|
super.setEnabled(enabled); |
|
refresh(); |
|
} |
|
|
|
private void refresh() { |
|
if (!isEnabled()) { |
|
mDrawable.setColor(mBgColorDisabled); |
|
setBackground(mDrawable); |
|
} else if (mBackgroundDrawable != null) { |
|
setBackground(mBackgroundDrawable); |
|
} else { |
|
mDrawable.setColor(mSelected ? mBgColorSelected : mBgColor); |
|
mDrawable.setCornerRadius(mCornerRadius); |
|
mDrawable.setStroke(mStrokeWidth, mStrokeColor); |
|
mDrawable.setShape(GradientDrawable.RECTANGLE); |
|
setBackground(mDrawable); |
|
} |
|
invalidate(); |
|
} |
|
|
|
@Override |
|
public void setPressed(boolean pressed) { |
|
super.setPressed(pressed); |
|
this.mSelected = pressed; |
|
refresh(); |
|
} |
|
} |