Last active
August 23, 2016 17:31
-
-
Save adneal/6210163 to your computer and use it in GitHub Desktop.
A custom ColorDrawable that can easily be implemented as a progress bar.
This file contains hidden or 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
/* | |
* Copyright 2013 Andrew Neal | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
*/ | |
package com.andrew.apollo.drawable; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.graphics.drawable.ColorDrawable; | |
import android.graphics.drawable.Drawable; | |
/** | |
* A custom {@link Drawable} that can easily be used as a progress bar. | |
* | |
* @author Andrew Neal ([email protected]) | |
*/ | |
public class ProgressDrawable extends ColorDrawable { | |
/** The drawable's bounds Rect */ | |
private final Rect mBounds = getBounds(); | |
/** Used to draw the progress bar */ | |
private final Paint mProgressPaint = new Paint(); | |
/** The upper range of the progress bar */ | |
public int max = 100; | |
/** The current progress */ | |
public int progress; | |
/** | |
* Constructor for <code>ProgessDrawable</code> | |
* | |
* @param color The color to use | |
*/ | |
public ProgressDrawable(int color) { | |
mProgressPaint.setColor(color); | |
mProgressPaint.setAntiAlias(true); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void draw(Canvas canvas) { | |
final int right = mBounds.right * progress / max; | |
canvas.drawRect(mBounds.left, mBounds.top, right, mBounds.bottom, mProgressPaint); | |
} | |
/** | |
* Set the range of the progress bar, 0 - max | |
* | |
* @param max The upper range of this progress bar | |
*/ | |
public void setMax(int max) { | |
this.max = max; | |
invalidateSelf(); | |
} | |
/** | |
* Set the current progress to the specified value | |
* | |
* @param progress The new progress, between 0 and {@link #mMax} | |
*/ | |
public void setProgress(int progress) { | |
this.progress = progress; | |
invalidateSelf(); | |
} | |
} |
This file contains hidden or 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
// Initialize the ProgressDrawable | |
final int bugDroid = Color.parseColor("#A4C639"); | |
final ProgressDrawable progressDrawable = new ProgressDrawable(bugDroid); | |
// Initialize the ActionBar | |
final ActionBar actionBar = getActionBar(); | |
actionBar.setBackgroundDrawable(progressDrawable); | |
actionBar.setTitle("0%"); | |
// Attach a progress listener | |
final SeekBar seekBar = (SeekBar) findViewById(android.R.id.progress); | |
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { | |
// Update the ProgressDrawable | |
progressDrawable.setProgress(progress); | |
// Update the ActionBar title | |
actionBar.setTitle(progress + "%"); | |
} | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
// Nothing to do | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
// Nothing to do | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment