自定义圆的半径(主类)
package com.baidu.myviews;
import com.baidu.myattrs.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View{
int radius;
//自定义一个空间
public final static String NAMESPACE="myview";
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//不需要自定义空间
//TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyView);
//radius=ta.getInt(R.styleable.MyView_radius, 0);
radius=attrs.getAttributeIntValue(NAMESPACE, "radius", 0);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint=new Paint();
paint.setColor(Color.GREEN);
paint.setTextSize(20);
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2, radius, paint);
}
}