Android我的文字描边处理

前言

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

方法一、

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

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

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

方法二:

继续百度终于找到一个不一样的了: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;
        }

    }

    @Override
    protected 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叠加展示:

<FrameLayout
        android: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.StrokeTextView
            android: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.StrokeTextView
            android: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各种大佬的博客解决了无数问题,于是我也来一篇,希望对各位有所帮助)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值