PieGraph.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Created by Daniel Nadeau
  3. * daniel.nadeau01@gmail.com
  4. * danielnadeau.blogspot.com
  5. *
  6. * Licensed to the Apache Software Foundation (ASF) under one
  7. or more contributor license agreements. See the NOTICE file
  8. distributed with this work for additional information
  9. regarding copyright ownership. The ASF licenses this file
  10. to you under the Apache License, Version 2.0 (the
  11. "License"); you may not use this file except in compliance
  12. with the License. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing,
  15. software distributed under the License is distributed on an
  16. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. KIND, either express or implied. See the License for the
  18. specific language governing permissions and limitations
  19. under the License.
  20. */
  21. package com.echo.holographlibrary;
  22. import android.content.Context;
  23. import android.graphics.Canvas;
  24. import android.graphics.Color;
  25. import android.graphics.Paint;
  26. import android.graphics.Path;
  27. import android.graphics.Path.Direction;
  28. import android.graphics.Point;
  29. import android.graphics.Rect;
  30. import android.graphics.RectF;
  31. import android.graphics.Region;
  32. import android.util.AttributeSet;
  33. import android.view.MotionEvent;
  34. import android.view.View;
  35. import java.util.ArrayList;
  36. import java.util.Iterator;
  37. import de.tudarmstadt.informatik.hostage.R;
  38. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  39. public class PieGraph extends View {
  40. private final static int TITLE_FONT_SIZE = 14;
  41. private final static int SUBTITLE_FONT_SIZE = 30;
  42. static final String ALL_TITLE = MainActivity.getContext().getString(R.string.pie_all);
  43. private ArrayList<PieSlice> slices = new ArrayList<PieSlice>();
  44. private Paint paint = new Paint();
  45. private Path path = new Path();
  46. private int indexSelected = -1;
  47. private int thickness;
  48. private OnSliceClickedListener listener;
  49. private boolean drawCompleted = false;
  50. private String title;
  51. private String subtitle;
  52. private Context mContext;
  53. /**
  54. * Constructor
  55. * @param context Context
  56. */
  57. public PieGraph(Context context) {
  58. super(context);
  59. this.mContext = context;
  60. thickness = (int) (25f * context.getResources().getDisplayMetrics().density);
  61. this.setWillNotDraw(false);
  62. }
  63. /**
  64. * Constructor
  65. * @param context Context
  66. * @param attrs AttributeSet
  67. */
  68. public PieGraph(Context context, AttributeSet attrs) {
  69. super(context, attrs);
  70. this.mContext = context;
  71. thickness = (int) (25f * context.getResources().getDisplayMetrics().density);
  72. this.setWillNotDraw(false);
  73. }
  74. @Override
  75. public void onDraw(Canvas canvas) {
  76. super.onDraw(canvas);
  77. canvas.drawColor(Color.TRANSPARENT);
  78. paint.reset();
  79. paint.setAntiAlias(true);
  80. float midX, midY, radius, innerRadius;
  81. path.reset();
  82. float currentAngle = 270;
  83. float currentSweep = 0;
  84. int totalValue = 0;
  85. float padding = 2;
  86. midX = getWidth()/2;
  87. midY = getHeight()/2;
  88. if (midX < midY){
  89. radius = midX;
  90. } else {
  91. radius = midY;
  92. }
  93. radius -= padding;
  94. innerRadius = radius - thickness;
  95. for (PieSlice slice : slices){
  96. totalValue += slice.getValue();
  97. }
  98. int count = 0;
  99. for (PieSlice slice : slices){
  100. Path p = new Path();
  101. paint.setColor(slice.getColor());
  102. currentSweep = (slice.getValue()/totalValue)*(360);
  103. p.arcTo(new RectF(midX-radius, midY-radius, midX+radius, midY+radius), currentAngle+padding, currentSweep - padding);
  104. p.arcTo(new RectF(midX-innerRadius, midY-innerRadius, midX+innerRadius, midY+innerRadius), (currentAngle+padding) + (currentSweep - padding), -(currentSweep-padding));
  105. p.close();
  106. slice.setPath(p);
  107. slice.setRegion(new Region((int)(midX-radius), (int)(midY-radius), (int)(midX+radius), (int)(midY+radius)));
  108. canvas.drawPath(p, paint);
  109. if (indexSelected == count && listener != null){
  110. path.reset();
  111. //paint.setColor(slice.getColor());
  112. paint.setColor(Color.parseColor("#33B5E5"));
  113. paint.setAlpha(100);
  114. if (slices.size() > 1) {
  115. path.arcTo(new RectF(midX-radius-(padding*2), midY-radius-(padding*2), midX+radius+(padding*2), midY+radius+(padding*2)), currentAngle, currentSweep+padding);
  116. path.arcTo(new RectF(midX-innerRadius+(padding*2), midY-innerRadius+(padding*2), midX+innerRadius-(padding*2), midY+innerRadius-(padding*2)), currentAngle + currentSweep + padding, -(currentSweep + padding));
  117. path.close();
  118. } else {
  119. path.addCircle(midX, midY, radius+padding, Direction.CW);
  120. }
  121. canvas.drawPath(path, paint);
  122. paint.setAlpha(255);
  123. }
  124. currentAngle = currentAngle+currentSweep;
  125. count++;
  126. }
  127. this.drawTitle(canvas);
  128. this.drawSubtitle(canvas);
  129. drawCompleted = true;
  130. }
  131. /**
  132. * Draws the title in the middle of the pie.
  133. * @param canvas Canvas
  134. */
  135. private void drawTitle(Canvas canvas){
  136. String title = this.title;
  137. if (title != null && title.length() != 0){
  138. this.paint.reset();
  139. paint.setColor(Color.BLACK);
  140. paint.setAlpha(50);
  141. paint.setAntiAlias(true);
  142. paint.setAlpha(255);
  143. this.paint.setTextSize(TITLE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  144. int yCenter = this.getHeight() / 2;
  145. int xCenter = this.getWidth() / 2;
  146. float textwidth = (this.paint.measureText(title));
  147. Rect bounds = new Rect();
  148. this.paint.getTextBounds(title,0,title.length(), bounds);
  149. canvas.drawText(title,xCenter - (textwidth / 2),yCenter - bounds.height(), this.paint);
  150. this.paint.reset();
  151. }
  152. }
  153. /**
  154. * Draws the Subtitle in the middle of the pie.
  155. * @param canvas Canvas
  156. */
  157. private void drawSubtitle(Canvas canvas){
  158. String title = this.subtitle;
  159. if (title != null && title.length() != 0){
  160. this.paint.reset();
  161. paint.setColor(Color.BLACK);
  162. paint.setAlpha(50);
  163. paint.setAntiAlias(true);
  164. paint.setAlpha(255);
  165. this.paint.setTextSize(SUBTITLE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  166. int yCenter = this.getHeight() / 2;
  167. int xCenter = this.getWidth() / 2;
  168. float textwidth = (this.paint.measureText(title));
  169. Rect bounds = new Rect();
  170. this.paint.getTextBounds(title,0,title.length(), bounds);
  171. canvas.drawText(title,xCenter - (textwidth / 2),yCenter + bounds.height(), this.paint);
  172. this.paint.reset();
  173. }
  174. }
  175. @Override
  176. public boolean onTouchEvent(MotionEvent event) {
  177. if (drawCompleted) {
  178. Point point = new Point();
  179. point.x = (int) event.getX();
  180. point.y = (int) event.getY();
  181. int count = 0;
  182. boolean drawedTitle = false;
  183. for (PieSlice slice : slices){
  184. Region r = new Region();
  185. r.setPath(slice.getPath(), slice.getRegion());
  186. if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
  187. indexSelected = count;
  188. this.title = slice.getTitle();
  189. this.subtitle = "" + (long) slice.getValue();
  190. drawedTitle = true;
  191. } else if (event.getAction() == MotionEvent.ACTION_UP){
  192. if (r.contains((int)point.x,(int) point.y) && listener != null){
  193. if (indexSelected > -1){
  194. listener.onClick(indexSelected);
  195. }
  196. indexSelected = -1;
  197. }
  198. }
  199. else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
  200. indexSelected = -1;
  201. }
  202. count++;
  203. }
  204. if ( event.getAction() == MotionEvent.ACTION_DOWN && !drawedTitle ) {
  205. // refresh title & subtitle
  206. this.addSlice(null);
  207. }
  208. if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
  209. postInvalidate();
  210. }
  211. }
  212. return true;
  213. }
  214. public ArrayList<PieSlice> getSlices() {
  215. return slices;
  216. }
  217. public void setSlices(ArrayList<PieSlice> slices) {
  218. this.slices = slices;
  219. //postInvalidate();
  220. }
  221. public PieSlice getSlice(int index) {
  222. return slices.get(index);
  223. }
  224. /**
  225. * Add a pie slice.
  226. * @param slice {@link PieSlice PieSlice}
  227. */
  228. public void addSlice(PieSlice slice) {
  229. if (slice != null)
  230. this.slices.add(slice);
  231. long countedValue = 0;
  232. Iterator<PieSlice> iter = this.slices.iterator();
  233. while (iter.hasNext()){
  234. PieSlice s = iter.next();
  235. countedValue+= s.getValue();
  236. }
  237. this.title = ALL_TITLE;
  238. this.subtitle = "" + countedValue;
  239. //postInvalidate();
  240. }
  241. public int getThickness() {
  242. return thickness;
  243. }
  244. public void setThickness(int thickness) {
  245. this.thickness = thickness;
  246. //postInvalidate();
  247. }
  248. /**
  249. * Remove all slices.
  250. */
  251. public void removeSlices(){
  252. Iterator<PieSlice> iter = slices.iterator();
  253. while (iter.hasNext()) {
  254. iter.next();
  255. iter.remove();
  256. }
  257. this.title = "";
  258. this.subtitle = "";
  259. //postInvalidate();
  260. }
  261. /**
  262. * Set the OnSliceClickedListener, which will be called if the user clicks a slice.
  263. * @param listener
  264. */
  265. public void setOnSliceClickedListener(OnSliceClickedListener listener) {
  266. this.listener = listener;
  267. }
  268. /**
  269. * OnSliceClickedListener will be called if the user clicks a pie slice.
  270. */
  271. public static interface OnSliceClickedListener {
  272. public abstract void onClick(int index);
  273. }
  274. }