概述
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.text.TextPaint; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.TextView; /** * Created by admin on 2018/1/25. */ public class StrokeTextView extends TextView { private TextView outlineTextView = null; public StrokeTextView(Context context) { super(context); outlineTextView = new TextView(context); init(); } public StrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); outlineTextView = new TextView(context, attrs); init(); } public StrokeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); outlineTextView = new TextView(context, attrs, defStyle); init(); } public void init() { TextPaint paint = outlineTextView.getPaint(); paint.setStrokeWidth(3);// 描边宽度 paint.setStyle(Paint.Style.STROKE); outlineTextView.setTextColor(Color.parseColor("#000000"));// 描边颜色 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); } }
最后
以上就是酷炫西牛为你收集整理的安卓文字加描边的全部内容,希望文章能够帮你解决安卓文字加描边所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复