Skip to content

Instantly share code, notes, and snippets.

@eddieberklee
Created December 22, 2015 23:54
Show Gist options
  • Select an option

  • Save eddieberklee/c4b0b80786b7caff43b5 to your computer and use it in GitHub Desktop.

Select an option

Save eddieberklee/c4b0b80786b7caff43b5 to your computer and use it in GitHub Desktop.
Android Colored Half Hour Arcs around a Circle
int[] colors = new int[]{
getResources().getColor(R.color.flatui_red_1),
getResources().getColor(R.color.flatui_red_2),
getResources().getColor(R.color.flatui_orange_1),
getResources().getColor(R.color.flatui_orange_2),
getResources().getColor(R.color.flatui_yellow_1),
getResources().getColor(R.color.flatui_yellow_2),
getResources().getColor(R.color.flatui_green_1),
getResources().getColor(R.color.flatui_green_2),
getResources().getColor(R.color.flatui_blue_1),
getResources().getColor(R.color.flatui_blue_2),
getResources().getColor(R.color.flatui_purple_1),
getResources().getColor(R.color.flatui_purple_2),
getResources().getColor(R.color.flatui_red_1),
getResources().getColor(R.color.flatui_red_2),
getResources().getColor(R.color.flatui_orange_1),
getResources().getColor(R.color.flatui_orange_2),
getResources().getColor(R.color.flatui_yellow_1),
getResources().getColor(R.color.flatui_yellow_2),
getResources().getColor(R.color.flatui_green_1),
getResources().getColor(R.color.flatui_green_2),
getResources().getColor(R.color.flatui_blue_1),
getResources().getColor(R.color.flatui_blue_2),
getResources().getColor(R.color.flatui_purple_1),
getResources().getColor(R.color.flatui_purple_2),
};
int NUM_HALF_HOURS = 12 * 2;
Path[] mHourPaths;
mHourPaths = new Path[NUM_HALF_HOURS];
Path hourPath = new Path();
hourPath.setFillType(Path.FillType.EVEN_ODD);
RectF fullCircleRect = new RectF(0, 0, getWidth(), getHeight());
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
for (int halfHour = 0; halfHour < NUM_HALF_HOURS; halfHour++) {
// circle drawing starts at 3 oclock so need to adjust
float halfHourAngle = 360f / NUM_HALF_HOURS;
float quarterHourAngle = halfHourAngle / 2;
float hourAngle = halfHourAngle * halfHour - quarterHourAngle;
hourAngle = (hourAngle + 360f * 3 / 4) % 360; // make up for mathematical vs clock degrees
hourPath.addArc(fullCircleRect, hourAngle, halfHourAngle); // hourSweepAngle covers 2 of these quarter hour sections = 1 half hour section
hourPath.lineTo(centerX, centerY);
hourPath.close();
highlightPaint.setColor(colors[halfHour]);
canvas.drawPath(hourPath, highlightPaint);
mHourPaths[halfHour] = new Path(hourPath);
hourPath.reset();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment