Last active
August 29, 2015 14:04
-
-
Save fiskurgit/23f0070ba0c7685caa14 to your computer and use it in GitHub Desktop.
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
package com.degree53.percentbars; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Paint.Style; | |
import android.graphics.RectF; | |
import android.os.Bundle; | |
import android.os.Parcelable; | |
import android.widget.ImageView; | |
/* | |
* Usage: | |
* | |
* PercentBar percentBar = new PercentBar(-100, 100, getActivity()); | |
* percentBar.setSelectedMinValue(-40); | |
* percentBar.setSelectedMaxValue(80); | |
* ((ViewGroup) rootView).addView(percentBar); | |
* | |
*/ | |
public class PercentBar extends ImageView { | |
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private final float mHeight = 30; | |
private final float mPadding = 10; | |
private final double mMinValue; | |
private final double mMaxValue; | |
private double mMinValueNormalised = 0d; | |
private double mMaxValueNormalised = 1d; | |
public int mBackgroundColour = Color.argb(0xFF, 0xcc, 0xcc, 0xcc); | |
public int mBarColour = Color.argb(0xFF, 0xff, 0x00, 0xcc); | |
public int mDividerColour = Color.argb(0xFF, 0xcc, 0xcc, 0xcc); | |
private boolean mDrawCentralDivider = true; | |
public PercentBar(int absoluteMinValue, int absoluteMaxValue, Context context) throws IllegalArgumentException { | |
super(context); | |
mMinValue = (double)absoluteMinValue; | |
mMaxValue = (double)absoluteMaxValue; | |
setFocusable(false); | |
} | |
public void setBackgroundColour(int backgroundColour){ | |
mBackgroundColour = backgroundColour; | |
invalidate(); | |
} | |
public void setBarColour(int barColour){ | |
mBarColour = barColour; | |
invalidate(); | |
} | |
public void setDividerColour(int dividerColour){ | |
mDividerColour = dividerColour; | |
invalidate(); | |
} | |
public void setSelectedMinValue(int value) { | |
if (0 == (mMaxValue - mMinValue)) { | |
setNormalizedMinValue(0d); | |
} | |
else { | |
setNormalizedMinValue(valueToNormalized(value)); | |
} | |
} | |
public void setSelectedMaxValue(int value) { | |
if (0 == (mMaxValue - mMinValue)) { | |
setNormalizedMaxValue(1d); | |
} | |
else { | |
setNormalizedMaxValue(valueToNormalized(value)); | |
} | |
} | |
@Override | |
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int width = 200; | |
if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(widthMeasureSpec)) { | |
width = MeasureSpec.getSize(widthMeasureSpec); | |
} | |
int height = (int) mHeight; | |
if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(heightMeasureSpec)) { | |
height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec)); | |
} | |
setMeasuredDimension(width, height); | |
} | |
@Override | |
protected synchronized void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
final RectF rect = new RectF(mPadding, 0.5f * (getHeight() - mHeight), getWidth() - mPadding, 0.5f * (getHeight() + mHeight)); | |
mPaint.setStyle(Style.FILL); | |
mPaint.setColor(mBackgroundColour); | |
mPaint.setAntiAlias(true); | |
canvas.drawRect(rect, mPaint); | |
rect.left = normalizedToScreen(mMinValueNormalised); | |
rect.right = normalizedToScreen(mMaxValueNormalised); | |
mPaint.setColor(mBarColour); | |
canvas.drawRect(rect, mPaint); | |
if(mDrawCentralDivider){ | |
rect.left = getWidth()/2 - mHeight/2; | |
rect.right = getWidth()/2 + mHeight/2; | |
mPaint.setColor(mDividerColour); | |
canvas.drawRect(rect, mPaint); | |
} | |
} | |
@Override | |
protected Parcelable onSaveInstanceState() { | |
final Bundle bundle = new Bundle(); | |
bundle.putParcelable("SUPER", super.onSaveInstanceState()); | |
bundle.putDouble("MIN", mMinValueNormalised); | |
bundle.putDouble("MAX", mMaxValueNormalised); | |
return bundle; | |
} | |
@Override | |
protected void onRestoreInstanceState(Parcelable parcel) { | |
final Bundle bundle = (Bundle) parcel; | |
super.onRestoreInstanceState(bundle.getParcelable("SUPER")); | |
mMinValueNormalised = bundle.getDouble("MIN"); | |
mMaxValueNormalised = bundle.getDouble("MAX"); | |
} | |
public void setNormalizedMinValue(double value) { | |
mMinValueNormalised = Math.max(0d, Math.min(1d, Math.min(value, mMaxValueNormalised))); | |
invalidate(); | |
} | |
public void setNormalizedMaxValue(double value) { | |
mMaxValueNormalised = Math.max(0d, Math.min(1d, Math.max(value, mMinValueNormalised))); | |
invalidate(); | |
} | |
private double valueToNormalized(int value) { | |
if (0 == mMaxValue - mMinValue) { | |
return 0d; | |
} | |
return ((double)value - mMinValue) / (mMaxValue - mMinValue); | |
} | |
private float normalizedToScreen(double normalizedCoord) { | |
return (float) (mPadding + normalizedCoord * (getWidth() - 2 * mPadding)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment