自定义ViewGroup基本步骤
- 写构造方法
- 写onLayout 确定子View的位置
- onMesure
根据子View的布局文件确定子View的大小,及位置 测量分为三种
EXACTLY 300dp matchParent
AT_MOST wrapContent
UNSPECIFIED 很少用
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
}
得到ViewGroup的parameter view.getLayoutParameter
LineLayout.LayoutParameters等onDraw()
先看一个圆饼图 主要就这么一个API
canvas.drawArc(mRectF ,start ,sweepAngle ,true ,mPaint);
看效果图
先准备一个bean类
public class PieDataEntity {
private String name;
private float value;
private float percent;
private int color = 0;
private float angle = 0;
public PieDataEntity(String name, float value, int color) {
this.name = name;
this.value = value;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
public float getpercent() {
return percent;
}
public void setpercent(float percent) {
this.percent = percent;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
}
public class PieChartView extends View{
private Paint mPaint;
private float mRadius;
private RectF mRectF ;
private int width ;
private int height ;
private List<PieDataEntity> mPieDataEntityList ;
public PieChartView(Context context) {
super(context);
init(context);
}
public PieChartView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mRectF = new RectF();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawPie(canvas);
}
private void drawPie(Canvas canvas) {
if (mPieDataEntityList == null){
return;
}
width = getMeasuredWidth()- getPaddingLeft() - getPaddingRight() ;
height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom() ;
mRadius = (float) (Math.min(width,height)/2*0.8);
mRectF.left = (width - 2*mRadius)/2 ;
mRectF.top = (height - 2*mRadius)/2 ;
mRectF.right = mRectF.left + 2* mRadius ;
mRectF.bottom = mRectF.top + 2* mRadius ;
float start = 180 ;
for (int i = 0 ; i< mPieDataEntityList.size();i++){
float sweepAngle = mPieDataEntityList.get(i).getValue()/mTotalValue*360-1;//每个扇形的角度
mPaint.setColor(mPieDataEntityList.get(i).getColor());
canvas.drawArc(mRectF ,start ,sweepAngle ,true ,mPaint);
start += sweepAngle+1 ;
}
}
private int mTotalValue ;
public void setData(List<PieDataEntity> pieList){
mPieDataEntityList = pieList ;
for (int i = 0 ; i< mPieDataEntityList.size() ; i++){
mTotalValue += mPieDataEntityList.get(i).getValue() ;
}
invalidate();
}
}
使用很简单
int [] colors = {R.color.red_color ,R.color.colorPrimary,R.color.colorAccent,R.color.green};
List<PieDataEntity> entities = new ArrayList<>();
for (int i = 0 ; i< 10 ;i ++){
PieDataEntity pieDataEntity = new PieDataEntity("hhh"+i,i+5,getResources().getColor(colors[i% colors.length]));
entities.add(pieDataEntity);
}
pieChart.setData(entities);