package com.sandftechnologies.schoolmanagement.CustomView; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; /** * Created by ASWIN on 7/15/2017. */ public class AttendanceCircleView extends View { private Paint backgroundPaint; private Paint whitePaint; private RectF bounds,boarderbounds; private float ViewHeight,ViewWidth; private float textsize; private Paint boarderPaint; private String nominator="",denominator=""; private Paint stringPaint; private DisplayMetrics metrics; private float boarderPaintWidth; @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); bounds=new RectF(); boarderbounds=new RectF(); ViewHeight=getMeasuredHeight(); ViewWidth=getMeasuredWidth(); if(ViewWidth>ViewHeight){ ViewWidth=ViewHeight; }else { ViewHeight=ViewWidth; } bounds.set(0,0,ViewWidth,ViewHeight); boarderbounds.set(boarderPaintWidth/2f,boarderPaintWidth/2f,ViewWidth-boarderPaintWidth/2f,ViewHeight-boarderPaintWidth/2f); textsize=ViewHeight/5; } public void setNominator(String s){ this.nominator=s; invalidate(); } public void setDenominator(String s){ this.denominator=s; invalidate(); } public AttendanceCircleView(Context context, AttributeSet attrs) { super(context, attrs); metrics = getResources().getDisplayMetrics(); int densityDpi = (int)(metrics.density * 160f); Log.d("$#####", "AttendanceCircleView: "+densityDpi); boarderPaintWidth = 4*(densityDpi/160); backgroundPaint=new Paint(); backgroundPaint.setAntiAlias(true); backgroundPaint.setStyle(Paint.Style.FILL); backgroundPaint.setColor(Color.parseColor("#3498db")); whitePaint=new Paint(); whitePaint.setAntiAlias(true); whitePaint.setStyle(Paint.Style.FILL); whitePaint.setColor(Color.parseColor("#ffffff")); whitePaint.setStrokeWidth(boarderPaintWidth); stringPaint=new Paint(); stringPaint.setAntiAlias(true); stringPaint.setStyle(Paint.Style.FILL); stringPaint.setColor(Color.parseColor("#ffffff")); stringPaint.setStrokeWidth(9); boarderPaint=new Paint(); boarderPaint.setAntiAlias(true); boarderPaint.setStyle(Paint.Style.STROKE); boarderPaint.setColor(Color.parseColor("#ffffff")); boarderPaint.setStrokeWidth(boarderPaintWidth); } @Override protected void onDraw(Canvas canvas) { canvas.drawOval(bounds,backgroundPaint); canvas.drawLine(ViewWidth*15/100,ViewHeight/2f,ViewWidth-(ViewWidth*15/100),ViewHeight/2f,whitePaint); float textwidth=whitePaint.measureText (nominator)+stringPaint.measureText(" absent"); float x=textwidth/2f; canvas.drawText(nominator,(ViewWidth/2f)-x,ViewHeight/2f-(ViewHeight*5/100),whitePaint); canvas.drawText(" absent",(((ViewWidth/2f)-x)+whitePaint.measureText (nominator)),ViewHeight/2f-(ViewHeight*5/100),stringPaint); float totalDenominatorLength=whitePaint.measureText (denominator)+stringPaint.measureText(" days"); float p=totalDenominatorLength/2f; float[] dimensArray=getDenominatorBounds(whitePaint,denominator,stringPaint," days"); canvas.drawText(denominator,((ViewWidth/2f)-p),(ViewHeight/2f+(ViewHeight*5/100)+dimensArray[1]),whitePaint); canvas.drawText(" days",(((ViewWidth/2f)-p)+whitePaint.measureText (denominator)),ViewHeight/2f+(ViewHeight*5/100)+dimensArray[3],stringPaint); canvas.drawOval(boarderbounds,boarderPaint); super.onDraw(canvas); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); ViewWidth=w; ViewHeight=h; if(ViewWidth>ViewHeight){ ViewWidth=ViewHeight; }else { ViewHeight=ViewWidth; } float desiredWidth=ViewWidth-(ViewWidth*(15/100)); setTextSizeForWidth(whitePaint,desiredWidth,"15",stringPaint); //15 is a dummy value for getting desired text size when scaling } private float[] getDenominatorBounds(Paint paint,String text, Paint paint2,String text2){ float[] dimArray=new float[]{1,1,1,1}; Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); dimArray[0]=bounds.width(); dimArray[1]=bounds.height(); Rect bounds2=new Rect(); paint2.getTextBounds(text2,0,text2.length(),bounds2); dimArray[2]=bounds2.width(); dimArray[3]=bounds2.height(); return dimArray; } private static void setTextSizeForWidth(Paint paint, float desiredWidth, String text,Paint stringPaint) { // Pick a reasonably large value for the test. Larger values produce // more accurate results, but may cause problems with hardware // acceleration. But there are workarounds for that, too; refer to // http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache final float testTextSize = 1f; // Get the bounds of the text, using our testTextSize. paint.setTextSize(testTextSize); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); // Calculate the desired size as a proportion of our testTextSize. float desiredTextSize; if(bounds.width()