Skip to content

Instantly share code, notes, and snippets.

@scottyab
Created March 8, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottyab/526ca01956af67f83c14 to your computer and use it in GitHub Desktop.
Save scottyab/526ca01956af67f83c14 to your computer and use it in GitHub Desktop.
package com.enquos.nutrition.dashboard;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
/**
* Created on 08/03/16.
*/
public class WeeklyDayCalorieBarView extends View {
private Paint calorieBarPaint;
private Paint overflowPaint;
private int calorieBarColor = Color.RED;
private int overflowColor = Color.BLUE;
private float calories;
private float overflow;
public static final float CALORIE_TO_PIXEL_RATIO = 0.1f;
private float scaleFactor;
/**
* Direction of the bar
*/
public enum DIRECTION{
UP,DOWN
}
private DIRECTION direction = DIRECTION.UP;
public WeeklyDayCalorieBarView(Context context) {
super(context);
initialise();
}
public WeeklyDayCalorieBarView(Context context, AttributeSet attrs) {
super(context, attrs);
initialise();
}
public WeeklyDayCalorieBarView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialise();
}
public WeeklyDayCalorieBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialise();
}
private void initialise() {
calorieBarPaint = new Paint();
overflowPaint = new Paint();
calorieBarPaint.setColor(calorieBarColor);
overflowPaint.setColor(overflowColor);
DisplayMetrics metrics = getResources().getDisplayMetrics();
scaleFactor = metrics.density;
}
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
int fullWidth = getWidth();
int fullHeight = getHeight();
//move to setter?
float calorieBarHeight= CALORIE_TO_PIXEL_RATIO * calories;
float overflowBarHeight = CALORIE_TO_PIXEL_RATIO * overflow;
//direction determins where to start drawing top up or bottom
//assume
//draw first block
if(calorieBarHeight!=0f) {
canvas.drawRect(0,calorieBarHeight,fullWidth,fullHeight , calorieBarPaint);
}
//draw 2nd block on top
if(overflowBarHeight!=0f) {
canvas.drawRect(0,calorieBarHeight+overflowBarHeight,fullWidth, calorieBarHeight, overflowPaint);
}
}
public DIRECTION getDirection() {
return direction;
}
public void setDirection(DIRECTION direction) {
this.direction = direction;
}
public float getCalories() {
return calories;
}
public float getOverflow() {
return overflow;
}
public void setCaloriesAndOverflow(float calories, float overflow) {
this.calories = calories;
this.overflow = overflow;
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment