本文实现一个如图所示的控件,包括两部分,左边的饼状图和中间的两个小方块,及右边的两行文字
实现起来比较简单,只是一些绘图API的调用
核心代码在onDraw函数里边,对静态控件进行绘制即可
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float centreX= getWidth()/5;
float centreY= getHeight()/2;
float textSize=getHeight()/7;
float width=(float)getWidth();
float height=(float)getHeight();
float halfSmallRec =((float)getHeight())*3/70;
percent =((float) mBigBallNumber)/(mBigBallNumber + mSmallBallNumber);
radius= Math.min(getWidth() * 1 / 8, getHeight() * 10 / 35);
rectf=new RectF((int)(centreX-radius),(int)(centreY-radius),(int)(centreX+radius),(int)(centreY+radius));
piePaint.setColor(mBigBallColor);
canvas.drawArc(rectf, 270, 360 * percent, true, piePaint);
piePaint.setColor(mSmallBallColor);
canvas.drawArc(rectf, 270 + 360 * percent, 360 - 360 * percent, true, piePaint);
颜色更改为大球的颜色*/
piePaint.setColor(mBigBallColor);
canvas.drawRect(width * 2 / 5 - halfSmallRec, height* 23/ 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height *23/ 60 + halfSmallRec, piePaint);
piePaint.setColor(mSmallBallColor);
canvas.drawRect(width * 2 / 5 - halfSmallRec, height * 37 / 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height * 37 / 60 + halfSmallRec, piePaint);
textPaint.setColor(getResources().getColor(typedValue.resourceId));
textPaint.setTextSize(textSize);
String strBig = strBigBallName + mBigBallNumber;
float textBigWidth =textPaint.measureText(strBig);
Paint.FontMetrics fontMetrics=textPaint.getFontMetrics();
canvas.drawText(strBig, width * 9 / 20 + textBigWidth / 2, height *23/ 60 - fontMetrics.top / 3, textPaint);
String strSmall = strSmallBallName + mSmallBallNumber;
float textUnderWidth=textPaint.measureText(strSmall);
canvas.drawText(strSmall,width*9/20+textUnderWidth/2,height*37/60-fontMetrics.top/3,textPaint);
textPaint.setColor(getResources().getColor(R.color.half_transparent));
String strBigPercent =" ("+ mPercentBigBall +")";
float bigPercent =textPaint.measureText(strBigPercent);
canvas.drawText(strBigPercent, width * 9 / 20+ textBigWidth + bigPercent /2, height*23 / 60-fontMetrics.top*1/3, textPaint);
String strSmallPercent =" ("+ mPercentSmallBall +")";
float smallPercent =textPaint.measureText(strSmallPercent);
canvas.drawText(strSmallPercent,width*9/20+textUnderWidth+ smallPercent /2,height*37/60-fontMetrics.top/3,textPaint);
}
Canvas 绘制文本时,使用FontMetrics对象,计算位置的坐标。参考:使用FontMetrics对象计算位置坐标
设置文字绘制以中心为起点开始绘制
textPaint.setTextAlign(Paint.Align.CENTER);
x的坐标好计算,y坐标需要按需使用FontMetrics几个属性即可
完整代码如下:
public class PieHalfView extends View {
private Paint piePaint;
private Paint textPaint;
private float radius;
private RectF rectf;
private float percent;
private int mBigBallColor, mSmallBallColor;
private int mBigBallNumber;
private int mSmallBallNumber;
private String mPercentBigBall;
private String mPercentSmallBall;
private TypedValue typedValue;
private String strBigBallName;
private String strSmallBallName;
public PieHalfView(Context context) {
super(context);
init(context);
}
public PieHalfView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public PieHalfView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
piePaint =new Paint();
piePaint.setAntiAlias(true);
piePaint.setStyle(Paint.Style.FILL);
textPaint=new Paint();
textPaint.setStyle(Paint.Style.STROKE);
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.CENTER);
mBigBallColor = 0xFF9CCA5D;
mSmallBallColor =0xFF5F7048;
typedValue=new TypedValue();
context.getTheme().resolveAttribute(R.attr.maintextclor,typedValue,true);
mBigBallNumber =1;
mSmallBallNumber =3;
mPercentBigBall ="40%";
mPercentSmallBall ="60%";
strBigBallName =getResources().getString(R.string.big);
strSmallBallName =getResources().getString(R.string.small);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float centreX= getWidth()/5;
float centreY= getHeight()/2;
float textSize=getHeight()/7;
float width=(float)getWidth();
float height=(float)getHeight();
float halfSmallRec =((float)getHeight())*3/70;
percent =((float) mBigBallNumber)/(mBigBallNumber + mSmallBallNumber);
radius= Math.min(getWidth() * 1 / 8, getHeight() * 10 / 35);
rectf=new RectF((int)(centreX-radius),(int)(centreY-radius),(int)(centreX+radius),(int)(centreY+radius));
piePaint.setColor(mBigBallColor);
canvas.drawArc(rectf, 270, 360 * percent, true, piePaint);
piePaint.setColor(mSmallBallColor);
canvas.drawArc(rectf, 270 + 360 * percent, 360 - 360 * percent, true, piePaint);
piePaint.setColor(mBigBallColor);
canvas.drawRect(width * 2 / 5 - halfSmallRec, height* 23/ 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height *23/ 60 + halfSmallRec, piePaint);
piePaint.setColor(mSmallBallColor);
canvas.drawRect(width * 2 / 5 - halfSmallRec, height * 37 / 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height * 37 / 60 + halfSmallRec, piePaint);
textPaint.setColor(getResources().getColor(typedValue.resourceId));
textPaint.setTextSize(textSize);
String strBig = strBigBallName + mBigBallNumber;
float textBigWidth =textPaint.measureText(strBig);
Paint.FontMetrics fontMetrics=textPaint.getFontMetrics();
canvas.drawText(strBig, width * 9 / 20 + textBigWidth / 2, height *23/ 60 - fontMetrics.top / 3, textPaint);
String strSmall = strSmallBallName + mSmallBallNumber;
float textUnderWidth=textPaint.measureText(strSmall);
canvas.drawText(strSmall,width*9/20+textUnderWidth/2,height*37/60-fontMetrics.top/3,textPaint);
textPaint.setColor(getResources().getColor(R.color.half_transparent));
String strBigPercent =" ("+ mPercentBigBall +")";
float bigPercent =textPaint.measureText(strBigPercent);
canvas.drawText(strBigPercent, width * 9 / 20+ textBigWidth + bigPercent /2, height*23 / 60-fontMetrics.top*1/3, textPaint);
String strSmallPercent =" ("+ mPercentSmallBall +")";
float smallPercent =textPaint.measureText(strSmallPercent);
canvas.drawText(strSmallPercent,width*9/20+textUnderWidth+ smallPercent /2,height*37/60-fontMetrics.top/3,textPaint);
}
public void setPercent(float percent1){
this.percent =percent1;
invalidate();
}
public void setColor(int mBigBallColor,int mSmallBallColor){
this.mBigBallColor =mBigBallColor;
this.mSmallBallColor =mSmallBallColor;
invalidate();
}
public void setOverRunner(String bigPecent, String smallPercent, int big, int small,
int bigColor, int smallColor){
this.mPercentBigBall = bigPecent;
this.mPercentSmallBall = smallPercent;
this.mBigBallNumber = big;
this.mSmallBallNumber = small;
this.mBigBallColor = bigColor;
this.mSmallBallColor = smallColor;
invalidate();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



