Android之定制音量进度条控件

直接上代码和图片
请添加图片描述
请添加图片描述

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;




public class VolumeProgressView extends View {
    private Paint circlePaint;
    private Paint progressPaint;
    private RectF circleBounds;

    private Drawable volumeIcon;
    private Drawable muteIcon;


    private Paint shadowPaint;

    private float volumeLevel = 0.5f; // 0.0~1.0
    private boolean isMuted = false;

    public VolumeProgressView(Context context) {
        this(context,null);

    }

    public VolumeProgressView(Context context,  AttributeSet attrs) {
        this(context, attrs,0);

    }

    public VolumeProgressView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VolumeProgressView, 0, 0);
        float strokeWidth = a.getDimension(R.styleable.VolumeProgressView_strokeW, 30);


        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(strokeWidth-10);
        circlePaint.setColor(context.getResources().getColor(R.color.volume_darker_gray));

        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(strokeWidth);
        progressPaint.setColor(context.getResources().getColor(R.color.volume_blue_dark));
        progressPaint.setStrokeCap(Paint.Cap.ROUND);

        shadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        shadowPaint.setStyle(Paint.Style.STROKE);
        shadowPaint.setStrokeWidth(strokeWidth);
        shadowPaint.setColor(context.getResources().getColor(R.color.volume_blue_dark));
        shadowPaint.setShadowLayer(10, 0, 0, Color.BLACK);
        shadowPaint.setStrokeCap(Paint.Cap.ROUND);


        setLayerType(LAYER_TYPE_SOFTWARE, shadowPaint);

        circleBounds = new RectF();

        volumeIcon = getDrawable(context, R.drawable.ic_volume_up);
        muteIcon = getDrawable(context, R.drawable.ic_volume_off);
        a.recycle();
    }

    public static Drawable getDrawable( Context context,  int id) {
        if (Build.VERSION.SDK_INT >= 21) {
            return context.getDrawable(id);
        } else if (Build.VERSION.SDK_INT >= 16) {
            return context.getResources().getDrawable(id);
        }
        return context.getResources().getDrawable(id);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        float padding = progressPaint.getStrokeWidth() ;
        circleBounds.set(padding, padding, w - padding, h - padding);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        canvas.drawOval(circleBounds, circlePaint);


        float shadowSweepAngle = 360 * volumeLevel;
        canvas.drawArc(circleBounds, -90, shadowSweepAngle, false, shadowPaint);


        float sweepAngle = 360 * volumeLevel;
        canvas.drawArc(circleBounds, -90, sweepAngle, false, progressPaint);


        Drawable icon = isMuted ? muteIcon : volumeIcon;
        if (icon != null) {
            int iconSize = Math.min(getWidth(), getHeight()) / 4;
            int left = getWidth() / 2 - iconSize / 2;
            int top = getHeight() / 2 - iconSize / 2;
            icon.setBounds(left, top, left + iconSize, top + iconSize);
            icon.draw(canvas);
        }
    }

    public void setVolumeLevel(float level1) {
        float level=level1/100;
        if (level < 0) level = 0;
        if (level > 1) level = 1;
        volumeLevel = level;
        isMuted = level == 0;
        invalidate();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值