android TextView 的圆角背景

实现方法:

定义类继承自Shape画成圆角矩形

public class MyShape extends Shape{

        @Override
        public void draw(Canvas canvas, Paint paint) {
            // TODO Auto-generated method stub
            paint.setColor(Color.RED);

            canvas.drawRoundRect(new RectF(0, 0, 200, 30), 8, 8, paint);
        }
        
    }


ShapeDrawable mShapeDrawable = new ShapeDrawable();    
        mShapeDrawable.setShape(new MyShape());
        textview.setBackgroundDrawable(mShapeDrawable);


自定义一个继承自TextView的类。重写onDraw方法,在onDraw方法中获得

Textview的实际大小,然后把参数传给RectF就可以画一个跟View大小一致的圆角矩形了。


这里有一个例子(还包含了写其他功能的代码)

package com.textview.style;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class PatchView_New extends TextView{
	
	private static final String TAG = "lxy";
	private static final float ELLIPSIZE_COEFFICIENT = 12.0f;
	
	private RectF mRect ;
	private int mColor = Color.YELLOW;
	private ShapeDrawable mShapeDrawable;
	
	private String realString;
	private String showString;
	private int _num;
	
	public PatchView_New(Context context) {
		super(context);
		setWillNotDraw(false);
		drawDefaultBGColor();
		setSingleLine(true);
		//setEllipsize(TextUtils.TruncateAt.valueOf("MIDDLE"));
		Log.d(TAG, "__________________ PatchView_New().1 _____________________");
	}
	
	public PatchView_New(Context context, AttributeSet attrs) {
		super(context, attrs);
		setWillNotDraw(false);
		drawDefaultBGColor();
		setSingleLine(true);
		//setEllipsize(TextUtils.TruncateAt.valueOf("MIDDLE"));
		Log.d(TAG, "__________________ PatchView_New().2 _____________________");
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.d(TAG, "__________________ onDraw() _____________________ mRect = " + mRect);
		if(mRect == null){
			mRect = new RectF();
		}
		//mRect.left = this.getLeft();
		mRect.right = this.getRight() - this.getLeft();
		//mRect.top = this.getTop();
		mRect.bottom = this.getBottom() - this.getTop();
		Log.d(TAG, "mRect = " + mRect);
		
		Log.d(TAG, "______________ _num = " + _num);
		if(_num >= 1){
			//_num = 0;
			return;
		}
		_num ++;
		if(mRect.width() != 0 && mRect.height() != 0){
			showString = getRightDisplayString(realString, getAvail());
			Log.d(TAG, "onDraw()_______________showString = " + showString);
			this.setText(showString);
		}
	}
	
	public void drawDefaultBGColor(){
		if(mShapeDrawable == null){
			mShapeDrawable = new ShapeDrawable();	
		}
		mShapeDrawable.setShape(new MyShape());
		setBackgroundDrawable(mShapeDrawable);
	}
	
	
	
	public void setMultiViewText(String headStr,
							String centerStr,  
							String tailStr){
		_num = 0;
		Log.d(TAG, "__________________ setMultiViewText() _____________________" + centerStr);
		realString = centerStr;
		setText(realString);
		
		
	}
	
	public void setCenterViewBGColor(int color){
		_num = 0;
		Log.d(TAG, "__________________ setCenterViewBGColor() _____________________" + color);
		this.mColor = color;
		
	}
	
	
	
	
	
/*------------------------- 字符截断 START ---------------------- ----------*/
	
	private String getRightDisplayString(String real, float width){
		if(TextUtils.isEmpty(real)){
			return "";
		}
		return TextUtils.ellipsize(real, new TextPaint(), width, 
				TextUtils.TruncateAt.valueOf("MIDDLE")).toString();
	}
	
	private float getAvail(){
		return ELLIPSIZE_COEFFICIENT * mRect.width()/getTextSize();
	}
/*------------------------- END  ---------------------------------------*/	
	
	
	
	
/*====================== Inner Class START===========================*/
	
	private class MyShape extends Shape{

		@Override
		public void draw(Canvas canvas, Paint paint) {
			// TODO Auto-generated method stub
			paint.setColor(mColor);
			Log.d(TAG, "__________________ draw() _____________________ mRect = " + mRect);
			if(mRect == null){
				mRect = new RectF();
			}
			canvas.drawRoundRect(mRect, 8f, 8f, paint);
		}
		
	}
/*============================ END===================================*/
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值