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


猜你喜欢
- 多线程下@Resource注入为null前情叙述记录以下这个坑,我的情况大致是这样的,我在用webmagic写爬虫,在类中通过@Resoou
- 1. AIE (演示地址)AIE是一个开源的ajax图片编辑器,基于ExtJS与PHP ImageMagick开发,易于与博客/相册等其它应
- 我们在开发Java项目的时候,经常需要对参数进行一些必填项、格式、长度等进行校验,如果手写代码对参数校验,每个接口会需要很多低级的代码,这样
- List 的方法列表方法名功能说明ArrayList()构造方法,用于创建一个空的数组列表add(E e)将指定的元素添加到此列表的尾部ge
- 前言今天我们继续聊聊在SprinBoot中如何集成参数校验Validator,以及参数校验的高阶技巧(自定义校验,分组校验)。&ld
- 总结并复现了一下Unsafe在安全领域的一些应用0 前言unsafe里面有很多好用的方法,比如allocateInstance可以直接创建实
- SpringMVC域对象共享数据一、域对象1. 域对象的作用就是在一定范围内可以共享数据,通常有 3 种:request: 一次请求,多个资
- 数组排序在很多的面试题上都会出现数组排序的操作形式。但是这个时候你千万别写上:java.util.Arrays.sort(数组)。而这种排序
- 本文实例演示了Java多线程死锁。分享给大家供大家参考,具体如下:package com.damlab.fz;public class De
- 服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 
- 1、代码就一个Controller,从官网复制过来的,如下package com.springboot.controller;import
- C/C++的数据类型:一,整型Turbo C: [signed] int 2Byte//有符号数,-32768~32
- 想要实现无限轮播,一直向左滑动,当到最后一个view时,会滑动到第一个,无限…可以自己写ViewPager然后加handler先实现自动滚动
- 目录wait-notifyjoin方式ReentrantLockReentrantLock+ConditionSemaphore三个线程T1
- 1.一级指针#include?<stdio.h>int?main(){?int?data?=?10;?int?*p1?=?&am
- 1、在build.gradle(Module)里引入依赖,然后重构(sync Now):android { ...
- 线程池做什么网络请求通常有两种形式:第一种,请求不是很频繁,而且每次连接后会保持相当一段时间来读数据或者写数据,最后断开,如文件下载,网络流
- * 什么是 * Spring MVC中的 * (Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户
- 本文实例为大家分享了Android ViewPager指示器的制作方法,供大家参考,具体内容如下1.概述ViewPageIndicator这
- 目前在做项目中有处理图片的部分,参考了一下网上案例,自己写了一个获取内容中的图片地址的方法。 一般来说一个 HTML 文档有很多标