字体描边+android,android描边文字的按钮

public class BorderButton extends Button {

protected Paint paint;

protected int color;

protected int borderColor;

public BorderButton(Context context) {

super(context);

afterInit(context, null);

}

public BorderButton(Context context, AttributeSet attrs)

{

super(context, attrs);

afterInit(context, attrs);

}

public BorderButton(Context context, AttributeSet attrs, int

defStyle) {

super(context, attrs, defStyle);

// TODO Auto-generated constructor stub

afterInit(context, attrs);

}

private void afterInit(Context context, AttributeSet attrs)

{

if (attrs != null) {

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.BorderButton);

borderColor = a.getColor(R.styleable.BorderButton_borderColor,

0);

a.recycle();

} else {

borderColor = 0;

}

ColorStateList clors = getTextColors();

color = clors.getColorForState(getDrawableState(), 0);

paint = new Paint(); // 设置一个画笔

paint.setAntiAlias(true);

paint.setStrokeJoin(Paint.Join.ROUND);

paint.setStrokeCap(Paint.Cap.ROUND);

}

@Override

public void setTextColor(int color) {

this.color = color;

super.setTextColor(color);

}

@Override

public void setTextColor(ColorStateList colors) {

super.setTextColor(colors);

color = colors.getColorForState(getDrawableState(), 0);

}

public void setBorderColor(int color) {

borderColor = color;

}

public int getExtendedPaddingLeft() {

int left = getCompoundPaddingLeft();

int right = getCompoundPaddingRight();

int viewwh = getWidth() - left - right;

int layoutwh = (int) getLayout().getLineWidth(0);

//注意这里就只支持一行了

if (layoutwh >= viewwh) {

return left;

}

final int gravity = getGravity() &

Gravity.HORIZONTAL_GRAVITY_MASK;

if (gravity == Gravity.LEFT) {

return left;

} else if (gravity == Gravity.RIGHT) {

return left + viewwh - layoutwh;

} else { // (gravity == Gravity.CENTER_VERTICAL)

return left + (viewwh - layoutwh) / 2;

}

}

public int getExtendedPaddingRight() {

int left = getCompoundPaddingLeft();

int right = getCompoundPaddingRight();

int viewwh = getWidth() - left - right;

int layoutwh = (int) getLayout().getLineWidth(0);

//注意这里就只支持一行了

if (layoutwh >= viewwh) {

return right;

}

final int gravity = getGravity() &

Gravity.HORIZONTAL_GRAVITY_MASK;

if (gravity == Gravity.LEFT) {

return right + viewwh - layoutwh;

} else if (gravity == Gravity.RIGHT) {

return right;

} else { // (gravity == Gravity.CENTER_VERTICAL)

return right + (viewwh - layoutwh) / 2;

}

}

private int getHorizentalOffset(boolean forceNormal) {

int hoffset = 0;

int mGravity = getGravity();

final int gravity = mGravity &

Gravity.HORIZONTAL_GRAVITY_MASK;

Layout l = getLayout();

if (gravity != Gravity.LEFT) {

int boxwh = getMeasuredWidth() -

getExtendedPaddingLeft()

- getExtendedPaddingRight();

int textwh = l.getWidth();

if (textwh < boxwh) {

if (gravity == Gravity.RIGHT)

hoffset = boxwh - textwh;

else

// (gravity == Gravity.CENTER_VERTICAL)

hoffset = (boxwh - textwh) >> 1;

}

}

return hoffset;

}

private int getVerticalOffset(boolean forceNormal) {

int voffset = 0;

int mGravity = getGravity();

final int gravity = mGravity &

Gravity.VERTICAL_GRAVITY_MASK;

Layout l = getLayout();

if (gravity != Gravity.TOP) {

int boxht;

boxht = getMeasuredHeight() - getExtendedPaddingTop()

- getExtendedPaddingBottom();

int textht = l.getHeight();

if (textht < boxht) {

if (gravity == Gravity.BOTTOM)

voffset = boxht - textht;

else

// (gravity == Gravity.CENTER_VERTICAL)

voffset = (boxht - textht) >> 1;

}

}

return voffset;

}

@Override

protected void onDraw(Canvas canvas) {

//一些TextView的变量,奇怪protected的但是不能直接用,就获得过来了

int mScrollX = getScrollX();

int mScrollY = getScrollY();

int mRight = getRight();

int mLeft = getLeft();

int mBottom = getBottom();

int mTop = getTop();

// Draw the background for this view

// super.onDraw(canvas);

final int compoundPaddingLeft =

getCompoundPaddingLeft();

final int compoundPaddingRight =

getCompoundPaddingRight();

final int scrollX = mScrollX;

final int scrollY = mScrollY;

final int right = mRight;

final int left = mLeft;

final int bottom = mBottom;

final int top = mTop;

int extendedPaddingTop = getExtendedPaddingTop();

int extendedPaddingBottom = getExtendedPaddingBottom();

float clipLeft = compoundPaddingLeft + scrollX;

float clipTop = extendedPaddingTop + scrollY;

float clipRight = right - left - compoundPaddingRight +

scrollX;

float clipBottom = bottom - top - extendedPaddingBottom +

scrollY;

canvas.clipRect(clipLeft - 1, clipTop - 1, clipRight +

1,

clipBottom + 1);

int voffsetText = 0;

int hoffsetText = 0;

int mGravity = getGravity();

// translate in by our padding

{

if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) !=

Gravity.TOP) {

voffsetText = getVerticalOffset(false);

}

if ((mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) !=

Gravity.LEFT) {

hoffsetText = getHorizentalOffset(false);

}

canvas.translate(getExtendedPaddingLeft() + hoffsetText,

extendedPaddingTop + voffsetText);

}

canvas.translate(0, 0); // 将位置移动画纸的坐标点:0,0

// 使用path绘制路径文字

canvas.save();

Path path = new Path();

path.moveTo(0, getLayout().getLineBaseline(0)); // 移动到

坐标10,10

path.lineTo(getLayout().getLineWidth(0),

getLayout().getLineBaseline(0));

Paint citePaint = new Paint(paint);

citePaint.setTextSize(getTextSize());

//绘制边

if ((borderColor & 0xff000000) != 0) {

citePaint.setStyle(Style.STROKE);

citePaint.setColor(borderColor);

citePaint.setStrokeWidth(3);

canvas.drawTextOnPath(getText().toString(), path, 0, 0,

citePaint);

}

//绘制字本身

if ((color & 0xff000000) != 0) {

citePaint.setStyle(Style.FILL);

citePaint.setColor(color);

citePaint.setStrokeWidth(1);

canvas.drawTextOnPath(getText().toString(), path, 0, 0,

citePaint);

}

canvas.restore();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值