Skip to content

Instantly share code, notes, and snippets.

@ixiyang
Last active August 29, 2015 14:02
Show Gist options
  • Save ixiyang/659725d11011e6edf52a to your computer and use it in GitHub Desktop.
Save ixiyang/659725d11011e6edf52a to your computer and use it in GitHub Desktop.
package net.yscs.android.square_progressbar_example.dialogs;
import net.yscs.android.square_progressbar_example.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
/**
* This gives the user the possibility to set a custom colour to the
* SquareProgressBar by selecting a RGB-colour.
*
* @author yansigner
* @since 1.4.0
*/
public class CustomColourDialog extends Dialog {
private final Button saveButton;
private SeekBar rSeekBar;
private SeekBar gSeekBar;
private SeekBar bSeekBar;
private int choosenRGB;
public CustomColourDialog(final Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.customcolourdialog);
this.setCancelable(false);
Button closeButton = (Button) this
.findViewById(R.id.returnColourDialog);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
saveButton = (Button) this.findViewById(R.id.shareColourDialog);
rSeekBar = (SeekBar) findViewById(R.id.rSeekBar);
rSeekBar.setMax(255);
rSeekBar.setProgress(111);
rSeekBar.setOnSeekBarChangeListener(rgbOnSeekBarListener());
gSeekBar = (SeekBar) findViewById(R.id.gSeekBar);
gSeekBar.setMax(255);
gSeekBar.setProgress(111);
gSeekBar.setOnSeekBarChangeListener(rgbOnSeekBarListener());
bSeekBar = (SeekBar) findViewById(R.id.bSeekBar);
bSeekBar.setMax(255);
bSeekBar.setProgress(111);
bSeekBar.setOnSeekBarChangeListener(rgbOnSeekBarListener());
calculateRGB();
}
private OnSeekBarChangeListener rgbOnSeekBarListener() {
return new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// nothing to do =)
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// nothing to do =)
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
calculateRGB();
}
};
}
/**
* Returns the save button of the dialog.
*
* @return the save {@link Button}.
*/
public Button getSaveButton() {
return saveButton;
}
/**
* Calculates the current set RGB value according to the three
* {@link SeekBar}s. This also changes the background of the Dialog.
*/
private void calculateRGB() {
int r = rSeekBar.getProgress();
int g = gSeekBar.getProgress();
int b = bSeekBar.getProgress();
((TextView) findViewById(R.id.rgbText)).setText("(" + r + "," + g + ","
+ b + ")");
choosenRGB = Color.rgb(r, g, b);
getWindow().setBackgroundDrawable(new ColorDrawable(choosenRGB));
}
/**
* Returns the Color which was chosen in the Dialog.
*
* @return the chosen RGB-colour.
*/
public int getChoosenRGB() {
return choosenRGB;
}
}
//use
final CustomColourDialog customColourDialog = new CustomColourDialog(
MainActivity.this);
customColourDialog.show();
customColourDialog.getSaveButton()
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
squareFragment.squareProgressBar
.setColorRGB(customColourDialog
.getChoosenRGB());
customColourDialog.dismiss();
}
});
package net.yscs.android.square_progressbar;
import java.text.DecimalFormat;
import net.yscs.android.square_progressbar.utils.CalculationUtil;
import net.yscs.android.square_progressbar.utils.PercentStyle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
public class SquareProgressView extends View {
private double progress;
private final Paint progressBarPaint;
private final Paint outlinePaint;
private final Paint textPaint;
private float widthInDp = 10;
private float strokewidth = 0;
private Canvas canvas;
private boolean outline = false;
private boolean startline = false;
private boolean showProgress = false;
private PercentStyle percentSettings = new PercentStyle(Align.CENTER, 150,
true);
private boolean clearOnHundred = false;
public SquareProgressView(Context context) {
super(context);
progressBarPaint = new Paint();
progressBarPaint.setColor(context.getResources().getColor(
android.R.color.holo_green_dark));
progressBarPaint.setStrokeWidth(CalculationUtil.convertDpToPx(
widthInDp, getContext()));
progressBarPaint.setAntiAlias(true);
progressBarPaint.setStyle(Style.STROKE);
outlinePaint = new Paint();
outlinePaint.setColor(context.getResources().getColor(
android.R.color.black));
outlinePaint.setStrokeWidth(1);
outlinePaint.setAntiAlias(true);
outlinePaint.setStyle(Style.STROKE);
textPaint = new Paint();
textPaint.setColor(context.getResources().getColor(
android.R.color.black));
textPaint.setAntiAlias(true);
textPaint.setStyle(Style.STROKE);
}
public SquareProgressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
progressBarPaint = new Paint();
progressBarPaint.setColor(context.getResources().getColor(
android.R.color.holo_green_dark));
progressBarPaint.setStrokeWidth(CalculationUtil.convertDpToPx(
widthInDp, getContext()));
progressBarPaint.setAntiAlias(true);
progressBarPaint.setStyle(Style.STROKE);
outlinePaint = new Paint();
outlinePaint.setColor(context.getResources().getColor(
android.R.color.black));
outlinePaint.setStrokeWidth(1);
outlinePaint.setAntiAlias(true);
outlinePaint.setStyle(Style.STROKE);
textPaint = new Paint();
textPaint.setColor(context.getResources().getColor(
android.R.color.black));
textPaint.setAntiAlias(true);
textPaint.setStyle(Style.STROKE);
}
public SquareProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
progressBarPaint = new Paint();
progressBarPaint.setColor(context.getResources().getColor(
android.R.color.holo_green_dark));
progressBarPaint.setStrokeWidth(CalculationUtil.convertDpToPx(
widthInDp, getContext()));
progressBarPaint.setAntiAlias(true);
progressBarPaint.setStyle(Style.STROKE);
outlinePaint = new Paint();
outlinePaint.setColor(context.getResources().getColor(
android.R.color.black));
outlinePaint.setStrokeWidth(1);
outlinePaint.setAntiAlias(true);
outlinePaint.setStyle(Style.STROKE);
textPaint = new Paint();
textPaint.setColor(context.getResources().getColor(
android.R.color.black));
textPaint.setAntiAlias(true);
textPaint.setStyle(Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
super.onDraw(canvas);
strokewidth = CalculationUtil.convertDpToPx(widthInDp, getContext());
float scope = canvas.getWidth() + canvas.getHeight()
+ canvas.getHeight() + canvas.getWidth();
float percent = (scope / 100) * Float.valueOf(String.valueOf(progress));
float halfOfTheImage = canvas.getWidth() / 2;
if (outline) {
drawOutline();
}
if (isStartline()) {
drawStartline();
}
if (showProgress) {
drawPercent(percentSettings);
}
if (clearOnHundred && progress == 100.0) {
return;
}
Path path = new Path();
if (percent > halfOfTheImage) {
paintFirstHalfOfTheTop(canvas);
float second = percent - halfOfTheImage;
if (second > canvas.getHeight()) {
paintRightSide(canvas);
float third = second - canvas.getHeight();
if (third > canvas.getWidth()) {
paintBottomSide(canvas);
float forth = third - canvas.getWidth();
if (forth > canvas.getHeight()) {
paintLeftSide(canvas);
float fifth = forth - canvas.getHeight();
if (fifth == halfOfTheImage) {
paintSecondHalfOfTheTop(canvas);
} else {
path.moveTo(strokewidth, (strokewidth / 2));
path.lineTo(strokewidth + fifth, (strokewidth / 2));
canvas.drawPath(path, progressBarPaint);
}
} else {
path.moveTo((strokewidth / 2), canvas.getHeight()
- strokewidth);
path.lineTo((strokewidth / 2), canvas.getHeight()
- forth);
canvas.drawPath(path, progressBarPaint);
}
} else {
path.moveTo(canvas.getWidth() - strokewidth,
canvas.getHeight() - (strokewidth / 2));
path.lineTo(canvas.getWidth() - third, canvas.getHeight()
- (strokewidth / 2));
canvas.drawPath(path, progressBarPaint);
}
} else {
path.moveTo(canvas.getWidth() - (strokewidth / 2), strokewidth);
path.lineTo(canvas.getWidth() - (strokewidth / 2), strokewidth
+ second);
canvas.drawPath(path, progressBarPaint);
}
} else {
path.moveTo(halfOfTheImage, strokewidth / 2);
path.lineTo(halfOfTheImage + percent, strokewidth / 2);
canvas.drawPath(path, progressBarPaint);
}
}
private void drawStartline() {
Path outlinePath = new Path();
outlinePath.moveTo(canvas.getWidth() / 2, 0);
outlinePath.lineTo(canvas.getWidth() / 2, strokewidth);
canvas.drawPath(outlinePath, outlinePaint);
}
private void drawOutline() {
Path outlinePath = new Path();
outlinePath.moveTo(0, 0);
outlinePath.lineTo(canvas.getWidth(), 0);
outlinePath.lineTo(canvas.getWidth(), canvas.getHeight());
outlinePath.lineTo(0, canvas.getHeight());
outlinePath.lineTo(0, 0);
canvas.drawPath(outlinePath, outlinePaint);
}
public void paintFirstHalfOfTheTop(Canvas canvas) {
Path path = new Path();
path.moveTo(canvas.getWidth() / 2, strokewidth / 2);
path.lineTo(canvas.getWidth() + strokewidth, strokewidth / 2);
canvas.drawPath(path, progressBarPaint);
}
public void paintRightSide(Canvas canvas) {
Path path = new Path();
path.moveTo(canvas.getWidth() - (strokewidth / 2), strokewidth);
path.lineTo(canvas.getWidth() - (strokewidth / 2), canvas.getHeight());
canvas.drawPath(path, progressBarPaint);
}
public void paintBottomSide(Canvas canvas) {
Path path = new Path();
path.moveTo(canvas.getWidth() - strokewidth, canvas.getHeight()
- (strokewidth / 2));
path.lineTo(0, canvas.getHeight() - (strokewidth / 2));
canvas.drawPath(path, progressBarPaint);
}
public void paintLeftSide(Canvas canvas) {
Path path = new Path();
path.moveTo((strokewidth / 2), canvas.getHeight() - strokewidth);
path.lineTo((strokewidth / 2), 0);
canvas.drawPath(path, progressBarPaint);
}
public void paintSecondHalfOfTheTop(Canvas canvas) {
Path path = new Path();
path.moveTo(strokewidth, (strokewidth / 2));
path.lineTo(canvas.getWidth() / 2, (strokewidth / 2));
canvas.drawPath(path, progressBarPaint);
}
public double getProgress() {
return progress;
}
public void setProgress(double progress) {
this.progress = progress;
this.invalidate();
}
public void setColor(int color) {
progressBarPaint.setColor(color);
this.invalidate();
}
/**
* @return the border
*/
public float getWidthInDp() {
return widthInDp;
}
/**
* @return the border
*/
public void setWidthInDp(int width) {
this.widthInDp = width;
progressBarPaint.setStrokeWidth(CalculationUtil.convertDpToPx(
widthInDp, getContext()));
this.invalidate();
}
public boolean isOutline() {
return outline;
}
public void setOutline(boolean outline) {
this.outline = outline;
this.invalidate();
}
public boolean isStartline() {
return startline;
}
public void setStartline(boolean startline) {
this.startline = startline;
this.invalidate();
}
private void drawPercent(PercentStyle setting) {
textPaint.setTextAlign(setting.getAlign());
if (setting.getTextSize() == 0) {
textPaint.setTextSize((canvas.getHeight() / 10) * 4);
} else {
textPaint.setTextSize(setting.getTextSize());
}
String percentString = new DecimalFormat("###").format(getProgress());
if (setting.isPercentSign()) {
percentString = percentString + percentSettings.getCustomText();
}
textPaint.setColor(percentSettings.getTextColor());
canvas.drawText(
percentString,
canvas.getWidth() / 2,
(int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint
.ascent()) / 2)), textPaint);
}
public boolean isShowProgress() {
return showProgress;
}
public void setShowProgress(boolean showProgress) {
this.showProgress = showProgress;
this.invalidate();
}
public void setPercentStyle(PercentStyle percentSettings) {
this.percentSettings = percentSettings;
this.invalidate();
}
public PercentStyle getPercentStyle() {
return percentSettings;
}
public void setClearOnHundred(boolean clearOnHundred) {
this.clearOnHundred = clearOnHundred;
this.invalidate();
}
public boolean isClearOnHundred() {
return clearOnHundred;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment