public class WordText extends TextView {

/** 画笔 */

private Paint paint = new Paint(); // 定义画笔

public WordText(Context context) {

super(context);

init(null);

}

public WordText(Context context, AttributeSet attrs) {

super(context, attrs);

init(attrs);

}

public WordText(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(attrs);

}

private void init(AttributeSet attrs) {

// 初始化画笔

paint.setAntiAlias(true);

paint.setStyle(Paint.Style.STROKE); // STROKE不填充,FILL填充,.FILL_AND_STROKE设置线条的样式,把空心线填满

PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);

paint.setPathEffect(effects);

// 根据用户配置的参数设置米字的颜色

if (attrs != null) {

TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.WordText);

// 获取米字背景颜色,并设置到UI

int iDottedLineColor = typedArray.getColor(R.styleable.WordText_DottedLineColor, Color.RED);

paint.setColor(iDottedLineColor);

boolean isKaiTi = typedArray.getBoolean(R.styleable.WordText_IsKaiTi, false);

// 设置字体为楷体

if (isKaiTi) {

setTypeface(MyApplication.m_tfSTKaiTi);

}

typedArray.recycle();

}

else {

paint.setColor(Color.RED);

}

}

@Override

protected void onDraw(Canvas canvas) {

// 字大小与控件高的比例计算出字体的大小(单位像素)

setTextSize(TypedValue.COMPLEX_UNIT_PX, getHeight() * 0.67f);

// 左上至右下线

drawDottedLine(0, 0, getWidth(), getHeight(), canvas);

// 纵向线

drawDottedLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), canvas);

// 右上至坐下

drawDottedLine(getWidth(), 0, 0, getHeight(), canvas);

// 水平线

drawDottedLine(0, getHeight() / 2, getWidth(), getHeight() / 2, canvas);

super.onDraw(canvas);

}

/**

* 画虚线

*/

private void drawDottedLine(int startX, int startY, int endX, int endY, Canvas canvas) {

Path path = new Path();

path.moveTo(startX, startY);

path.lineTo(endX, endY);

canvas.drawPath(path, paint);

}

}

自定义样式

android:shape="rectangle" >

布局文件中调用

xmlns:mc="http://schemas.android.com/apk/res/com.miisi.nc.magicchinese"

android:id="@+id/parent"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="#232187" >

<com.miisi.nc.magicchinese.view.WordText android:id="@+id/tvWord"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="223"

mc:DottedLineColor="#FFF"

mc:IsKaiTi="true"

android:text="日"

android:textSize="120dp"

android:textColor="#fff"

android:gravity="center"

android:background="@drawable/bg_style_frame_white_no_solid"

/>

android textview 楷体,Android自定义控件之自定义Text,画出米字格-FenGKun相关推荐

  1. android:ellipsize=end 不起作用,Android应用开发Android TextView关于android:ellipsize=end的一个神奇bug解决方案...

    本文将带你了解Android应用开发Android TextView关于android:ellipsize=end的一个神奇bug解决方案,希望本文对大家学Android有所帮助. 疑惑 今天在开发过 ...

  2. Android TextView带背景图片和自定义边框

    TextView带背景图片和自定义边框,效果如下图: 页面xml: <android.support.constraint.ConstraintLayout xmlns:android=&quo ...

  3. android textview import,android – textview中的镜像文本?

    我很确定使用4.0之前的TextView是不可能的. 镜像自定义TextView并不难: package your.pkg; import android.content.Context; impor ...

  4. android textview adapter,Android在FragmentPagerAdapter中的Fragment中设置TextView文本

    这个让我疯了.基本上,我想创建一个ViewPager并为其添加一些片段.然后,我想做的就是在Fragment的TextViews中设置一个值.我可以添加Fragments,并且它们会附加,但是当我在第 ...

  5. android textview截断,Android Textview文字在底部被截断

    所以我有2个文本视图,一个在另一个之上,但是当填充第二个文本视图时,剪切在底部被截断.标题为header_subtitle的textview存在问题 很明显我错过了一些明显的东西,但不知道是什么. [ ...

  6. android textview svg,Android中使用SVG与WebFont矢量图标

    一.参考文献 二.html使用方法 1.下载字体 网上百度自己要使用的字体,一般下载的是ttf格式,需要4种(或5)格式,其他的格式可以通过在线工具基于ttf转换 web-fontmin(这个在线转换 ...

  7. android Textview 功能,Android:TextView的常用功能

    android:autoText如果设置,将自动执行输入值的拼写纠正.此处无效果,在显示输入法并输入的时候起作用 android:bufferType指定getText()方式取得的文本类别.选项ed ...

  8. android textview坐标,android – 获取TextView中文本的位置

    看看几个Paint方法: getTextBounds()和 measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextVi ...

  9. android textview layoutparams,Android动态设置布局的LayoutParams属性总遇到造型异常

    Android动态设置布局的setLayoutParams()属性总遇到造型异常问题 android:id="@+id/introduce" android:layout_widt ...

最新文章

  1. springboot controller 参数绑定
  2. Unix高级编程之文件权限
  3. GridSearchCV和RandomizedSearchCV调参
  4. 误删/var/lib/dpkg/info,文件解决方案(是否完全解决,不确定)
  5. Protobuf生成Java代码(Maven)
  6. vue.js v-bind
  7. Hadoop社区正式支持腾讯云COS,全球大数据开发者将无缝使用中国云存储
  8. 时态数据库的应用介绍(1)
  9. 基于钉钉的多人协作项目办公
  10. 第4届CCCC字符串大赛 L2-3深入虎穴
  11. Java编程语言的风格
  12. 炼数成金 课件整理数据分析与R语言 第1周
  13. spring-boot mybadis多数据源配置
  14. 服务器协议 重发 回包 039,3情报板预置播放表、位图一览表.doc
  15. c# 微软 REST API语音转文字
  16. 图文解说Quartus II使用入门
  17. windows 内存映射文件
  18. 【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
  19. php文件格式及其导出
  20. [新手必读]论坛推广技巧

热门文章

  1. MTK Android8.1 UI之蓝牙和WiFi
  2. 给浏览器设置一个图片背景/主题
  3. 你能背着“逃避”背多久?
  4. 江苏机器人竞赛南航_关于公布2020年南航金城学院首届大学生机器人比赛暨江苏省大学生机器人大赛校内选拔赛的评审结果的通知...
  5. 链表中快慢指针的应用
  6. 【Java基础】UML类图及各种线代表的关系含义总结
  7. xp职称计算机考试题库,全国职称计算机考试题库(WindowsXP模块)
  8. uniapp实现国际化多语言切换
  9. Android 之 TranslateAnimation类:位移动画类(转载)
  10. BalalaPower个人解题心得