android 文本渐变显示
Android开发过程中,对TextView会用得非常多,字体颜色渐变或增加很多色彩.
这里说三种渐变方式:
一、LinearGradient
1)继承 TextView,重写 onLayout 方法后设置 Shader,也可再ondraw中处理
public class GradientTextView1 extends AppCompatTextView {private int mStartColor;private int mEndColor;private boolean mBold;public GradientTextView1(@NonNull Context context) {this(context, null);}public GradientTextView1(@NonNull Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public GradientTextView1(@NonNull Context context, @Nullable AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(@NonNull Context context, @Nullable AttributeSet attrs) {TypedArray typedArray =context.obtainStyledAttributes(attrs, R.styleable.GradientTextView);if (typedArray == null) {return;}mBold = typedArray.getBoolean(R.styleable.GradientTextView_gradient_bold,false);mStartColor =typedArray.getColor(R.styleable.GradientTextView_gradient_start_color,getResources().getColor(R.color.primary_color));mEndColor =typedArray.getColor(R.styleable.GradientTextView_gradient_end_color,getResources().getColor(R.color.accent_color));typedArray.recycle();if (mBold) {getPaint().setFakeBoldText(true);}}@SuppressLint("DrawAllocation")@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);if (changed) {getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(),mStartColor,mEndColor,Shader.TileMode.CLAMP));}}
}
二、自自定义工具方法,在代码中动态改变
public class TextViewUtil {public static void setTextColorGradient(TextView textView, @ColorRes int startColor, @ColorRes int endColor) {if (textView == null || textView.getContext() == null) {return;}Context context = textView.getContext();@ColorInt final int sc = context.getResources().getColor(startColor);@ColorInt final int ec = context.getResources().getColor(endColor);final float x = textView.getPaint().getTextSize() * textView.getText().length();LinearGradient gradient = new LinearGradient(0, 0, x, 0, sc, ec, Shader.TileMode.CLAMP);textView.getPaint().setShader(gradient);textView.invalidate();}public static void setTextColorGradient(TextView textView, int[] colors, float[] positions) {if (textView == null || textView.getContext() == null) {return;}String text = textView.getText().toString();// 方法1:getTextBoundsRect rect = new Rect();textView.getPaint().getTextBounds(text, 0, text.length(), rect);// 方法2:measureText
// float measuredWidth = textView.getPaint().measureText(text);float textWidth = rect.width();LinearGradient linearGradient = new LinearGradient(0, 0, textWidth, 0,colors,positions,Shader.TileMode.REPEAT);textView.getPaint().setShader(linearGradient);textView.invalidate();}public static void clearTextColorGradient(TextView textView) {textView.getPaint().setShader(null);textView.invalidate();}
三、使用Span做法,自定义一个Span,自己写个Span,代码中再去用
使用这种方式:
<declare-styleable name="GradientTextView"><attr name="gradient_text" format="string" /><attr name="gradient_bold" format="boolean" /><attr name="gradient_start_color" format="color" /><attr name="gradient_end_color" format="color" />
</declare-styleable>public class GradientTextView extends AppCompatTextView {private String mGradientText;private int mStartColor;private int mEndColor;private boolean mBold;public GradientTextView(@NonNull Context context) {this(context, null);}public GradientTextView(@NonNull Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public GradientTextView(@NonNull Context context, @Nullable AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(@NonNull Context context, @Nullable AttributeSet attrs) {TypedArray typedArray =context.obtainStyledAttributes(attrs, R.styleable.GradientTextView);if (typedArray == null) {return;}mGradientText =typedArray.getString(R.styleable.GradientTextView_gradient_text);mBold = typedArray.getBoolean(R.styleable.GradientTextView_gradient_bold,false);mStartColor =typedArray.getColor(R.styleable.GradientTextView_gradient_start_color,getResources().getColor(R.color.primary_color));mEndColor =typedArray.getColor(R.styleable.GradientTextView_gradient_end_color,getResources().getColor(R.color.accent_color));typedArray.recycle();setGradientText(mGradientText);if (mBold) {getPaint().setFakeBoldText(true);}}public void setGradientText(String text) {if (text == null || text.length() == 0) {return;}SpannableString spannableString = new SpannableString(text);GradientFontSpan gradientFontSpan = new GradientFontSpan(mStartColor, mEndColor);spannableString.setSpan(gradientFontSpan, 0, text.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);setText(spannableString);invalidate();}public static class GradientFontSpan extends ReplacementSpan {private int mSize;private int mStartColor;private int mEndColor;public GradientFontSpan(int startColor, int endColor) {mStartColor = startColor;mEndColor = endColor;}public GradientFontSpan(Context context) {mStartColor = context.getResources().getColor(R.color.primary_color);mEndColor = context.getResources().getColor(R.color.accent_color);}@Overridepublic int getSize(@NonNull Paint paint, CharSequence text, int start, int end,@Nullable Paint.FontMetricsInt fm) {mSize = Math.round(paint.measureText(text, start, end));Paint.FontMetricsInt metrics = paint.getFontMetricsInt();if (fm != null) {fm.top = metrics.top;fm.ascent = metrics.ascent;fm.descent = metrics.descent;fm.bottom = metrics.bottom;}return mSize;}@Overridepublic void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x,int top, int y, int bottom, @NonNull Paint paint) {LinearGradient gradient = new LinearGradient(0, 0, mSize, 0, mEndColor, mStartColor,Shader.TileMode.CLAMP);paint.setShader(gradient);canvas.drawText(text, start, end, x, y, paint);}}
}
这三种方式都可以实现文本渐变效果
- END
android 文本渐变显示相关推荐
- android文本后面显示按钮,按下按钮后显示文本android
我是新来的android,现在我想按下一个按钮,然后在屏幕上显示文本.这里是我的代码:按下按钮后显示文本android XML android:id="@+id/button1" ...
- android 文本分页显示,Android查看带有列表视图的分页器并动态添加文本
我在一个分片活动中有一个Viewpager,它有一个带有编辑文本和发送按钮的botton框架. 在片段布局中,我有一个ListView,并在片段中附加了一个适配器.现在我正在实现从片段中的Parent ...
- android顶部渐变显示,Android实现直播聊天区域中顶部的渐变效果
Android实现直播聊天区域中顶部的渐变效果 发布时间:2020-10-15 08:24:18 来源:脚本之家 阅读:102 作者:MG屠夫 背景 在4月份开发直播时,有一个需求,需要实现一个Rec ...
- android顶部渐变显示,Android实现直播聊天区域顶部渐变效果
背景 4月份开发直播时,有一个需求,需要实现一个RecylerView顶部渐变的效果 实际效果 解决思路 图层重叠处理(本质是alpha叠加出来的效果) 实现流程 保存一个图层,然后画渐变,最后再和原 ...
- 5 控件固定大小_【聊技术】在Android中实现自适应文本大小显示
本周的聊技术话题和大家说说如何在Android中实现自适应文本大小显示. 想象一下,在布局中,通常显示文本的区域大小是固定的,但是文本长度并不总是固定的.比如列表中的文章标题.界面下方的按钮文本等等. ...
- 【聊技术】在Android中实现自适应文本大小显示
本周的聊技术话题和大家说说如何在Android中实现自适应文本大小显示. 想象一下,在布局中,通常显示文本的区域大小是固定的,但是文本长度并不总是固定的.比如列表中的文章标题.界面下方的按钮文本等等. ...
- android 逐字动画,Android实现文本逐字显示View(类似rpg游戏人物对话,文本逐字显示)...
前面好多篇文章都是Android Studio.源码编译.ndk等相关教程,今天敲一敲代码,不然都生锈了哈_. 来个古装动画美图,缓解大家疲劳的眼睛...(话说有木有人知道这是谁???) Paste_ ...
- Android中设置显示文本,Android文本显示控件-TextView属性详解
android:autoLink //设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web /email/phone/map/all) andr ...
- com.android.rrpgdemo,Android实现文本逐字显示View(类似rpg游戏人物对话,文本逐字显示)...
8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 前面好多篇文章都是Android Studio.源码编译.ndk等相关教程,今天敲一敲代码,不然都生锈了哈^_^. 来个 ...
最新文章
- 在CentOS 6.6 64bit上安装截图软件shutter
- 经典mysql 语句收录
- 时代中坚:互联网电视迎来极致时代
- 史上最轻量​!阿里新型单元测试Mock工具开源了
- why we see different http status code like 404, 500. where are they handled
- python统计分析--2.预分析:异常值、缺失值处理
- linux系统编程之进程(七):system()函数使用【转】
- 如何确定Ionic是否适合您的项目
- ijkplayer、VLC Player、SmartPlayer、ExoPlayer播放器比较
- photoshop CG6 基础知识的学习
- linux umask命令
- 麒麟系统下安装win10_win10系统安装图文详细教程
- 从需求出发来看关系模型与非关系模型–时代的变革
- PyPI 官方仓库遭遇挖矿恶意组件投毒
- 怎样清理苹果手机内存空间_手机资讯:苹果iPhone如何拒绝骚扰短信清理短信教程...
- Android基础学习笔记14:安卓手势编程
- 多IP服务器有什么用
- 写一个GAMEBOY的模拟器
- linux 安装谷歌浏览器--Google chrome
- [mybatis异常:Could not find result map ......]
热门文章
- SpringBoot项目启动执行任务的几种方式
- python模拟手机访问_Python selenium 模拟Chrome浏览器打开手机模式
- 中国第二大传感器企业宝座易主!14家公司新上市!国产传感器风起云涌!(附最新市值排名榜单)
- 组合式Api 及相关操作用法
- 多多自走棋改动_多多自走棋最新版下载|多多自走棋修改版安卓版下载 v1.4.0 - 跑跑车安卓网...
- 软考高项我一次考过的秘诀 | 附记忆口诀记忆方法
- 写给谷歌的纪念-一个终于放弃坚守谷歌的中国用户的心声
- 【Java设计模式】工厂模式(案例详解)
- “疯狂木头”设计的落雁弹弓
- (转载)混合图层算法