Android 视频亮度图标


attrs.xml

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--图标颜色-->
    <attr name="ijkSolid" format="color|reference" />
    <!--圆角大小-->
    <attr name="ijkRadius" format="dimension|reference" />
    <!--线条长度-->
    <attr name="ijkLineLength" format="dimension|reference" />
    <!--线条和圆圈间距-->
    <attr name="ijkGap" format="dimension|reference" />
    <!--线条宽度-->
    <attr name="ijkLineWidth" format="dimension|reference" />
    <!--视频亮度-->
    <declare-styleable name="VideoBrightness">
        <!--图标颜色-->
        <attr name="ijkSolid" />
        <!--圆角大小-->
        <attr name="ijkRadius" />
        <!--线条长度-->
        <attr name="ijkLineLength" />
        <!--线条和圆圈间距-->
        <attr name="ijkGap" />
        <!--线条宽度-->
        <attr name="ijkLineWidth" />
    </declare-styleable>
</resources>

源码

java 复制代码
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 lineLength = 0;
    private float lineLengthClone;
    /**
     * 线条和中心圆间距
     */
    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);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        cx = getMeasuredWidth() / 2;
        cy = getMeasuredHeight() / 2;
        int slide = Math.min(getMeasuredHeight(), getMeasuredWidth());
        radius = radius == 0 ? slide / 6 : radius;
        lineLength = lineLength == 0 ? (radius / 2 - gap) : lineLength;
    }

    @Override
    protected 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 setValue(float percent) {
        setRotation(percent * 360);//旋转
        //线条长度
        lineLengthClone = lineLengthClone == 0 ? lineLength : lineLengthClone;
        lineLength = lineLengthClone * percent;
        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();
    }

}
相关推荐
闲暇部落2 小时前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
诸神黄昏EX4 小时前
Android 分区相关介绍
android
大白要努力!5 小时前
android 使用SQLiteOpenHelper 如何优化数据库的性能
android·数据库·oracle
Estar.Lee5 小时前
时间操作[取当前北京时间]免费API接口教程
android·网络·后端·网络协议·tcp/ip
Winston Wood5 小时前
Perfetto学习大全
android·性能优化·perfetto
Dnelic-8 小时前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen10 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年18 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿20 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神21 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri