WEB开发网
开发学院手机开发Android 开发 android 用三张图片实现七个音量等级的录音效果 阅读

android 用三张图片实现七个音量等级的录音效果

 2013-09-29 19:22:09 来源:WEB开发网   
核心提示: 很多有发送语音功能的即时通讯软件中,在录音时都有音量变化的效果,android 用三张图片实现七个音量等级的录音效果,如果QQ,微信,为了减少图片资源的容量,现在我们用三张图片来实现7个音量等级的效果,最近的易信,

 很多有发送语音功能的即时通讯软件中,在录音时都有音量变化的效果,如果QQ,微信,最近的易信。。。等等,若有7个音量的效果就需要7张图片,为了减少图片资源的容量,现在我们用三张图片来实现7个音量等级的效果。

实现重点:Region.Op.DIFFERENCE

看代码:

package com.open.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.open.widget.mic.R;

/**
 * 麦克风的录音效果
 * @author yanglonghui
 *
 */
public class AudioRecordMicView extends View{

	private Drawable micBgDrawable;
	private Drawable mic_1Drawable;
	private Drawable mic_2Drawable;
	private Rect micBgDrawableRect=new Rect();
	private Rect mic_1DrawableRect=new Rect();
	private Rect mic_2DrawableRect=new Rect();
	private Rect spaceRect=new Rect();
	private int maxLevel=7;
	
	public AudioRecordMicView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public AudioRecordMicView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public AudioRecordMicView(Context context) {
		super(context);
		init();
	}

	private void init()
	{
		micBgDrawable=getResources().getDrawable(R.drawable.tooltip_mic_bg);
		mic_1Drawable=getResources().getDrawable(R.drawable.tooltip_mic_1);
		mic_2Drawable=getResources().getDrawable(R.drawable.tooltip_mic_2);
	}
	
	/**
	 * 设置录音的级数,从1-7
	 * @param level
	 */
	public void setLevel(int level)
	{
		int progress=level*mic_2DrawableRect.height()/maxLevel;
		setProgress(progress);
	}
	
	public void setMaxLevel(int maxLevel)
	{
		this.maxLevel=maxLevel;
	}
	
	public int getMaxLevel()
	{
		return maxLevel;
	}
	 
	private void setProgress(int progress)
	{
		int bottom=0;
		if(progress>mic_2DrawableRect.height())
		{
			bottom=0;
		}
		else
		{
			bottom=mic_2DrawableRect.height()-progress;
		}
		spaceRect.set(mic_2DrawableRect.left, mic_2DrawableRect.top, mic_2DrawableRect.right, mic_2DrawableRect.top+bottom);
		invalidate();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(micBgDrawable.getIntrinsicWidth(), micBgDrawable.getIntrinsicHeight()); 
		
		micBgDrawableRect.set(0, 0, micBgDrawable.getIntrinsicWidth(), micBgDrawable.getIntrinsicHeight());
		micBgDrawable.setBounds(micBgDrawableRect);
		
		int left=(getMeasuredWidth()-mic_1Drawable.getIntrinsicWidth())/2;
		int top=(getMeasuredHeight()-mic_1Drawable.getIntrinsicHeight())/2;
		
		mic_1DrawableRect.set(left, top, left+mic_1Drawable.getIntrinsicWidth(), top+mic_1Drawable.getIntrinsicHeight());
		mic_1Drawable.setBounds(mic_1DrawableRect);
		
		mic_2DrawableRect.set(mic_1DrawableRect);
		mic_2Drawable.setBounds(mic_2DrawableRect);
		
		spaceRect.set(mic_1DrawableRect);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		micBgDrawable.draw(canvas);
		mic_1Drawable.draw(canvas);
		
        canvas.save();
        canvas.clipRect(mic_2DrawableRect);
        canvas.clipRect(spaceRect,Region.Op.DIFFERENCE);
        mic_2Drawable.draw(canvas);
        canvas.restore();
	}
}

Tags:android 图片 实现

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接