blob: c9bab401db98f236cae1937105ac9d8fccadd53a [file] [log] [blame]
The Android Open Source Projectb301ed22009-03-03 19:32:17 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.calculator2;
18
19import android.content.Context;
Mindy Pereirac0c011f2011-12-05 14:30:51 -080020import android.content.res.Resources;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080021import android.graphics.Canvas;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080022import android.graphics.Paint;
Jacek Surazski1d311752009-08-25 17:33:12 +020023import android.graphics.Paint.Style;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080024import android.util.AttributeSet;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080025import android.view.View.OnClickListener;
26import android.view.View;
27import android.view.MotionEvent;
Mindy Pereirac0c011f2011-12-05 14:30:51 -080028import android.widget.Button;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080029
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080030/**
31 * Button with click-animation effect.
32 */
33class ColorButton extends Button implements OnClickListener {
34 int CLICK_FEEDBACK_COLOR;
35 static final int CLICK_FEEDBACK_INTERVAL = 10;
36 static final int CLICK_FEEDBACK_DURATION = 350;
Dmitri Plotnikov9476df22011-01-17 14:46:51 -080037
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080038 float mTextX;
39 float mTextY;
40 long mAnimStart;
41 OnClickListener mListener;
Jacek Surazski1d311752009-08-25 17:33:12 +020042 Paint mFeedbackPaint;
Dmitri Plotnikov9476df22011-01-17 14:46:51 -080043
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080044 public ColorButton(Context context, AttributeSet attrs) {
45 super(context, attrs);
Jacek Surazski1d311752009-08-25 17:33:12 +020046 Calculator calc = (Calculator) context;
47 init(calc);
48 mListener = calc.mListener;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080049 setOnClickListener(this);
50 }
51
52 public void onClick(View view) {
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080053 mListener.onClick(this);
54 }
55
Jacek Surazski1d311752009-08-25 17:33:12 +020056 private void init(Calculator calc) {
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080057 Resources res = getResources();
58
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080059 CLICK_FEEDBACK_COLOR = res.getColor(R.color.magic_flame);
Jacek Surazski1d311752009-08-25 17:33:12 +020060 mFeedbackPaint = new Paint();
61 mFeedbackPaint.setStyle(Style.STROKE);
62 mFeedbackPaint.setStrokeWidth(2);
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080063 getPaint().setColor(res.getColor(R.color.button_text));
Jacek Surazski1d311752009-08-25 17:33:12 +020064
Dmitri Plotnikov9476df22011-01-17 14:46:51 -080065 mAnimStart = -1;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080066 }
67
68
Dmitri Plotnikov9476df22011-01-17 14:46:51 -080069 @Override
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080070 public void onSizeChanged(int w, int h, int oldW, int oldH) {
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080071 measureText();
72 }
73
74 private void measureText() {
75 Paint paint = getPaint();
76 mTextX = (getWidth() - paint.measureText(getText().toString())) / 2;
77 mTextY = (getHeight() - paint.ascent() - paint.descent()) / 2;
78 }
79
80 @Override
81 protected void onTextChanged(CharSequence text, int start, int before, int after) {
82 measureText();
83 }
84
85 private void drawMagicFlame(int duration, Canvas canvas) {
86 int alpha = 255 - 255 * duration / CLICK_FEEDBACK_DURATION;
87 int color = CLICK_FEEDBACK_COLOR | (alpha << 24);
Jacek Surazski1d311752009-08-25 17:33:12 +020088
89 mFeedbackPaint.setColor(color);
90 canvas.drawRect(1, 1, getWidth() - 1, getHeight() - 1, mFeedbackPaint);
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080091 }
92
93 @Override
94 public void onDraw(Canvas canvas) {
95 if (mAnimStart != -1) {
96 int animDuration = (int) (System.currentTimeMillis() - mAnimStart);
Dmitri Plotnikov9476df22011-01-17 14:46:51 -080097
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080098 if (animDuration >= CLICK_FEEDBACK_DURATION) {
The Android Open Source Projectb301ed22009-03-03 19:32:17 -080099 mAnimStart = -1;
100 } else {
101 drawMagicFlame(animDuration, canvas);
102 postInvalidateDelayed(CLICK_FEEDBACK_INTERVAL);
103 }
104 } else if (isPressed()) {
105 drawMagicFlame(0, canvas);
106 }
Dmitri Plotnikov9476df22011-01-17 14:46:51 -0800107
The Android Open Source Projectb301ed22009-03-03 19:32:17 -0800108 CharSequence text = getText();
109 canvas.drawText(text, 0, text.length(), mTextX, mTextY, getPaint());
110 }
111
112 public void animateClickFeedback() {
113 mAnimStart = System.currentTimeMillis();
Dmitri Plotnikov9476df22011-01-17 14:46:51 -0800114 invalidate();
115 }
116
The Android Open Source Projectb301ed22009-03-03 19:32:17 -0800117 @Override
118 public boolean onTouchEvent(MotionEvent event) {
Jacek Surazski1d311752009-08-25 17:33:12 +0200119 boolean result = super.onTouchEvent(event);
120
121 switch (event.getAction()) {
122 case MotionEvent.ACTION_UP:
Michael Jurka57ba0ae2011-10-19 07:15:35 -0700123 if (isPressed()) {
124 animateClickFeedback();
125 } else {
126 invalidate();
127 }
Jacek Surazski1d311752009-08-25 17:33:12 +0200128 break;
129 case MotionEvent.ACTION_DOWN:
130 case MotionEvent.ACTION_CANCEL:
Michael Jurkaf3197952012-02-29 15:10:49 -0800131 mAnimStart = -1;
Jacek Surazski1d311752009-08-25 17:33:12 +0200132 invalidate();
133 break;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -0800134 }
Jacek Surazski1d311752009-08-25 17:33:12 +0200135
136 return result;
The Android Open Source Projectb301ed22009-03-03 19:32:17 -0800137 }
138}