Home | History | Annotate | Download | only in soundrecorder
      1 package com.android.soundrecorder;
      2 
      3 import java.util.Map;
      4 
      5 import android.content.Context;
      6 import android.graphics.Canvas;
      7 import android.graphics.Color;
      8 import android.graphics.Paint;
      9 import android.graphics.drawable.Drawable;
     10 import android.util.AttributeSet;
     11 import android.view.View;
     12 
     13 public class VUMeter extends View {
     14     static final float PIVOT_RADIUS = 3.5f;
     15     static final float PIVOT_Y_OFFSET = 10f;
     16     static final float SHADOW_OFFSET = 2.0f;
     17     static final float DROPOFF_STEP = 0.18f;
     18     static final float SURGE_STEP = 0.35f;
     19     static final long  ANIMATION_INTERVAL = 70;
     20 
     21     Paint mPaint, mShadow;
     22     float mCurrentAngle;
     23 
     24     Recorder mRecorder;
     25 
     26     public VUMeter(Context context) {
     27         super(context);
     28         init(context);
     29     }
     30 
     31     public VUMeter(Context context, AttributeSet attrs) {
     32         super(context, attrs);
     33         init(context);
     34     }
     35 
     36     void init(Context context) {
     37         Drawable background = context.getResources().getDrawable(R.drawable.vumeter);
     38         setBackgroundDrawable(background);
     39 
     40         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
     41         mPaint.setColor(Color.WHITE);
     42         mShadow = new Paint(Paint.ANTI_ALIAS_FLAG);
     43         mShadow.setColor(Color.argb(60, 0, 0, 0));
     44 
     45         mRecorder = null;
     46 
     47         mCurrentAngle = 0;
     48     }
     49 
     50     public void setRecorder(Recorder recorder) {
     51     	mRecorder = recorder;
     52     	invalidate();
     53     }
     54 
     55     @Override
     56     protected void onDraw(Canvas canvas) {
     57         super.onDraw(canvas);
     58 
     59         final float minAngle = (float)Math.PI/8;
     60         final float maxAngle = (float)Math.PI*7/8;
     61 
     62         float angle = minAngle;
     63         if (mRecorder != null)
     64         	angle += (float)(maxAngle - minAngle)*mRecorder.getMaxAmplitude()/32768;
     65 
     66         if (angle > mCurrentAngle)
     67             mCurrentAngle = angle;
     68         else
     69             mCurrentAngle = Math.max(angle, mCurrentAngle - DROPOFF_STEP);
     70 
     71         mCurrentAngle = Math.min(maxAngle, mCurrentAngle);
     72 
     73         float w = getWidth();
     74         float h = getHeight();
     75         float pivotX = w/2;
     76         float pivotY = h - PIVOT_RADIUS - PIVOT_Y_OFFSET;
     77         float l = h*4/5;
     78         float sin = (float) Math.sin(mCurrentAngle);
     79         float cos = (float) Math.cos(mCurrentAngle);
     80         float x0 = pivotX - l*cos;
     81         float y0 = pivotY - l*sin;
     82         canvas.drawLine(x0 + SHADOW_OFFSET, y0 + SHADOW_OFFSET, pivotX + SHADOW_OFFSET, pivotY + SHADOW_OFFSET, mShadow);
     83         canvas.drawCircle(pivotX + SHADOW_OFFSET, pivotY + SHADOW_OFFSET, PIVOT_RADIUS, mShadow);
     84         canvas.drawLine(x0, y0, pivotX, pivotY, mPaint);
     85         canvas.drawCircle(pivotX, pivotY, PIVOT_RADIUS, mPaint);
     86 
     87         if (mRecorder != null && mRecorder.state() == Recorder.RECORDING_STATE)
     88         	postInvalidateDelayed(ANIMATION_INTERVAL);
     89     }
     90 }
     91