我是靠谱客的博主 炙热小蚂蚁,最近开发中收集的这篇文章主要介绍java描边_Android TextView文字描边的实现!!,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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());

}

}

}

最后

以上就是炙热小蚂蚁为你收集整理的java描边_Android TextView文字描边的实现!!的全部内容,希望文章能够帮你解决java描边_Android TextView文字描边的实现!!所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部