/**
*
* 转载请标明出处:http://blog.csdn.net/u013598111/article/details/50198101
* @author:【JunTao_sun】
*
*
*/
A Drawable object that draws primitive shapes. A ShapeDrawable takes a Shape
object and manages its presence on
the screen. If no Shape is given, then the ShapeDrawable will default to a RectShape
.
This object can be defined in an XML file with the <shape>
element.
Drawable 对象是可以画初始的外形, shapeDrawable 用shape对象 管理出现在屏幕上控件外形。
如果不给定现状,默认ShapeDrawable 会用矩形的现状
这个类可以被定义在XML文件里 也就是drawable目录下 文件下的 元素用<shape>
/**
* Called from the drawable's draw() method after the canvas has been set
* to draw the shape at (0,0). Subclasses can override for special effects
* such as multiple layers, stroking, etc.
*/
设置图形在画布在0 0点后 调用此方法 子类可以重写这个方法 来指定特殊的效果
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
shape.draw(canvas, paint);
}
关于ShapeDrawable的使用,分为两个部分
一、是使用已经存在的ShapeDrawable子类
(1)首先声明ShapeDrawable对象,有子类实例化。
(2)设置ShapeDrawable对象的Color或者Shader,Shader是一个渲染图形的类
(3)在View中的onDraw()方法中(重点),利用ShapeDrawable.setBounds()方法设置绘图区域,
ShapeDrawable.draw(Canvas)把ShapeDrawable对象画到画布的指定位置
二、重新构造自己的ShapeDrawable子类
必须重写onDraw()方法。其他的如上
public class myview extends TextView{
private RectF mRect ;
private int mColor = 0xaa0000ff;
private ShapeDrawable mShapeDrawable;
private Paint mPaint;
public myview(Context context) {
this(context,null);
}
public myview(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
drawDefaultBGColor();
setSingleLine(true);
mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setDither(true);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setTextSize(18);
}
private int textHeight;
private void measureText(){
FontMetrics fm=mPaint.getFontMetrics();
textHeight=(int) (fm.descent+fm.ascent);
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas); 自己画
measureText();
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();
String show=(String) getText();
canvas.drawText(show, caculateStartDrawText(), getHeight()/2-textHeight/2, mPaint);
}
/**
* 创建ShapeDrawable 设置shape参数
*/
public void drawDefaultBGColor(){
if(mShapeDrawable == null){
mShapeDrawable = new ShapeDrawable();
}
mShapeDrawable.setShape(new MyShape());
setBackground(mShapeDrawable);
}
/**
* 计算坐边距 因为画矩形或者圆 最大距离是半径 防止字体跑出边界
* @return
*/
private int caculateStartDrawText(){
int h=getHeight();
int radius=h>>1;
return radius;
}
/**
*
* shape子类 实现draw方法 在方法里画roundRect
* @author Administrator
*
*/
private class MyShape extends Shape{
@Override
public void draw(Canvas canvas, Paint paint) {
// TODO Auto-generated method stub
// paint.setColor(mColor);
paint.setStyle(Paint.Style.STROKE);
if(mRect == null){
mRect = new RectF();
}
canvas.drawRoundRect(mRect, 180, 180, paint);
}
}
一般不在自定义控件设置 会导致字体越界 不好控制,还是在xml布局定义比较方便。
final ShapeDrawable shapedrawable=new ShapeDrawable();
shapedrawable.getPaint().setStyle(Paint.Style.STROKE);
shapedrawable.setShape(new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawRoundRect(new RectF(0, 0, 50, 50), 20, 20, paint);
}
});
text.setBackground(shapedrawable);
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="20dp" />
<stroke
android:width="1px"
android:color="#ff0000ff" />
<gradient android:startColor="#ff00ff00" />
<padding android:left="10dp" />
<solid android:color="#ff00ff00" />
<size android:width="50dp" />
</shape>