Last active
April 19, 2021 18:41
-
-
Save SatyaSnehith/c0d81685c5bab04c464bf8cf869a7afd to your computer and use it in GitHub Desktop.
Progress Bar with drawable in Andorid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<View | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/progress_bar" | |
android:layout_width="match_parent" | |
android:layout_height="4dp" | |
android:layout_margin="10dp"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.view.View; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ProgressDrawable progressDrawable = new ProgressDrawable(); | |
View view = findViewById(R.id.progress_bar); | |
view.setBackground(progressDrawable); | |
view.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
progressDrawable.invalidateSelf(); | |
} | |
}); | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
//runs after 1 second | |
progressDrawable.setProgress(100); | |
} | |
}, 1000); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Canvas; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.graphics.PixelFormat; | |
import android.graphics.Rect; | |
import android.graphics.RectF; | |
import android.graphics.drawable.Animatable; | |
import android.graphics.drawable.Drawable; | |
import android.os.SystemClock; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
public class ProgressDrawable extends Drawable implements Drawable.Callback, Animatable { | |
private final Paint bPaint, pPaint; | |
private final Path bPath, pPath; | |
private final RectF rectF, pRectF; | |
private Rect bounds; | |
int radius; | |
int from, to, current, speed; | |
private boolean isRunning; | |
private static final long FRAME_DURATION = 1000 / 60; | |
public ProgressDrawable() { | |
//increment of width of progress rectangle (number of pixels) per frame. | |
speed = 4; | |
to = -1; | |
bPaint = new Paint(); | |
bPaint.setStyle(Paint.Style.FILL); | |
bPaint.setAntiAlias(true); | |
bPaint.setColor(0xFFE0E0E0); | |
pPaint = new Paint(); | |
pPaint.setStyle(Paint.Style.FILL); | |
pPaint.setAntiAlias(true); | |
pPaint.setColor(0xFF1565C0); | |
pPath = new Path(); | |
pPath.setFillType(Path.FillType.EVEN_ODD); | |
bPath = new Path(); | |
bPath.setFillType(Path.FillType.EVEN_ODD); | |
rectF = new RectF(); | |
pRectF = new RectF(); | |
setCallback(this); | |
} | |
@Override | |
protected void onBoundsChange(Rect bounds) { | |
pPath.reset(); | |
bPath.reset(); | |
this.bounds = bounds; | |
radius = bounds.height() / 2; | |
rectF.set(bounds.left, bounds.top, bounds.right, bounds.bottom); | |
bPath.addRoundRect(rectF, radius, radius, Path.Direction.CW); | |
} | |
@Override | |
public void draw(@NonNull Canvas canvas) { | |
if (bounds == null) { | |
bounds = getBounds(); | |
} | |
//resetting the progress rectangle and adding with new width | |
pPath.reset(); | |
pRectF.set(bounds.left, bounds.top, current, bounds.bottom); | |
pPath.addRoundRect(pRectF, radius, radius, Path.Direction.CW); | |
//drawing the background | |
canvas.drawPath(bPath, bPaint); | |
//drawing the progress | |
canvas.drawPath(pPath, pPaint); | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
pPaint.setAlpha(alpha); | |
} | |
@Override | |
public void setColorFilter(@Nullable ColorFilter colorFilter) { | |
pPaint.setColorFilter(colorFilter); | |
} | |
@Override | |
public int getOpacity() { | |
return PixelFormat.TRANSLUCENT; | |
} | |
@Override | |
public void invalidateDrawable(@NonNull Drawable who) { | |
final Callback callback = getCallback(); | |
if (callback != null) { | |
callback.invalidateDrawable(this); | |
} | |
} | |
@Override | |
public void scheduleDrawable(@NonNull Drawable who, @NonNull Runnable what, long when) { | |
invalidateDrawable(who); | |
} | |
@Override | |
public void unscheduleDrawable(@NonNull Drawable who, @NonNull Runnable what) { | |
} | |
private final Runnable updater = new Runnable() { | |
@Override | |
public void run() { | |
current += speed; | |
if (current >= to) { | |
stop(); | |
} | |
invalidateSelf(); | |
if (isRunning) | |
scheduleSelf(updater, SystemClock.uptimeMillis() + FRAME_DURATION); | |
} | |
}; | |
public void setProgress(int progress) { | |
if (to == -1) { | |
to = (int) pRectF.left; | |
} | |
from = to; | |
to = bounds.right - (int)(((100 - progress) * bounds.width()) / 100); | |
current = from; | |
start(); | |
} | |
@Override | |
public void start() { | |
if (!isRunning()) { | |
isRunning = true; | |
scheduleSelf(updater, SystemClock.uptimeMillis() + FRAME_DURATION); | |
invalidateSelf(); | |
} | |
} | |
@Override | |
public void stop() { | |
if (isRunning()) { | |
unscheduleSelf(updater); | |
isRunning = false; | |
} | |
} | |
@Override | |
public boolean isRunning() { | |
return isRunning; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment