渐变的TextView

设置圆角渐变的TextView

第一步:在Drawable创建bg.xml 文件

如下所示

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置圆角的半径-->
    <corners android:radius="50dp"></corners>

    <!--指定使用渐变背景色 颜色从红色-绿色-蓝色-->
    <gradient
        android:centerColor="#0f0"
        android:endColor="#00f"
        android:startColor="#f00"></gradient>
</shape>
第二步:在TextView中引入

<TextView
    android:textSize="50sp"
    android:background="@drawable/bg1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="圆角渐变" />
效果图如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现`TextView`文字滚动时颜色渐变的效果,可以使用`LinearGradient`和`Matrix`来实现。 具体实现步骤如下: 1. 定义一个`LinearGradient`对象,用于存储渐变颜色信息。 ```java int[] colors = {Color.BLACK, Color.WHITE, Color.BLACK}; // 定义渐变颜色 float[] positions = {0f, 0.5f, 1f}; // 定义渐变颜色位置 LinearGradient gradient = new LinearGradient(0, 0, textView.getWidth(), 0, colors, positions, Shader.TileMode.CLAMP); // 创建线性渐变对象 ``` 2. 定义一个`Matrix`对象,用于控制文字的绘制。 ```java Matrix matrix = new Matrix(); matrix.setTranslate(textView.getWidth(), 0); // 设置初始位置 ``` 3. 在`TextView`的`onDraw()`方法中,使用`Paint`对象设置渐变和矩阵,并调用`canvas.drawText()`方法绘制文字。 ```java Paint paint = textView.getPaint(); paint.setShader(gradient); paint.getShader().setLocalMatrix(matrix); canvas.drawText(textView.getText().toString(), 0, textView.getHeight() / 2, paint); ``` 4. 在`TextView`的`onSizeChanged()`方法中,重新设置`LinearGradient`和`Matrix`对象的参数。 ```java @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int[] colors = {Color.BLACK, Color.WHITE, Color.BLACK}; float[] positions = {0f, 0.5f, 1f}; gradient = new LinearGradient(0, 0, w, 0, colors, positions, Shader.TileMode.CLAMP); matrix = new Matrix(); matrix.setTranslate(w, 0); } ``` 完整代码示例: ```java public class GradientTextView extends androidx.appcompat.widget.AppCompatTextView { private LinearGradient gradient; private Matrix matrix; public GradientTextView(Context context) { super(context); } public GradientTextView(Context context, AttributeSet attrs) { super(context, attrs); } public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { Paint paint = getPaint(); paint.setShader(gradient); paint.getShader().setLocalMatrix(matrix); canvas.drawText(getText().toString(), 0, getHeight() / 2, paint); invalidate(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { int[] colors = {Color.BLACK, Color.WHITE, Color.BLACK}; float[] positions = {0f, 0.5f, 1f}; gradient = new LinearGradient(0, 0, w, 0, colors, positions, Shader.TileMode.CLAMP); matrix = new Matrix(); matrix.setTranslate(w, 0); } } ``` 使用方法: ```xml <com.example.GradientTextView android:id="@+id/gradient_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lorem_ipsum" android:textColor="@android:color/black" android:textSize="24sp" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值