FileDocCategorySizeDatePackage
ColorButton.javaAPI DocAndroid 1.5 API4724Wed May 06 22:42:42 BST 2009com.android.calculator2

ColorButton

public class ColorButton extends android.widget.Button implements android.view.View.OnClickListener
Button 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
Constructors Summary
public ColorButton(android.content.Context context, android.util.AttributeSet attrs)

    
         
        super(context, attrs);
        init();
        mListener = ((Calculator) context).mListener;
        setOnClickListener(this);
    
Methods Summary
public voidanimateClickFeedback()

        mAnimStart = System.currentTimeMillis();
        invalidate();        
    
private voiddrawMagicFlame(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 voidinit()

        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 voidmeasureText()

        Paint paint = getPaint();
        mTextX = (getWidth() - paint.measureText(getText().toString())) / 2;
        mTextY = (getHeight() - paint.ascent() - paint.descent()) / 2;
    
public voidonClick(android.view.View view)

        animateClickFeedback();
        mListener.onClick(this);
    
public voidonDraw(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 voidonSizeChanged(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 voidonTextChanged(java.lang.CharSequence text, int start, int before, int after)

        measureText();
    
public booleanonTouchEvent(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);