Skip to content

Instantly share code, notes, and snippets.

@TheReprator
Last active July 28, 2017 02:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TheReprator/6f0861146cfaaa1c90630fc57cc27e0c to your computer and use it in GitHub Desktop.
Save TheReprator/6f0861146cfaaa1c90630fc57cc27e0c to your computer and use it in GitHub Desktop.
Double Arc progressbar with text inside
<declare-styleable name="CircleArcProgress">
<attr name="circle_size" format="dimension"/>
<attr name="color_circle" format="color"/>
<attr name="arc_radius" format="dimension"/>
<attr name="arc_color" format="color"/>
<attr name="arc_text" format="string"/>
<attr name="arc_text_size" format="dimension"/>
</declare-styleable>
package com.singh.daman.proprogressviews;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
public class CircleArcProgress extends View {
private Paint paint=new Paint();
private int startAngle=120;
private int startAngle2=240;
private RectF oval=new RectF();
private int sweepAngle=100;
private float in_rad;
private float out_rad;
private int colorArc;
private int colorArc2;
private String text = "TheGreat0004";
private Paint mPaint;
public CircleArcProgress(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array=context.getTheme().obtainStyledAttributes(attrs,R.styleable.CircleArcProgress,0,0);
float textSize = 30;
try{
String text = array.getString(R.styleable.CircleArcProgress_arc_text);
if(!TextUtils.isEmpty(text))
this.text = text;
colorArc=array.getColor(R.styleable.CircleArcProgress_color_circle, Color.parseColor("#5C6BC0"));
//out_rad=array.getDimension(R.styleable.CircleArcProgress_arc_radius,100);
colorArc2=array.getColor(R.styleable.CircleArcProgress_arc_color, Color.parseColor("#1A237E"));
float nTextSize = array.getDimension(R.styleable.CircleArcProgress_arc_text_size,0);
if(0.0 != nTextSize) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
textSize = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, nTextSize, metrics) / metrics.density);
}
}
catch (Exception e){
e.printStackTrace();
}
finally {
array.recycle();
}
mPaint = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
//mPaint.setMaskFilter(new BlurMaskFilter(1, BlurMaskFilter.Blur.NORMAL));
// mPaint.setFakeBoldText(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setColor(Color.RED);
mPaint.setTextSize(textSize);
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
mPaint.setTextAlign(Paint.Align.CENTER);
paint.setStyle(Paint.Style.STROKE);
post(animator);
}
private int measureHeight(int measureSpec, int measuredWidth) {
int size = getPaddingTop() + getPaddingBottom();
size += mPaint.getFontSpacing();
return resolveSizeAndState(measuredWidth + size, measureSpec, 0);
}
private int measureWidth(int measureSpec) {
int size = getPaddingLeft() + getPaddingRight();
Rect bounds = new Rect();
mPaint.getTextBounds(text, 0, text.length(), bounds);
size += bounds.width();
size *= 2;
return resolveSizeAndState(size, measureSpec, 0);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = measureWidth(widthMeasureSpec);
setMeasuredDimension(widthSize, measureHeight(heightMeasureSpec,widthSize));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
in_rad = w/2 - 15;
out_rad = in_rad + 10;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(colorArc);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(getWidth()/2,getHeight()/2, in_rad, paint);
int x = getWidth()/ 2;
int y = (int) ((getHeight() - (mPaint.descent() + mPaint.ascent())) / 2);
canvas.drawText(text, x, y,mPaint);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(colorArc2);
paint.setStrokeWidth(10);
oval.set(getWidth()/2-out_rad,getHeight()/2-out_rad,getWidth()/2+out_rad,getHeight()/2+out_rad);
canvas.drawArc(oval,startAngle,sweepAngle,false,paint);
canvas.drawArc(oval,startAngle2,sweepAngle,false,paint);
}
Runnable animator=new Runnable() {
@Override
public void run() {
if(startAngle<=360){
startAngle+=10;
}
else{
startAngle=1;
}
if(startAngle2>=1){
startAngle2-=15;
}
else{
startAngle2=360;
}
invalidate();
postDelayed(this,30);
}
};
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<views.CircleArcProgress
app:arc_text_size = "12sp"
app:arc_text = "Vikram Singh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<views.CircleArcProgress
app:arc_text_size = "10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment