class MandarinTextView(context: Context, attrs: AttributeSet?) :
AppCompatTextView(context, attrs) {
//外框线
private var mLinePaint: Paint = Paint()
//内十字线
private var mDottedLinePaint: Paint = Paint()
//田字格区域
private var r2: RectF = RectF()
//外框颜色
private var outlineColor: Int
//内框颜色
private var inlineColor: Int
//外框宽度
private var outlineWidth: Int
//内框宽度
private var inlineWidth: Int
//外框圆度
private var borderRadius: Int
//内框透明度
private var inlineAlpha: Float
init {
// 获取参数
val ta = context.obtainStyledAttributes(
attrs,
R.styleable.MandarinTextView
)
outlineColor = ta.getColor(
R.styleable.MandarinTextView_outlineColor,
Color.parseColor("#666666")
)
inlineColor = ta.getColor(
R.styleable.MandarinTextView_inlineColor,
Color.parseColor("#DDDDDD")
)
outlineWidth = ta.getDimensionPixelSize(
R.styleable.MandarinTextView_outlineWidth,
0.5f.px.toInt()
)
inlineWidth = ta.getDimensionPixelSize(
R.styleable.MandarinTextView_inlineWidth,
0.5f.px.toInt()
)
borderRadius = ta.getDimensionPixelSize(
R.styleable.MandarinTextView_outlineRadius,
5f.px.toInt()
)
inlineAlpha = ta.getFloat(
R.styleable.MandarinTextView_inlineAlpha,
0.8f
)
// 释放资源
ta.recycle()
mLinePaint.color = outlineColor
mLinePaint.style = Paint.Style.STROKE
mLinePaint.strokeWidth = outlineWidth.toFloat()
mLinePaint.isAntiAlias = true
mLinePaint.pathEffect = DashPathEffect(floatArrayOf(15f, 10f), 0f)
mDottedLinePaint.color = inlineColor
mDottedLinePaint.style = Paint.Style.STROKE
mDottedLinePaint.strokeWidth = inlineWidth.toFloat()
mDottedLinePaint.isAntiAlias = true
mDottedLinePaint.alpha = (255 * inlineAlpha).toInt()
mDottedLinePaint.pathEffect = DashPathEffect(floatArrayOf(15f, 10f), 0f)
}
override fun onDraw(canvas: Canvas) {
canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), mDottedLinePaint)
canvas.drawLine(0f, height / 2f, width.toFloat(), height / 2f, mDottedLinePaint)
r2.set(
0f + outlineWidth / 2,
0f + outlineWidth / 2,
width.toFloat() - outlineWidth / 2,
height.toFloat() - outlineWidth / 2
)
canvas.drawRoundRect(r2, borderRadius.toFloat(), borderRadius.toFloat(), mLinePaint)
super.onDraw(canvas)
}
fun setOutlineColor(color: Int) {
outlineColor = color
mLinePaint.color = outlineColor
invalidate()
}
}
自定义View-田字格
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 对于Android程序员来说,自定义View是绕不过的话题,作为Android终端,除了一些后台应用,大部分...
- 本文出自简书:尧沐,如需转载请标明出处,尊重原创谢谢博客地址:http://www.jianshu.com/p/6...
- 一、概述 继续学习自定义view,主要还是讲解画笔,实现的功能是用两种颜色的画笔画文字,根据滑动的偏移量,实现颜色...