Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capability to set a background colour and change the direction clockwise/counter clockwise #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ proguard/

# Log Files
*.log

# Android Studio
*.iml
.idea
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

22 changes: 0 additions & 22 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/gradle.xml

This file was deleted.

38 changes: 0 additions & 38 deletions .idea/misc.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

95 changes: 0 additions & 95 deletions app/app.iml

This file was deleted.

8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 27
buildToolsVersion '26.0.2'

defaultConfig {
applicationId "se.kmdev.circularprogressbar"
minSdkVersion 19
targetSdkVersion 22
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand All @@ -21,5 +21,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:appcompat-v7:27.0.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CircularProgressBar extends View {
private int mViewWidth;
private int mViewHeight;

private final float mStartAngle = -90; // Always start from top (default is: "3 o'clock on a watch.")
private float mStartAngle = -90; // Start from top (default is: "3 o'clock on a watch.")
private float mSweepAngle = 0; // How long to sweep from mStartAngle
private float mMaxSweepAngle = 360; // Max degrees to sweep = full circle
private int mStrokeWidth = 20; // Width of outline
Expand All @@ -30,10 +30,14 @@ public class CircularProgressBar extends View {
private boolean mDrawText = true; // Set to true if progress text should be drawn
private boolean mRoundedCorners = true; // Set to true if rounded corners should be applied to outline ends
private int mProgressColor = Color.BLACK; // Outline color
private int mBackgroundColor = Color.BLACK; // Background color
private int mTextColor = Color.BLACK; // Progress text color
private boolean mClockWise = true;
private boolean hasBackground = false;

private final Paint mPaint; // Allocate paint outside onDraw to avoid unnecessary object creation


public CircularProgressBar(Context context) {
this(context, null);
}
Expand All @@ -50,31 +54,36 @@ public CircularProgressBar(Context context, AttributeSet attrs, int defStyleAttr
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initMeasurments();
initMeasurements();
drawOutlineArc(canvas);

if (mDrawText) {
drawText(canvas);
}
}

private void initMeasurments() {
private void initMeasurements() {
mViewWidth = getWidth();
mViewHeight = getHeight();
}

private void drawOutlineArc(Canvas canvas) {

final int diameter = Math.min(mViewWidth, mViewHeight);
final float pad = mStrokeWidth / 2.0;
final float pad = (float) (mStrokeWidth / 2.0);
final RectF outerOval = new RectF(pad, pad, diameter - pad, diameter - pad);

mPaint.setColor(mProgressColor);
mPaint.setStrokeWidth(mStrokeWidth);
mPaint.setAntiAlias(true);
mPaint.setStrokeCap(mRoundedCorners ? Paint.Cap.ROUND : Paint.Cap.BUTT);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(outerOval, mStartAngle, mSweepAngle, false, mPaint);
if(hasBackground) {
mPaint.setColor(mBackgroundColor);
mPaint.setStrokeCap(Paint.Cap.BUTT);
canvas.drawArc(outerOval, 0, mMaxSweepAngle, false, mPaint);
}
mPaint.setStrokeCap(mRoundedCorners ? Paint.Cap.ROUND : Paint.Cap.BUTT);
mPaint.setColor(mProgressColor);
canvas.drawArc(outerOval, mStartAngle, mClockWise ? mSweepAngle : -mSweepAngle, false, mPaint);
}

private void drawText(Canvas canvas) {
Expand All @@ -85,7 +94,7 @@ private void drawText(Canvas canvas) {

// Center text
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((mPaint.descent() + mPaint.ascent()) / 2)) ;
int yPos = (int) ((canvas.getHeight() / 2) - ((mPaint.descent() + mPaint.ascent()) / 2));

canvas.drawText(calcProgressFromSweepAngle(mSweepAngle) + "%", xPos, yPos, mPaint);
}
Expand All @@ -100,6 +109,7 @@ private int calcProgressFromSweepAngle(float sweepAngle) {

/**
* Set progress of the circular progress bar.
*
* @param progress progress between 0 and 100.
*/
public void setProgress(int progress) {
Expand Down Expand Up @@ -139,10 +149,44 @@ public void showProgressText(boolean show) {
/**
* Toggle this if you don't want rounded corners on progress bar.
* Default is true.
*
* @param roundedCorners true if you want rounded corners of false otherwise.
*/
public void useRoundedCorners(boolean roundedCorners) {
mRoundedCorners = roundedCorners;
invalidate();
}

public float getStartAngle() {
return mStartAngle;
}

public void setStartAnle(float angle) {
mStartAngle = angle;
}

public boolean isClockWise() {
return mClockWise;
}

public void setClockWise(boolean mClockWise) {
this.mClockWise = mClockWise;
}

public int getBackgroundColor() {
return mBackgroundColor;
}

public void setBackgroundColor(int mBackgroundColor) {
this.mBackgroundColor = mBackgroundColor;
}

public boolean isHasBackground() {
return hasBackground;
}

public void setHasBackground(boolean hasBackground) {
this.hasBackground = hasBackground;
}
}

Loading