Anroid自定义可滑动的音量条

项目中,需要实现一个滑动调节音量的功能,需要使用android画图实现,怎奈画图平时使用的少,就晚上百度了一个demo,自己研究下,改进,实现功能,分享给大家。

直接上代码,自定义一个控件



public class SoundView extends View {

    private Context mContext;
    private Bitmap bm, bm1;// 画图
    private int bitmapWidth, bitmapHeight;
    private int index;
    private OnVolumeChangedListener mOnVolumeChangedListener;

    public static int HEIGHT = 14;// 单个音量条高度间隔
    private int MY_HEIGHT;

    private AudioManager mAudioManager;
    private MyVolumeReceiver myVolumeReceiver;

    public interface OnVolumeChangedListener {
        public void setYourVolume(int index);
    }

    public void setOnVolumeChangeListener(OnVolumeChangedListener l,
            int MY_HEIGHT) {
        mOnVolumeChangedListener = l;
        this.MY_HEIGHT = MY_HEIGHT;
    }

    public SoundView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public SoundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public SoundView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    private void init() {

        myVolumeReceiver = new MyVolumeReceiver();
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
        mContext.registerReceiver(myVolumeReceiver, mIntentFilter);

        mAudioManager = (AudioManager) mContext
                .getSystemService(Context.AUDIO_SERVICE);

        // 加载画图bitmap
        bm = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.sound_line);
        bm1 = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.sound_line1);
        bitmapWidth = bm.getWidth();
        bitmapHeight = bm.getHeight();

        AudioManager am = (AudioManager) mContext
                .getSystemService(Context.AUDIO_SERVICE);
        setIndex(am.getStreamVolume(AudioManager.STREAM_MUSIC));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        int n = y * 15 / MY_HEIGHT;// 控件总高度均分15份
        setIndex(15 - n);
        JwdLog.i("n:" + n + ";  y:" + y);
        return true;
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {

        int reverseIndex = 15 - index;

        // 从控件的左上开始画图,左上坐标是(0,0)右下为正
        for (int i = 0; i != reverseIndex; ++i) {// 灰色
            Rect mRect1 = new Rect(0, 0, bitmapWidth, bitmapHeight);//绘制的区域

            // i * HEIGHT + 6 顶部距离Y轴间距
            // bitmapHeight+6 底部距离Y轴间距
            Rect mRect2 = new Rect(0, i * HEIGHT + 6, bitmapWidth, i * HEIGHT
                    + bitmapHeight + 6);// 左侧X轴坐标,顶部Y轴坐标,右侧X坐标,底部Y轴坐标
            canvas.drawBitmap(bm1, mRect1, mRect2, null);
        }
        for (int i = reverseIndex; i != 15; ++i) {// 音量
            Rect mRect1 = new Rect(0, 0, bitmapWidth, bitmapHeight);
            Rect mRect2 = new Rect(0, i * HEIGHT + 6, bitmapWidth, i * HEIGHT
                    + bitmapHeight + 6);
            canvas.drawBitmap(bm, mRect1, mRect2, null);
        }

        super.onDraw(canvas);
    }

    /**
     * 
     * @author James
     * @Description 音量值
     * @param n
     */
    public void setIndex(int n) {
        if (n > 15) {
            n = 15;
        } else if (n < 0) {
            n = 0;
        }
        if (index != n) {
            index = n;
            if (mOnVolumeChangedListener != null) {
                mOnVolumeChangedListener.setYourVolume(n);// 回调显示音量系统UI
            }
        }
        invalidate();// 重新绘图,执行onDraw();
    }

    /**
     * 
     * @author James
     * @Package com.carboy.launch.widgets
     * @Title: SoundView.java
     * @Description 监听音量改变,改变音量条
     * @version 1.0
     * 
     */
    private class MyVolumeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction()
                    .equals("android.media.VOLUME_CHANGED_ACTION")) {
                int currVolume = mAudioManager
                        .getStreamVolume(AudioManager.STREAM_MUSIC);// 当前的媒体音量
                setIndex(currVolume);
            }
        }
    }

    /**
     * 
     * @author James
     * @Description 注销广播
     */
    public void unregister() {

        if (myVolumeReceiver != null) {
            mContext.unregisterReceiver(myVolumeReceiver);
        }
    }

}

效果图

你可能感兴趣的:(android移动开发)