Android我的文字描边处理

前言

首先在网上百度了很久基本都是一个方法一种模板出来的,关键是不知道为啥这方法在一部分手机上有问题,这里分享一下找到的方法和遇到的问题。

方法一、

随便找了个基本差不多 实现android文字描边功能
但是我的项目在部分手机中出现描边发生偏移的问题:
android文字描边
发现这种方式是用这个定位的:

@Overrideprotected 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);}

继续百度发现大多数描边处理都是用这种方式,但是我项目中不知道为啥就是不行。

方法二:

继续百度终于找到一个不一样的了:Android文字描边效果实现
这个方法就简单明确了一出问题瞬间知道在哪儿了:
又是在某些手机中 自定义的TextView和原本TextView的换行规则不用导致一行最后一个字前面有空格的话TextView就要自动换行而自定义的TextView却是顶满了才换行,这样换行后又错位了。
我的解决办法:
使用2个自定义的TextView来重合一个样式为paint.setStyle(Paint.Style.FILL)填充,一个样式为描边paint.setStyle(Paint.Style.STROKE);

1、自定义TextView:

//文字描边
public class StrokeTextView extends AppCompatTextView {private int mStrokeColor = Color.WHITE;private float mStrokeWidth = 0;private int mStyle=0;public StrokeTextView(Context context) {this(context, null);}public StrokeTextView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public StrokeTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);if (attrs != null) {TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);mStrokeColor = a.getColor(R.styleable.StrokeTextView_strokeColor1, Color.WHITE);mStrokeWidth = a.getDimension(R.styleable.StrokeTextView_strokeWidth1, 0f);mStyle = a.getInteger(R.styleable.StrokeTextView_strokeStyle,0);a.recycle();}setTextColor(mStrokeColor);TextPaint paint = getPaint();switch ((int)mStyle) {case 0:paint.setStyle(Paint.Style.FILL);paint.setStrokeWidth(mStrokeWidth);break;case 1:// 只有描边paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(mStrokeWidth);break;case 2:paint.setStyle(Paint.Style.FILL_AND_STROKE);paint.setStrokeWidth(mStrokeWidth);break;}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}
}

2、自定义属性:

  <declare-styleable name="StrokeTextView"><attr name="strokeColor1" format="color" /><attr name="strokeWidth1" format="dimension" /><attr name="strokeStyle" format="integer" />declare-styleable>

3、 2个自定义TextView叠加展示:

<FrameLayoutandroid:layout_gravity="center_horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="@dimen/dp_10"android:paddingRight="@dimen/dp_30"android:layout_alignParentBottom="true"><com.cqtimes.sknews.util.StrokeTextViewandroid:id="@+id/bannerTitle"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="30dp"android:ellipsize="end"android:lineSpacingExtra="2dp"android:maxLines="2"android:minLines="2"android:textStyle="bold"android:textColor="#ffffff"android:textSize="20sp"app:strokeStyle="0"/><com.cqtimes.sknews.util.StrokeTextViewandroid:id="@+id/bannerTitle_b"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="30dp"android:ellipsize="end"android:lineSpacingExtra="2dp"android:maxLines="2"android:minLines="2"android:textStyle="bold"android:textColor="#ffffff"android:textSize="20sp"app:strokeColor1="@color/black"app:strokeWidth1="0.5dp"app:strokeStyle="1"/>FrameLayout>

(白嫖CSDN各种大佬的博客解决了无数问题,于是我也来一篇,希望对各位有所帮助)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部