ColorButtonpublic class ColorButton extends android.widget.Button implements android.view.View.OnClickListenerButton with click-animation effect. |
Fields Summary |
---|
int | CLICK_FEEDBACK_COLOR | static final int | CLICK_FEEDBACK_INTERVAL | static final int | CLICK_FEEDBACK_DURATION | android.graphics.drawable.Drawable | mButtonBackground | android.graphics.drawable.Drawable | mButton | float | mTextX | float | mTextY | long | mAnimStart | android.view.View.OnClickListener | mListener |
Methods Summary |
---|
public void | animateClickFeedback()
mAnimStart = System.currentTimeMillis();
invalidate();
| private void | drawMagicFlame(int duration, android.graphics.Canvas canvas)
int alpha = 255 - 255 * duration / CLICK_FEEDBACK_DURATION;
int color = CLICK_FEEDBACK_COLOR | (alpha << 24);
mButtonBackground.setColorFilter(color, PorterDuff.Mode.SRC_IN);
int cx = getWidth() / 2;
int cy = getHeight() / 2;
float angle = 250.0f * duration / CLICK_FEEDBACK_DURATION;
canvas.rotate(angle, cx, cy);
mButtonBackground.draw(canvas);
canvas.rotate(-angle, cx, cy);
| private void | init()
setBackgroundDrawable(null);
Resources res = getResources();
mButtonBackground = res.getDrawable(R.drawable.button_bg);
mButton = res.getDrawable(R.drawable.button);
CLICK_FEEDBACK_COLOR = res.getColor(R.color.magic_flame);
getPaint().setColor(res.getColor(R.color.button_text));
mAnimStart = -1;
| private void | measureText()
Paint paint = getPaint();
mTextX = (getWidth() - paint.measureText(getText().toString())) / 2;
mTextY = (getHeight() - paint.ascent() - paint.descent()) / 2;
| public void | onClick(android.view.View view)
animateClickFeedback();
mListener.onClick(this);
| public void | onDraw(android.graphics.Canvas canvas)
if (mAnimStart != -1) {
int animDuration = (int) (System.currentTimeMillis() - mAnimStart);
if (animDuration >= CLICK_FEEDBACK_DURATION) {
mButtonBackground.clearColorFilter();
mAnimStart = -1;
} else {
drawMagicFlame(animDuration, canvas);
postInvalidateDelayed(CLICK_FEEDBACK_INTERVAL);
}
} else if (isPressed()) {
drawMagicFlame(0, canvas);
}
mButton.draw(canvas);
CharSequence text = getText();
canvas.drawText(text, 0, text.length(), mTextX, mTextY, getPaint());
| public void | onSizeChanged(int w, int h, int oldW, int oldH)
int selfW = mButton.getIntrinsicWidth();
int selfH = mButton.getIntrinsicHeight();
int marginX = (w - selfW) / 2;
int marginY = (h - selfH) / 2;
mButtonBackground.setBounds(marginX, marginY, marginX + selfW, marginY + selfH);
mButton.setBounds(marginX, marginY, marginX + selfW, marginY + selfH);
measureText();
| protected void | onTextChanged(java.lang.CharSequence text, int start, int before, int after)
measureText();
| public boolean | onTouchEvent(android.view.MotionEvent event)
int a = event.getAction();
if (a == MotionEvent.ACTION_DOWN
|| a == MotionEvent.ACTION_CANCEL
|| a == MotionEvent.ACTION_UP) {
invalidate();
}
return super.onTouchEvent(event);
|
|