attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources><!--图标颜色--><attr name="ijkSolid" format="color|reference" /><!--喇叭底座宽度--><attr name="ijkCornerWidth" format="dimension|reference" /><!--喇叭底座高度--><attr name="ijkCornerHeight" format="dimension|reference" /><!--音量线条宽度--><attr name="ijkStrokeWidth" format="dimension|reference" /><!--音量线条角度--><attr name="ijkAngle" format="integer|reference" /><!--音量线条数量--><attr name="ijkCount" format="integer|reference" /><!--喇叭底座X--><attr name="ijkLeftCenterX" format="integer|reference" /><!--视频音量--><declare-styleable name="VideoVoice"><!--图标颜色--><attr name="ijkSolid" /><!--喇叭底座宽度--><attr name="ijkCornerWidth" /><!--喇叭底座高度--><attr name="ijkCornerHeight" /><!--音量线条宽度--><attr name="ijkStrokeWidth" /><!--音量线条角度--><attr name="ijkAngle" /><!--音量线条数量--><attr name="ijkCount" /><!--喇叭底座X--><attr name="ijkLeftCenterX" /></declare-styleable>
</resources>
源码
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;import androidx.annotation.Nullable;
import androidx.ijk.R;/*** 视频亮度图标*/
public class VideoBrightness extends View {private Paint paint;/*** 填充颜色*/private int solid = Color.CYAN;private int solidClone = solid;/*** 中心x*/private float cx;/*** 中心y*/private float cy;/*** 半径*/private float radius = 0;/*** 直径*/private float diameter = 0;/*** 线条长度*/private float lineLength = 0;/*** 线条和中心圆间距*/private int gap = 20;/*** 线条宽度*/private float lineWidth = 5;public VideoBrightness(Context context) {this(context, null);}public VideoBrightness(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public VideoBrightness(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);if (attrs != null) {TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.VideoBrightness);solid = solidClone = array.getColor(R.styleable.VideoBrightness_ijkSolid, solid);radius = array.getDimension(R.styleable.VideoBrightness_ijkRadius, radius);lineLength = array.getDimension(R.styleable.VideoBrightness_ijkLineLength, lineLength);gap = array.getDimensionPixelSize(R.styleable.VideoBrightness_ijkGap, gap);lineWidth = array.getDimension(R.styleable.VideoBrightness_ijkLineWidth, lineWidth);array.recycle();}paint = new Paint();paint.setAntiAlias(true);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);cx = getMeasuredWidth() / 2;cy = getMeasuredHeight() / 2;diameter = Math.min(getMeasuredHeight(), getMeasuredWidth());radius = radius == 0 ? diameter / 6 : radius;lineLength = lineLength == 0 ? (diameter / 12 - gap) : lineLength;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);paint.setColor(solid);paint.setStyle(Paint.Style.FILL);canvas.drawCircle(cx, cy, radius, paint);paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(lineWidth);paint.setStrokeCap(Paint.Cap.ROUND);float length = radius + gap;for (int angle = 0; angle < 360; angle += 45) {double radians = Math.toRadians(angle);float startX = cx + (float) (length * Math.cos(radians));float startY = cy + (float) (length * Math.sin(radians));float endX = cx + (float) ((length + lineLength) * Math.cos(radians));float endY = cy + (float) ((length + lineLength) * Math.sin(radians));canvas.drawLine(startX, startY, endX, endY, paint);}}/*** 设置填充颜色** @param solid*/public void setSolid(int solid) {this.solid = solidClone = solid;invalidate();}/*** 设置填充颜色值百分比** @param percent*/public void setSolidAlpha(float percent) {percent = percent < 0.25f ? 0.25f : percent;int alpha = Color.alpha(solidClone);int red = Color.red(solidClone);int green = Color.green(solidClone);int blue = Color.blue(solidClone);alpha *= percent;solid = alpha << 24 | red << 16 | green << 8 | blue;invalidate();}/*** 设置半径大小** @param radius*/public void setRadius(float radius) {this.radius = radius;invalidate();}/*** 设置线段长度** @param lineLength*/public void setLineLength(float lineLength) {this.lineLength = lineLength;invalidate();}/*** 设置线段和圆圈间距** @param gap*/public void setGap(int gap) {this.gap = gap;invalidate();}/*** 设置线段宽度** @param lineWidth*/public void setLineWidth(float lineWidth) {this.lineWidth = lineWidth;invalidate();}}