在android应用界面设计过程中,为了提高美观体验,有些布局、控件总会有圆角的背景。如果不是用图片作背景的话,就需要我们用xml文件来描绘圆角背景。但是,在实际工作过程中总会觉得这样会很麻烦!即使,另一个控件要改一个圆角的大小或者换一种背景颜色,就要重新写一个xml文件。所以,用代码来实现圆角背景就显得实用很多。
下面就是本人代码实现的一个支持带边框,可代码设置边框大小,厚度,颜色的工具类。拿出来与大家分享!
package com.hztbc.android.definedview;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
/**
* 生成圆角矩形背景
* @author Administrator
*
*/
public class ShapeDrawbleZG extends Drawable {
private static final int DEFAULT_WIDHT = 0;
private static final int DEFAULT_HEIGHT = 0;
private static final int DEFAULT_EDHWIDTH = 5;
private Paint p;
private Paint innerP;
private int width = DEFAULT_WIDHT;//背景宽度
private int height = DEFAULT_HEIGHT;//背景高度
private int r ;//圆角半径
private boolean isEdging = false;//是否带边框
private int edgWidth = DEFAULT_EDHWIDTH;//边框厚度
public ShapeDrawbleZG(View view,int radius,boolean isEdg){
initPaint();
getViewWH(view);
r=radius;
isEdging = isEdg;
}
private void initPaint(){
p = new Paint();
p.setColor(Color.YELLOW);
p.setStrokeJoin(Join.ROUND);
p.setStrokeCap(Cap.ROUND);
p.setStrokeWidth(3);
innerP = new Paint();
innerP.setColor(Color.WHITE);
innerP.setStrokeJoin(Join.ROUND);
innerP.setStrokeCap(Cap.ROUND);
innerP.setStrokeWidth(3);
}
boolean hasMeasured = false;
private void getViewWH(final View v){
if(null == v){
return;
}
v.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if(!hasMeasured){
width = v.getMeasuredWidth();
height = v.getMeasuredHeight();
hasMeasured = true;
}
return true;
}
});
}
public ShapeDrawbleZG setRadius(int radius){
this.r = radius;
invalidateSelf();
return this;
}
public ShapeDrawbleZG setEdgWidth(int width){
this.edgWidth = width;
if(isEdging){
invalidateSelf();
}
return this;
}
public ShapeDrawbleZG setEdgColor(int color){
p.setColor(color);
invalidateSelf();
return this;
}
public ShapeDrawbleZG setInnerColor(int color){
innerP.setColor(color);
invalidateSelf();
return this;
}
private void drawRect(Canvas canvas){
RectF rf = new RectF(0, 0, width, height);
canvas.drawRoundRect(rf, r, r, p);
}
private void drawEdg(Canvas canvas){
RectF rf = new RectF(edgWidth, edgWidth, width-edgWidth, height-edgWidth);
canvas.drawRoundRect(rf, r, r, innerP);
}
@Override
public void draw(Canvas canvas) {
drawRect(canvas);
if(isEdging){
drawEdg(canvas);
}
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}
}
以上是工具类代码,如下为测试代码:
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);btn = (Button) findViewById(R.id.other_button);
// 测试ShapeDrawble用法
// btn.setBackgroundDrawable(new ShapeDrawble(btn, 5, true).setEdgColor(
// Color.RED).setEdgWidth(5));
// 测试点击可变背景,即xml里的selector
StateListDrawable stalistDrawable = new StateListDrawable();// 获取对应的属性值 Android框架自带的属性 attr
stalistDrawable.addState(SelectorDrawbleZG.PRESSED, new ShapeDrawble(btn, 10, true));stalistDrawable.addState(new int[] {android.R.attr.state_pressed },new ShapeDrawble(btn, 10, true).setInnerColor(Color.RED));
// 没有任何状态时显示的图片,我们给它设置我空集合
stalistDrawable.addState(new int[] {},new ShapeDrawble(btn, 10, true).setInnerColor(Color.GRAY));
btn.setBackgroundDrawable(stalistDrawable);
}
}
没怎么写过博客,代码一片片的粘,真的很麻烦!测试代码只写了activity里面的onCreate方法,其它请自行完善吧!
这个工具已经到2.0版本,有机会再拿出来分享吧!可恶的sdc