PieGraph.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 android.widget.TextView;
  36. import java.util.ArrayList;
  37. import java.util.Iterator;
  38. import de.tudarmstadt.informatik.hostage.R;
  39. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  40. public class PieGraph extends View {
  41. private final static int TITLE_FONT_SIZE = 14;
  42. private final static int SUBTITLE_FONT_SIZE = 30;
  43. static final String ALL_TITLE = MainActivity.getContext().getString(R.string.pie_all);
  44. private ArrayList<PieSlice> slices = new ArrayList<PieSlice>();
  45. private Paint paint = new Paint();
  46. private Path path = new Path();
  47. private int indexSelected = -1;
  48. private int thickness;
  49. private OnSliceClickedListener listener;
  50. private boolean drawCompleted = false;
  51. private TextView titleTextView;
  52. private TextView subtitleTextView;
  53. private String title;
  54. private String subtitle;
  55. private Context mContext;
  56. public PieGraph(Context context) {
  57. super(context);
  58. this.mContext = context;
  59. thickness = (int) (25f * context.getResources().getDisplayMetrics().density);
  60. this.setWillNotDraw(false);
  61. }
  62. public PieGraph(Context context, AttributeSet attrs) {
  63. super(context, attrs);
  64. this.mContext = context;
  65. thickness = (int) (25f * context.getResources().getDisplayMetrics().density);
  66. this.setWillNotDraw(false);
  67. }
  68. public void onDraw(Canvas canvas) {
  69. super.onDraw(canvas);
  70. canvas.drawColor(Color.TRANSPARENT);
  71. paint.reset();
  72. paint.setAntiAlias(true);
  73. float midX, midY, radius, innerRadius;
  74. path.reset();
  75. float currentAngle = 270;
  76. float currentSweep = 0;
  77. int totalValue = 0;
  78. float padding = 2;
  79. midX = getWidth()/2;
  80. midY = getHeight()/2;
  81. if (midX < midY){
  82. radius = midX;
  83. } else {
  84. radius = midY;
  85. }
  86. radius -= padding;
  87. innerRadius = radius - thickness;
  88. for (PieSlice slice : slices){
  89. totalValue += slice.getValue();
  90. }
  91. int count = 0;
  92. for (PieSlice slice : slices){
  93. Path p = new Path();
  94. paint.setColor(slice.getColor());
  95. currentSweep = (slice.getValue()/totalValue)*(360);
  96. p.arcTo(new RectF(midX-radius, midY-radius, midX+radius, midY+radius), currentAngle+padding, currentSweep - padding);
  97. p.arcTo(new RectF(midX-innerRadius, midY-innerRadius, midX+innerRadius, midY+innerRadius), (currentAngle+padding) + (currentSweep - padding), -(currentSweep-padding));
  98. p.close();
  99. slice.setPath(p);
  100. slice.setRegion(new Region((int)(midX-radius), (int)(midY-radius), (int)(midX+radius), (int)(midY+radius)));
  101. canvas.drawPath(p, paint);
  102. if (indexSelected == count && listener != null){
  103. path.reset();
  104. //paint.setColor(slice.getColor());
  105. paint.setColor(Color.parseColor("#33B5E5"));
  106. paint.setAlpha(100);
  107. if (slices.size() > 1) {
  108. path.arcTo(new RectF(midX-radius-(padding*2), midY-radius-(padding*2), midX+radius+(padding*2), midY+radius+(padding*2)), currentAngle, currentSweep+padding);
  109. 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));
  110. path.close();
  111. } else {
  112. path.addCircle(midX, midY, radius+padding, Direction.CW);
  113. }
  114. canvas.drawPath(path, paint);
  115. paint.setAlpha(255);
  116. }
  117. currentAngle = currentAngle+currentSweep;
  118. count++;
  119. }
  120. this.drawTitle(canvas);
  121. this.drawSubtitle(canvas);
  122. drawCompleted = true;
  123. }
  124. private void drawTitle(Canvas canvas){
  125. String title = this.title;
  126. if (title != null && title.length() != 0){
  127. this.paint.reset();
  128. paint.setColor(Color.BLACK);
  129. paint.setAlpha(50);
  130. paint.setAntiAlias(true);
  131. paint.setAlpha(255);
  132. this.paint.setTextSize(TITLE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  133. int yCenter = this.getHeight() / 2;
  134. int xCenter = this.getWidth() / 2;
  135. float textwidth = (this.paint.measureText(title));
  136. Rect bounds = new Rect();
  137. this.paint.getTextBounds(title,0,title.length(), bounds);
  138. canvas.drawText(title,xCenter - (textwidth / 2),yCenter - bounds.height(), this.paint);
  139. this.paint.reset();
  140. }
  141. }
  142. private void drawSubtitle(Canvas canvas){
  143. String title = this.subtitle;
  144. if (title != null && title.length() != 0){
  145. this.paint.reset();
  146. paint.setColor(Color.BLACK);
  147. paint.setAlpha(50);
  148. paint.setAntiAlias(true);
  149. paint.setAlpha(255);
  150. this.paint.setTextSize(SUBTITLE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  151. int yCenter = this.getHeight() / 2;
  152. int xCenter = this.getWidth() / 2;
  153. float textwidth = (this.paint.measureText(title));
  154. Rect bounds = new Rect();
  155. this.paint.getTextBounds(title,0,title.length(), bounds);
  156. canvas.drawText(title,xCenter - (textwidth / 2),yCenter + bounds.height(), this.paint);
  157. this.paint.reset();
  158. }
  159. }
  160. @Override
  161. public boolean onTouchEvent(MotionEvent event) {
  162. if (drawCompleted) {
  163. Point point = new Point();
  164. point.x = (int) event.getX();
  165. point.y = (int) event.getY();
  166. int count = 0;
  167. boolean drawedTitle = false;
  168. for (PieSlice slice : slices){
  169. Region r = new Region();
  170. r.setPath(slice.getPath(), slice.getRegion());
  171. if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
  172. indexSelected = count;
  173. this.title = slice.getTitle();
  174. this.subtitle = "" + (long) slice.getValue();
  175. drawedTitle = true;
  176. } else if (event.getAction() == MotionEvent.ACTION_UP){
  177. if (r.contains((int)point.x,(int) point.y) && listener != null){
  178. if (indexSelected > -1){
  179. listener.onClick(indexSelected);
  180. }
  181. indexSelected = -1;
  182. }
  183. }
  184. else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
  185. indexSelected = -1;
  186. }
  187. count++;
  188. }
  189. if ( event.getAction() == MotionEvent.ACTION_DOWN && !drawedTitle ) {
  190. // refresh title & subtitle
  191. this.addSlice(null);
  192. }
  193. if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
  194. postInvalidate();
  195. }
  196. }
  197. return true;
  198. }
  199. public ArrayList<PieSlice> getSlices() {
  200. return slices;
  201. }
  202. public void setSlices(ArrayList<PieSlice> slices) {
  203. this.slices = slices;
  204. //postInvalidate();
  205. }
  206. public PieSlice getSlice(int index) {
  207. return slices.get(index);
  208. }
  209. public void addSlice(PieSlice slice) {
  210. if (slice != null)
  211. this.slices.add(slice);
  212. long countedValue = 0;
  213. Iterator<PieSlice> iter = this.slices.iterator();
  214. while (iter.hasNext()){
  215. PieSlice s = iter.next();
  216. countedValue+= s.getValue();
  217. }
  218. this.title = ALL_TITLE;
  219. this.subtitle = "" + countedValue;
  220. //postInvalidate();
  221. }
  222. public void setOnSliceClickedListener(OnSliceClickedListener listener) {
  223. this.listener = listener;
  224. }
  225. public int getThickness() {
  226. return thickness;
  227. }
  228. public void setThickness(int thickness) {
  229. this.thickness = thickness;
  230. //postInvalidate();
  231. }
  232. public void removeSlices(){
  233. Iterator<PieSlice> iter = slices.iterator();
  234. while (iter.hasNext()) {
  235. iter.next();
  236. iter.remove();
  237. }
  238. this.title = "";
  239. this.subtitle = "";
  240. //postInvalidate();
  241. }
  242. public static interface OnSliceClickedListener {
  243. public abstract void onClick(int index);
  244. }
  245. }