package com.example.textBorder;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by Maijx on 2015/5/6.
*/
public class StrokeTextView extends TextView {
private int strokeColor;
private int strokeWidth;
private TextPaint strokePaint;
public StrokeTextView(Context context) {
super(context);
}
public StrokeTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public StrokeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView, defStyleAttr, 0);
strokeColor = a.getColor(R.styleable.StrokeTextView_border_color, 0);
strokeWidth = a.getDimensionPixelSize(R.styleable.StrokeTextView_border_width, 0);
}
@Override
protected void onDraw(Canvas canvas) {
if (strokeWidth > 0) {
if (strokePaint == null) {
strokePaint = new TextPaint();
}
// 复制原来TextViewg画笔中的一些参数
TextPaint paint = getPaint();
strokePaint.setTextSize(paint.getTextSize());
strokePaint.setFlags(paint.getFlags());
strokePaint.setTypeface(paint.getTypeface());
strokePaint.setAlpha(paint.getAlpha());
// 自定义描边效果
strokePaint.setStyle(Paint.Style.FILL_AND_STROKE);// 设置不同Style有不同效果哟
strokePaint.setColor(strokeColor);
strokePaint.setStrokeWidth(strokeWidth);
String text = getText().toString();
//在文本底层画出带描边的文本
canvas.drawText(text, (getWidth() - strokePaint.measureText(text)) / 2, getBaseline(), strokePaint);
}
super.onDraw(canvas);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TextViewBorderTest.zip (52个子文件)
TextViewBorderTest
gradlew 5KB
settings.gradle 16B
TextViewBorderTest.iml 847B
.idea
libraries
appcompat_v7_21_0_3.xml 421B
support_v4_21_0_3.xml 564B
support_annotations_21_0_3.xml 511B
misc.xml 840B
compiler.xml 734B
workspace.xml 107KB
gradle.xml 580B
.name 18B
encodings.xml 171B
modules.xml 385B
scopes
scope_settings.xml 143B
copyright
profiles_settings.xml 76B
vcs.xml 173B
gradle
wrapper
gradle-wrapper.properties 232B
gradle-wrapper.jar 49KB
.gradle
2.2.1
taskArtifacts
cache.properties.lock 17B
fileHashes.bin 84KB
outputFileStates.bin 19KB
taskArtifacts.bin 45KB
fileSnapshots.bin 1.5MB
cache.properties 37B
local.properties 436B
gradlew.bat 2KB
gradle.properties 872B
.gitignore 87B
app
src
androidTest
java
com
example
textBorder
ApplicationTest.java 365B
main
AndroidManifest.xml 715B
res
mipmap-hdpi
fake_luxury_tiled.xml 192B
ic_launcher.png 3KB
fake_luxury.png 494B
triangles_tiled.xml 190B
triangles.png 438B
mipmap-xhdpi
ic_launcher.png 5KB
drawable-hdpi
menu
menu_text_border.xml 407B
mipmap-mdpi
ic_launcher.png 2KB
drawable
layout
activity_text_border.xml 1KB
values-w820dp
dimens.xml 364B
values
attrs.xml 347B
strings.xml 194B
styles.xml 202B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
java
com
example
textBorder
StrokeTextView.java 2KB
TextBorderActivity.java 713B
libs
app.iml 7KB
.gitignore 8B
build.gradle 613B
proguard-rules.pro 670B
build.gradle 455B
共 52 条
- 1
资源评论
- zwj02882015-09-02项目中用到了,刚好。
- dicy8882015-06-29资源不对 功能不能正确实现
- foodman132015-10-27无法使用,光从代码里看不出所以然
kkmike999
- 粉丝: 15
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功