自定义控件--描边字体
来源:互联网 发布:bf是什么意思淘宝 编辑:程序博客网 时间:2024/05/17 20:21
今天给大家介绍一个具有描边效果的自定义控件(不同于阴影),废话不多说,先看看效果
如果你眼前一亮,我艹,我要的就是这个,嘿嘿…
public class StroketText extends TextView { private TextView outlineTextView = null; private int outSideColor; //描边颜色 private int strokeWidth; //描边粗细 public StroketText(Context context) { super(context); outlineTextView = new TextView(context); init(); } public StroketText(Context context, AttributeSet attrs) { super(context, attrs); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.StroketText); if(array!=null){ outSideColor = array.getColor(R.styleable.StroketText_outsideColor, Color.TRANSPARENT); strokeWidth = array.getInt(R.styleable.StroketText_stroketWidth, 2); } outlineTextView = new TextView(context, attrs); init(); } public StroketText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.StroketText); if(array!=null){ outSideColor = array.getColor(R.styleable.StroketText_outsideColor, Color.TRANSPARENT); strokeWidth = array.getInt(R.styleable.StroketText_stroketWidth, 2); } outlineTextView = new TextView(context, attrs, defStyleAttr); init(); } public void init(){ TextPaint paint = outlineTextView.getPaint(); paint.setStrokeWidth(strokeWidth); // 描边宽度 paint.setStyle(Paint.Style.STROKE); outlineTextView.setTextColor(outSideColor); // 描边颜色 outlineTextView.setGravity(getGravity()); } @Override public void setLayoutParams (ViewGroup.LayoutParams params) { super.setLayoutParams(params); outlineTextView.setLayoutParams(params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 设置轮廓文字 CharSequence outlineText = outlineTextView.getText(); if (outlineText == null || !outlineText.equals(this.getText())) { outlineTextView.setText(getText()); postInvalidate(); } outlineTextView.measure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); outlineTextView.layout(left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { outlineTextView.draw(canvas); super.onDraw(canvas); }}
大家也看的出来,控件中有两个自定义属性,分别控制描边的颜色和描边的宽度,这当然需要在res/values/attrs下添加
<declare-styleable name="StroketText"> <attr name="outsideColor" format="color|reference" /> <attr name="stroketWidth" format="integer|reference" /> </declare-styleable>
最后在xml布局中使用即可
<!--默认字体--> <com.test.myView.StroketText android:id="@+id/tv_default" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="默认字体" android:textColor="@color/colorWhite" android:textSize="25dp" app:outsideColor="@color/colorAccent" app:stroketWidth="10" />
0 0
- 自定义控件--描边字体
- 批量使用字体样式:自定义字体控件
- Android自定义字体变色控件
- 自定义控件点滴3(字体相关)
- Flex自定制控件-给控件 字体描边
- [VB.NET]请问怎样修改自定义控件的缺省字体
- Activity 中获取所有控件 并设置自定义字体
- 【cocos2dx开发技巧9】自定义控件-定制字体工具
- ios9 解决计算字体/字数自定义控件高度的方法:
- Android 字体添加阴影效果 自定义控件也能用!
- WPF自定义控件与样式(1)-矢量字体图标(iconfont)
- 控件字体
- 自定义字体
- 自定义字体
- 自定义字体
- 自定义字体
- 自定义字体
- 自定义字体
- Spring组合属性
- Android Matrix的用法总结
- Opencv的Kalman滤波器函数二
- 初识linux
- Java设计模式之策略模式
- 自定义控件--描边字体
- Syntax error, insert "EnumBody" to complete ClassBodyDeclarations
- UGUI学习笔记(二) ScrollView相关
- 由自定义标签了解标签是如何起作用的
- Glide解析-图片转换过程
- DIO 数字量电路
- vue在windows环境搭建
- byte数组与int的互转
- MVC、MVP、MVVM-作者: 阮一峰