字体描边 终极版

package com.joyodream.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint.Style;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 
 * 描边,默认是灰色描边
 * 
 * @author lipeilong
 *
 */
public class JDStrokeTextView extends TextView {
     
    private TextView borderText = null;
    private final int STROKE_WIDTH        = 6;  // 线宽度
 
    public JDStrokeTextView(Context context) {
        super(context);
        borderText = new TextView(context);
        init();
    }
 
    public JDStrokeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        borderText = new TextView(context,attrs);
        init();
    }
 
    public JDStrokeTextView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        borderText = new TextView(context,attrs,defStyle);
        init();
    }
 
    public void init(){
        TextPaint tp1 = borderText.getPaint(); 
        tp1.setStrokeWidth(STROKE_WIDTH);                                  
        tp1.setStyle(Style.STROKE);                             
        borderText.setTextColor(0xff333333);  
        borderText.setGravity(getGravity());
    }
 
    @Override
    public void setLayoutParams (ViewGroup.LayoutParams params){
        super.setLayoutParams(params);
        borderText.setLayoutParams(params);
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        CharSequence tt = borderText.getText();        
        if(tt== null || !tt.equals(this.getText())){
            borderText.setText(getText());
            this.postInvalidate();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        borderText.measure(widthMeasureSpec, heightMeasureSpec);
    }
 
    protected void onLayout (boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right+STROKE_WIDTH, bottom);
        borderText.layout(left, top, right+STROKE_WIDTH, bottom);// 总要,否则会截断部分文字
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        borderText.draw(canvas);
        super.onDraw(canvas);
    }
 
}

网上找的几种解决方案中,这个是最靠谱的了。使用系统自带的阴影也是可以实现部分效果,看情况选择使用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值