标准自定义控件CirvleView
2016-06-07 09:59
204 查看
1.写一个类继承View
2.自定义属性CircleColor
3.重写onMeasure方法
4.重写onDraw方法
<?xml version=“1.0” encoding=“utf-8”?>
<resources>
<declare-styleable name=”CircleView”>
<attr name="circle_color" format="color"/>
</declare-styleable>
</resources>
public class CircleView extends View {
private int mColor =Color.RED;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public CircleView (Context context, AttributeSet attrs){
this(context,attrs,0)
public CircleView(Context context ,AttrbuteSet attrs, int deStyeAttr){
super (context,attrs,deStyleAttr);
TypedArray a= context.obtainStyledAttributes(attrs,R.styleable.CircleView);
mColor=a.getColor(R.styleable.CircleView_circle_color,Color.RED);
a.recycle();
init();
}
private void init(){
mPaint.setColor(mColor);
}
@override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize =MeasureSpec.getSize(withMeasureSpec);
int heightSpecMode =MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);
if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode ==MeasureSpec.AT_MOST){
setMeasuredDimension(200,200);
}else if(withSpecMode==MeasureSpec.AT_MOST)[
setMeasuredDimension(200,heightSpecSize);
} else if(heightMeasureMode == MeasureSpec.AT_MOST){
setMeasuredDimension(widthSpecSize,200);
}
}
@overtide
protected void onDraw (Canvas canvas){
super.onDraw(canvas);
final int paddingLeft =getPaddingLeft();
final int paddingRight=getPaddingRight();
final int paddingTop=getPaddingTop();
final int paddingBottom =getPaddingBottom();
int width =getWidth()-paddingLeft- paddingRight;
int height=getHeight()-paddingTop-paddingBottom;
int radius = Math.min(width,height)/2;
canvas.drawCircle(paddingLeft+width/2,paddingTop+height/2,radius,mPaint);
}
}
}
}
2.自定义属性CircleColor
3.重写onMeasure方法
4.重写onDraw方法
<?xml version=“1.0” encoding=“utf-8”?>
<resources>
<declare-styleable name=”CircleView”>
<attr name="circle_color" format="color"/>
</declare-styleable>
</resources>
public class CircleView extends View {
private int mColor =Color.RED;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public CircleView (Context context, AttributeSet attrs){
this(context,attrs,0)
public CircleView(Context context ,AttrbuteSet attrs, int deStyeAttr){
super (context,attrs,deStyleAttr);
TypedArray a= context.obtainStyledAttributes(attrs,R.styleable.CircleView);
mColor=a.getColor(R.styleable.CircleView_circle_color,Color.RED);
a.recycle();
init();
}
private void init(){
mPaint.setColor(mColor);
}
@override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize =MeasureSpec.getSize(withMeasureSpec);
int heightSpecMode =MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);
if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode ==MeasureSpec.AT_MOST){
setMeasuredDimension(200,200);
}else if(withSpecMode==MeasureSpec.AT_MOST)[
setMeasuredDimension(200,heightSpecSize);
} else if(heightMeasureMode == MeasureSpec.AT_MOST){
setMeasuredDimension(widthSpecSize,200);
}
}
@overtide
protected void onDraw (Canvas canvas){
super.onDraw(canvas);
final int paddingLeft =getPaddingLeft();
final int paddingRight=getPaddingRight();
final int paddingTop=getPaddingTop();
final int paddingBottom =getPaddingBottom();
int width =getWidth()-paddingLeft- paddingRight;
int height=getHeight()-paddingTop-paddingBottom;
int radius = Math.min(width,height)/2;
canvas.drawCircle(paddingLeft+width/2,paddingTop+height/2,radius,mPaint);
}
}
}
}
相关文章推荐
- NumberFormat
- Integer
- 针对RecyclerView打造属于你的LayoutManager
- hive
- 安装flume的步骤
- input number maxlength 失效 移动端使用
- 为什么要使用SLF4J而不是Log4J
- Struts2基于注解配置Action
- log4j 滚动日志 及 实现操作日志
- ADO.NET 学习链接
- 堆排序
- 一个完整的ant build.xml
- "內存碎片"疑似“内存泄漏”
- Android Double类型数据保留两位小数
- 由strcat函数引发的对char *a和char a[]以及sizeof和strlen
- javascript 数组的定义和数组的长度
- jQuery实现按钮点击遮罩加载及处理完后恢复的效果
- https+shiro+cas+springmvc
- C# unsafe(fixed) 介于 managed code & unmanaged code之间的特性(转)
- /proc/sysrq-trigger的功能 介绍