视频直播系统源码,使用自定义UI 完成文字颜色加载效果

视频直播系统源码,使用自定义UI 完成文字颜色加载效果

1、自定义文本属性

在项目attrs.xml 文件中 res -> values -> attrs.xml,自定义文本属性,没有这个文件,就新建一个。

 

1
<br><?xml version="1.0" encoding="utf-8"?><br><resources><br>    <declare-styleable name="ColorTrackTextView"><br>        <attr name="originColor" format="color" /><br>        <attr name="changeColor" format="color" /><br>    </declare-styleable><br></resources>

​2、创建组件

新建一个 ColorTrackTextView 文件,继承自 AppCompatTextView 这里也可以直接继承View,我只需要自定义TextView ,就直接继承 AppCompatTextView

 

1
<br>public class ColorTrackTextView extends AppCompatTextView {<br>//    绘制不变色字体的画笔<br>    private Paint mOriginPaint;<br>//    绘制变色字体的画笔<br>    private Paint mChangePaint;<br>    public ColorTrackTextView(@NonNull Context context) {<br>        this(context, null);<br>    }<br>    public ColorTrackTextView(@NonNull Context context, @Nullable AttributeSet attrs) {<br>        this(context, attrs, 0);<br>    }<br>    public ColorTrackTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {<br>        super(context, attrs, defStyleAttr);<br>    }<br>}

3、在 xml 中使用组件

 

1
<com.traveleasy.leaningui.ColorTrackTextView<br>       android:id="@+id/color_track_tv"<br>       android:text="Hello Word"<br>       android:textSize="20sp"<br>       app:changeColor="@color/teal_200"<br>       android:layout_width="match_parent"<br>       android:layout_height="wrap_content"<br>       tools:ignore="MissingConstraints" />

上面的三步中,已经完成了自定义TextView 的属性设置,以及在 xml 中使用,接下来开始做测量绘制的效果

4、测量

这里我直接继承的是 AppCompatTextView, 所以就直接跳过了测量部分。当然,如果继承的是View,也只需要测量自己就可以了

 

5、绘制

接下来通过 onDraw:绘制,在此之前,我们需要先创建一个画笔

 

1
<br>  /**<br>     * 根据颜色,设置画笔<br>     *<br>     * @return<br>     */<br>    private Paint getPaintByColor(int color) {<br>        Paint paint = new Paint();<br>//        设置颜色<br>        paint.setColor(color);<br>//       设置抗锯齿<br>        paint.setAntiAlias(true);<br>//       防抖动<br>        paint.setDither(true);<br>//       设置字体的大小 就是TextView 文本字体大小<br>        paint.setTextSize(getTextSize());<br>        return paint;<br>    }

画笔中的颜色,我传入即可

这里我初始了两个画笔颜色,一个默认颜色,和一个需要变成的颜色

 

1
<br>//    绘制不变色字体的画笔<br>    private Paint mOriginPaint;<br>//    绘制变色字体的画笔<br>    private Paint mChangePaint;

需要先初始化一个 TypedArray,在使用 TypedArray 时候,别忘了回收哦

 

1
<br>    private void initPaint(Context context, AttributeSet attrs) {<br>        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView);<br>        int changeColor = typedArray.getColor(R.styleable.ColorTrackTextView_changeColor, getTextColors().getDefaultColor());<br>        int originColor = typedArray.getColor(R.styleable.ColorTrackTextView_originColor, getTextColors().getDefaultColor());<br>//        typedArray 回收<br>        typedArray.recycle();<br>//        不变颜色的画笔<br>        mOriginPaint = getPaintByColor(originColor);<br>//        变色的画笔<br>        mChangePaint = getPaintByColor(changeColor);<br>    }

以上就是视频直播系统源码,使用自定义UI 完成文字颜色加载效果, 更多内容欢迎关注之后的文章

 

posted @   云豹科技-苏凌霄  阅读(148)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示