直接上代码和图片
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();
}
}