1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.inputmethod.deprecated.voice;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.android.inputmethod.latin.R;
/**
* A widget which shows the volume of audio using a microphone icon
*/
public class SoundIndicator extends ImageView {
@SuppressWarnings("unused")
private static final String TAG = "SoundIndicator";
private static final float UP_SMOOTHING_FACTOR = 0.9f;
private static final float DOWN_SMOOTHING_FACTOR = 0.4f;
private static final float AUDIO_METER_MIN_DB = 7.0f;
private static final float AUDIO_METER_DB_RANGE = 20.0f;
private static final long FRAME_DELAY = 50;
private Bitmap mDrawingBuffer;
private Canvas mBufferCanvas;
private Bitmap mEdgeBitmap;
private float mLevel = 0.0f;
private Drawable mFrontDrawable;
private Paint mClearPaint;
private Paint mMultPaint;
private int mEdgeBitmapOffset;
private Handler mHandler;
private Runnable mDrawFrame = new Runnable() {
public void run() {
invalidate();
mHandler.postDelayed(mDrawFrame, FRAME_DELAY);
}
};
public SoundIndicator(Context context) {
this(context, null);
}
public SoundIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
mFrontDrawable = getDrawable();
BitmapDrawable edgeDrawable =
(BitmapDrawable) context.getResources().getDrawable(R.drawable.vs_popup_mic_edge);
mEdgeBitmap = edgeDrawable.getBitmap();
mEdgeBitmapOffset = mEdgeBitmap.getHeight() / 2;
mDrawingBuffer =
Bitmap.createBitmap(mFrontDrawable.getIntrinsicWidth(),
mFrontDrawable.getIntrinsicHeight(), Config.ARGB_8888);
mBufferCanvas = new Canvas(mDrawingBuffer);
// Initialize Paints.
mClearPaint = new Paint();
mClearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mMultPaint = new Paint();
mMultPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
mHandler = new Handler();
}
@Override
public void onDraw(Canvas canvas) {
//super.onDraw(canvas);
float w = getWidth();
float h = getHeight();
// Clear the buffer canvas
mBufferCanvas.drawRect(0, 0, w, h, mClearPaint);
// Set its clip so we don't draw the front image all the way to the top
Rect clip = new Rect(0,
(int) ((1.0 - mLevel) * (h + mEdgeBitmapOffset)) - mEdgeBitmapOffset,
(int) w,
(int) h);
mBufferCanvas.save();
mBufferCanvas.clipRect(clip);
// Draw the front image
mFrontDrawable.setBounds(new Rect(0, 0, (int) w, (int) h));
mFrontDrawable.draw(mBufferCanvas);
mBufferCanvas.restore();
// Draw the edge image on top of the buffer image with a multiply mode
mBufferCanvas.drawBitmap(mEdgeBitmap, 0, clip.top, mMultPaint);
// Draw the buffer image (on top of the background image)
canvas.drawBitmap(mDrawingBuffer, 0, 0, null);
}
/**
* Sets the sound level
*
* @param rmsdB The level of the sound, in dB.
*/
public void setRmsdB(float rmsdB) {
float level = ((rmsdB - AUDIO_METER_MIN_DB) / AUDIO_METER_DB_RANGE);
level = Math.min(Math.max(0.0f, level), 1.0f);
// We smooth towards the new level
if (level > mLevel) {
mLevel = (level - mLevel) * UP_SMOOTHING_FACTOR + mLevel;
} else {
mLevel = (level - mLevel) * DOWN_SMOOTHING_FACTOR + mLevel;
}
invalidate();
}
public void start() {
mHandler.post(mDrawFrame);
}
public void stop() {
mHandler.removeCallbacks(mDrawFrame);
}
}
|