Android开发TextvView实现镂空字体效果示例代码
作者:cachelittlepeople 发布时间:2023-07-14 03:03:20
标签:Android,镂空字体,TextvView
Android镂空字体的实现效果图,感兴趣的朋友可以参考实现代码。
效果图:
记录一下...
自定义TextView
public class HollowTextView extends AppCompatTextView {
private Paint mTextPaint, mBackgroundPaint;
private Bitmap mBackgroundBitmap,mTextBitmap;
private Canvas mBackgroundCanvas,mTextCanvas;
private RectF mBackgroundRect;
private int mBackgroundColor;
private float mCornerRadius;
public HollowTextView(Context context) {
this(context,null);
}
public HollowTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs,0);
initPaint();
}
public HollowTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs,defStyleAttr);
initPaint();
}
private void initAttrs(AttributeSet attrs,int defStyleAttr){
if(attrs == null){
return;
}
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HollowTextView, defStyleAttr, 0);
mBackgroundColor = typedArray.getColor(R.styleable.HollowTextView_hollowTextView_background_color, Color.TRANSPARENT);
mCornerRadius = typedArray.getDimension(R.styleable.HollowTextView_hollowTextView_corner_radius,0);
typedArray.recycle();
}
/***
* 初始化画笔属性
*/
private void initPaint() {
//画文字的paint
mTextPaint = new Paint();
//这是镂空的关键
mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mTextPaint.setAntiAlias(true);
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(mBackgroundColor);
mBackgroundPaint.setAntiAlias(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBackgroundBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
mBackgroundCanvas = new Canvas(mBackgroundBitmap);
mTextBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_4444);
mTextCanvas = new Canvas(mTextBitmap);
mBackgroundRect = new RectF(0,0,getWidth(),getHeight());
}
@Override
protected void onDraw(Canvas canvas) {
//这里给super传入的是mTextCanvas,把一些基本属性都支持进去
super.onDraw(mTextCanvas);
drawBackground(mBackgroundCanvas);
int sc;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ){
sc = canvas.saveLayer(0,0,getMeasuredWidth(),getMeasuredHeight(),null);
}else {
sc = canvas.saveLayer(0,0,getMeasuredWidth(),getMeasuredHeight(),null,Canvas.ALL_SAVE_FLAG);
}
canvas.drawBitmap(mBackgroundBitmap,0,0,null);
canvas.drawBitmap(mTextBitmap, 0, 0, mTextPaint);
canvas.restoreToCount(sc);
}
private void drawBackground(Canvas canvas){
if(mCornerRadius > 0){
canvas.drawRoundRect(mBackgroundRect,mCornerRadius,mCornerRadius, mBackgroundPaint);
}else {
canvas.drawColor(mBackgroundColor);
}
}
attr.xml文件
<declare-styleable name="HollowTextView">
<attr name="hollowTextView_background_color" format="color|reference"/>
<attr name="hollowTextView_corner_radius" format="dimension|reference"/>
</declare-styleable>
xml中使用
<com.cn.util.HollowTextView
android:id="@+id/hollowtext"
android:layout_width="60dp"
android:layout_height="50dp"
android:gravity="center"
android:text="99+"
android:textSize="30sp"
android:textStyle="bold"
app:hollowTextView_background_color="@color/white"
app:hollowTextView_corner_radius="5dp"
android:layout_centerInParent="true"/>
来源:https://blog.csdn.net/congcongguniang/article/details/109330727
0
投稿
猜你喜欢
- 与任何程序设计语言一样,Java使用条件语句和循环结构确定控制流。本文将简单讲解条件、循环和switch。一、块作用域块(block),即复
- 1、首先创建一个按钮<Buttonandroid:id="@+id/click"android:layout_wi
- 一、Vitamio介绍 1.1 Vitamio是什么? Vit
- 在Excel中,可对单元格中的字符串设置多种不同样式,通常只需要获取到单元格直接设置样式即可,该方法设置的样式会应用于该单元格中的所有字符。
- 组合模式是一种常见的设计模式(但我感觉有点复杂)也叫合成模式,有时又叫做部分-整体模式,主要是用来描述部分与整体的关系。个人理解:组合模式就
- yaml中的数组类型取值yaml中简单的风格,十分受大家的欢迎先说下简单的配置如何取值# application-dev.yml testV
- Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Andro
- 本文实例讲述了C#文件断点续传实现方法。分享给大家供大家参考。具体实现方法如下:/// <summary>/// 下载局域网文件
- 一、介绍JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有
- JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在
- 一、项目运行环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,
- 对于单链表不熟悉的可以看一下基于Java实现单链表的增删改查一、原地反转1、新建一个哨兵节点下一结点指向头结点2、把待反转链表的下一节点插入
- 详解C#使用AD(Active Directory)验证内网用户名密码1. 连到内网,找到AD的domain地址nslookup set t
- StringDictionary:默认key不区分大小写 NameValueCollection:默认key区分大小写 KeyedColle
- 如下就可以获取button中的文本内容using System.Collections.Generic;using UnityEngine;
- 本文实例讲述了Android编程判断网络是否可用及调用系统设置项的方法。分享给大家供大家参考,具体如下:private boolean ch
- 前言文件上传是项目开发中最常见的功能之一 ,SpringMVC 可以很好的支持文件上传,但是SpringMVC上下文中默认没有装配Multi
- 1、引言你能搜到这个教程,说明你对 Maven 感兴趣,但是又不是太理解。那么接下来这个系列的教程将会详细讲解 Maven 的用法,相信你看
- 前言这几天在做一个小工具app,结果在fragment里面动态申请权限提示原有的申请方法已经弃用,还画了很明显的删除线。。。这叫一个强迫症的
- Java 线程池ExecutorService1.线程池1.1什么情况下使用线程池单个任务处理的时间比较短.将需处理的任务的数量大.1.2使