我是靠谱客的博主 执着画笔,最近开发中收集的这篇文章主要介绍TextView 描边效果,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.

 

 

package com.example.testproject;
 
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;
/*
 * StrokeTextView的目标是给文字描边
 * 实现方法是两个TextView叠加,只有描边的TextView为底,实体TextView叠加在上面
 * 看上去文字就有个不同颜色的边框了
 */
public class StrokeTextView extends TextView {
 
    private TextView borderText = null;///用于描边的TextView
 
    public StrokeTextView(Context context) {
        super(context);
        borderText = new TextView(context);
        init();
    }
 
    public StrokeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        borderText = new TextView(context,attrs);
        init();
    }
 
    public StrokeTextView(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(4);                                  //设置描边宽度 4px
        tp1.setStyle(Style.STROKE);                             //对文字只描边
        borderText.setTextColor(getResources().getColor(R.color.border_text));  //设置描边颜色
        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();
         
        //两个TextView上的文字必须一致
        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, bottom);
        borderText.layout(left, top, right, bottom);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        borderText.draw(canvas);
        super.onDraw(canvas);
    }
 
}

<color name="border_text" >#ffffff</color>

 

 

2.最近在android上搞一个带描边效果的TextView,网上搜索了下都是采用两个TextView在一个layout中进行实现,但细想了下,如果我在一个TextView中让其以不同的方式进行两次描绘不就能够达到效果了吗?由于网上没有看到和我类似的方法,故发出来和大家分享,由于只需要重写protected void onDraw(Canvas canvas);方法即可,故只附上onDraw的实现:

       private boolean m_bDrawSideLine = false; // 默认不采用描边  
       /* (non-Javadoc) 
 * @see android.widget.TextView#onDraw(android.graphics.Canvas) 
 */  
@Override  
protected void onDraw(Canvas canvas) {  
    if (m_bDrawSideLine) {  
                       // 描外层  
                       //super.setTextColor(Color.BLUE); // 不能直接这么设,如此会导致递归  
                       setTextColorUseReflection(Color.BLUE);  
                       m_TextPaint.setStrokeWidth(3);  // 描边宽度  
                       m_TextPaint.setStyle(Style.FILL_AND_STROKE); //描边种类  
                       m_TextPaint.setFakeBoldText(true); // 外层text采用粗体  
                       m_TextPaint.setShadowLayer(1, 0, 0, 0); //字体的阴影效果,可以忽略  
        super.onDraw(canvas);  
                         
  
                       // 描内层,恢复原先的画笔  
  
                       //super.setTextColor(Color.BLUE); // 不能直接这么设,如此会导致递归    
                       setTextColorUseReflection(Color.RED);  
                       m_TextPaint.setStrokeWidth(0);                           
                       m_TextPaint.setStyle(Style.FILL_AND_STROKE);                           
                       m_TextPaint.setFakeBoldText(false);                          
                       m_TextPaint.setShadowLayer(0, 0, 0, 0);                           
              }  
              super.onDraw(canvas);  
       }  
         
       private void setTextColorUseReflection(int color) {  
           Field textColorField;  
    try {  
    textColorField = TextView.class.getDeclaredField("mCurTextColor");  
    textColorField.setAccessible(true);  
    textColorField.set(color);  
    textColorField.setAccessible(false);  
    } catch (NoSuchFieldException e) {  
    e.printStackTrace();  
    } catch (IllegalArgumentException e) {  
    e.printStackTrace();  
    } catch (IllegalAccessException e) {  
    e.printStackTrace();  
    }  
           m_TextPaint.setColor(color);  
       }  
        以上代码中m_bDrawSideLine为false即和一般的textView无异,只需要将其改为true即可看到描边效果。

 

3.

package com.zg.mrcheney;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.widget.TextView;

public class StrokeText extends TextView {

private float mBigFontBottom;
private float mBigFontHeight;

private String text;
private Paint mPaint;
private int strokeSize = 1;

public StrokeText(Context context) {
super(context);
init();
}

public StrokeText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public StrokeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(getTextSize());
mPaint.setColor(getResources().getColor(R.color.font_paint_color));
FontMetrics fm = mPaint.getFontMetrics();
mBigFontBottom = fm.bottom;
mBigFontHeight = fm.bottom - fm.top;
}

@Override
protected void onDraw(Canvas canvas) {
if (strokeSize > 0 && strokeSize < 4) {
float y = getPaddingTop() + mBigFontHeight - mBigFontBottom;
canvas.drawText(text, 0, y - strokeSize, mPaint);
canvas.drawText(text, 0, y + strokeSize, mPaint);
canvas.drawText(text, 0 + strokeSize, y, mPaint);
canvas.drawText(text, 0 + strokeSize, y + strokeSize, mPaint);
canvas.drawText(text, 0 + strokeSize, y - strokeSize, mPaint);
canvas.drawText(text, 0 - strokeSize, y, mPaint);
canvas.drawText(text, 0 - strokeSize, y + strokeSize, mPaint);
canvas.drawText(text, 0 - strokeSize, y - strokeSize, mPaint);
}

super.onDraw(canvas);
}

@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
this.text = text.toString();
invalidate();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (strokeSize > 0 && strokeSize < 4) {
setMeasuredDimension(getMeasuredWidth() + strokeSize, getMeasuredHeight());
}
}

}

 

最后

以上就是执着画笔为你收集整理的TextView 描边效果的全部内容,希望文章能够帮你解决TextView 描边效果所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部