Android 让SuperTextView支持边框线颜色渐变


前言

  最近碰到一个需要,需要我实现大量的边框颜色渐变效果,思来想去,能做到这个效果的应该就是SuperTextView了,然后我就上了Github去找它的源码库https://github.com/chenBingX/SuperTextView,然后研究了一下发现SuperTextView只支持单色边框线,作者在20年的时候说会考虑加上(https://github.com/chenBingX/SuperTextView/issues/88),但是到现在都还没加上,那就只能自己动手了,做完了发出来跟大家共享。
本文链接:https://blog.csdn.net/weixin_44337681/article/details/124525493


一、先上效果图

让SuperTextView支持边框线颜色渐变 效果图

二、SuperTextView源码修改

	1、首先增加以下全局变量
	
    private boolean strokeShaderEnable;
    private int strokeShaderStartColor;
    private int strokeShaderEndColor;
    private ShaderMode strokeShaderMode;
    private LinearGradient strokeShader;

	2、在SuperTextView库的attrs文件里 declare-styleable标签 增加以下代码
	
	<attr name="stv_strokeShaderEnable" format="boolean"/>
    <attr name="stv_strokeShaderStartColor" format="reference|color"/>
    <attr name="stv_strokeShaderEndColor" format="reference|color"/>
    <attr name="stv_strokeShaderMode" format="enum">
      <enum name="topToBottom" value="0"/>
      <enum name="bottomToTop" value="1"/>
      <enum name="leftToRight" value="2"/>
      <enum name="rightToLeft" value="3"/>
    </attr>

	3、在 initAttrs 方法增加这些属性的获取
	
	strokeShaderEnable = typedArray.getBoolean(R.styleable.SuperTextView_stv_strokeShaderEnable, false);
    strokeShaderMode = ShaderMode.valueOf(typedArray.getInteger(R.styleable.SuperTextView_stv_strokeShaderMode, ShaderMode.TOP_TO_BOTTOM.code));
    strokeShaderStartColor =
            typedArray.getColor(R.styleable.SuperTextView_stv_strokeShaderStartColor, 0);
    strokeShaderEndColor =
            typedArray.getColor(R.styleable.SuperTextView_stv_strokeShaderEndColor, 0);

	4、在 drawStrokeLine 修改画笔颜色

	把原本的
    paint.setColor(strokeColor);
    替换成
    if (strokeShaderEnable) {
        if (strokeShader == null) {
            strokeShader = createShader(strokeShaderStartColor, strokeShaderEndColor, strokeShaderMode, 0, 0, width, height);
        }
        paint.setShader(strokeShader);
    } else {
        paint.setColor(strokeColor);
    }
    
	

三、效果图代码示例 (xml文件)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.coorchice.library.SuperTextView
        android:id="@+id/stv"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="原版效果"
        android:textSize="16sp"
        android:textColor="@color/white"
        android:gravity="center"
        app:stv_corner="16dp"
        app:stv_solid="#666666"
        app:stv_stroke_width="5dp"
        app:stv_stroke_color="#ffff00"
        android:layout_marginBottom="20dp"
        app:layout_constraintBottom_toTopOf="@id/stv1"
        app:layout_constraintStart_toStartOf="@id/stv1"
        />

    <com.coorchice.library.SuperTextView
        android:id="@+id/stv_start_color"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="start_color"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:gravity="center"
        app:stv_corner="10dp"
        app:stv_solid="#ffff00"
        android:padding="5dp"
        app:layout_constraintTop_toTopOf="@id/stv"
        app:layout_constraintBottom_toBottomOf="@id/stv"
        app:layout_constraintEnd_toStartOf="@id/stv_end_color"
        app:layout_constraintStart_toEndOf="@id/stv"/>

    <com.coorchice.library.SuperTextView
        android:id="@+id/stv_end_color"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="end_color"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:gravity="center"
        app:stv_corner="10dp"
        app:stv_solid="#00ffff"
        android:padding="5dp"
        app:layout_constraintTop_toTopOf="@id/stv_start_color"
        app:layout_constraintBottom_toBottomOf="@id/stv_start_color"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/stv_start_color"
        />

    <com.coorchice.library.SuperTextView
        android:id="@+id/stv1"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="leftToRight"
        android:textSize="16sp"
        android:textColor="@color/white"
        android:gravity="center"
        app:stv_corner="16dp"
        app:stv_solid="#666666"
        app:stv_stroke_width="5dp"
        app:stv_strokeShaderEnable="true"
        app:stv_strokeShaderStartColor="#ffff00"
        app:stv_strokeShaderEndColor="#00ffff"
        app:stv_strokeShaderMode="leftToRight"
        android:layout_marginBottom="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/stv2"
        />

    <com.coorchice.library.SuperTextView
        android:id="@+id/stv2"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="topToBottom"
        android:textSize="16sp"
        android:textColor="@color/white"
        android:gravity="center"
        app:stv_corner="16dp"
        app:stv_solid="#666666"
        app:stv_stroke_width="5dp"
        app:stv_strokeShaderEnable="true"
        app:stv_strokeShaderStartColor="#ffff00"
        app:stv_strokeShaderEndColor="#00ffff"
        app:stv_strokeShaderMode="topToBottom"
        android:layout_marginBottom="100dp"
        app:layout_constraintStart_toEndOf="@id/stv1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

    <com.coorchice.library.SuperTextView
        android:id="@+id/stv3"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="rightToLeft"
        android:textSize="16sp"
        android:textColor="@color/white"
        android:gravity="center"
        app:stv_corner="16dp"
        app:stv_solid="#666666"
        app:stv_stroke_width="5dp"
        app:stv_strokeShaderEnable="true"
        app:stv_strokeShaderStartColor="#ffff00"
        app:stv_strokeShaderEndColor="#00ffff"
        app:stv_strokeShaderMode="rightToLeft"
        app:layout_constraintTop_toBottomOf="@id/stv1"
        app:layout_constraintStart_toStartOf="@id/stv1"
        app:layout_constraintBottom_toBottomOf="parent"
        />

    <com.coorchice.library.SuperTextView
        android:id="@+id/stv4"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="bottomToTop"
        android:textSize="16sp"
        android:textColor="@color/white"
        android:gravity="center"
        app:stv_corner="16dp"
        app:stv_solid="#666666"
        app:stv_stroke_width="5dp"
        app:stv_strokeShaderEnable="true"
        app:stv_strokeShaderStartColor="#ffff00"
        app:stv_strokeShaderEndColor="#00ffff"
        app:stv_strokeShaderMode="bottomToTop"
        app:layout_constraintTop_toBottomOf="@id/stv1"
        app:layout_constraintEnd_toEndOf="@id/stv2"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

总结

  本文解决了SuperTextView不支持边框颜色渐变的问题,虽然目前只支持xml文件配置使用,但也能完成我的需求了,后面会找时间把代码设置的方式一起加上。由于SuperTextView的源码较长,就不在这里整个放出来了,有需要的同学留言私发。创作不易,觉得不错的话,就来个一键三连吧,感谢大家!
  未经许可,请勿转载!
本文链接:https://blog.csdn.net/weixin_44337681/article/details/124525493

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白白猪

感谢兄弟的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值