Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. package com.example.studente.esercizio3;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.PointF;
  8. import android.graphics.RectF;
  9. import android.support.annotation.Nullable;
  10. import android.util.AttributeSet;
  11. import android.view.View;
  12.  
  13. import java.util.List;
  14.  
  15. public class PieChartView extends View {
  16.  
  17. private PointF center = new PointF();
  18. private RectF enclosing = new RectF();
  19. private List<Integer> segmentColor;
  20. private List<Float> percent;
  21. private int radius= 100;
  22. private int backgroundColor = Color.WHITE;
  23. private int strokeColor;
  24.  
  25.  
  26. public PieChartView(Context context) {
  27. super(context);
  28. }
  29.  
  30. public PieChartView(Context context, @Nullable AttributeSet attrs) {
  31. super(context, attrs);
  32. }
  33.  
  34. public PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  35. super(context, attrs, defStyleAttr);
  36. }
  37.  
  38. @Override
  39. protected void onDraw(Canvas canvas){
  40. Paint paint = new Paint();
  41. paint.setColor(backgroundColor);
  42. canvas.drawRect(0,0,canvas.getHeight(),canvas.getWidth(),paint);
  43.  
  44. paint.setFlags(Paint.ANTI_ALIAS_FLAG);
  45.  
  46. center.x = canvas.getWidth()/2;
  47. center.y = canvas.getHeight()/2;
  48. enclosing.top = center.y - radius;
  49. enclosing.bottom = center.y + radius;
  50. enclosing.left = center.x - radius;
  51. enclosing.right = center.x + radius;
  52.  
  53. float alpha=-90.0f;
  54. float p2a=360.0f/100.0f;
  55.  
  56. float p;
  57. Integer c;
  58.  
  59. for(int i = 0; i < percent.size();i++){
  60. p = percent.get(i); // % da rappresentare
  61. c = segmentColor.get(i); // colore da usare
  62. paint.setColor(c);
  63. paint.setStyle(Paint.Style.FILL)
  64.  
  65. canvas.drawArc(enclosing,alpha,p*p2a,true,paint);
  66. alpha += p*p2a;
  67. }
  68.  
  69. alpha=-90.0f;
  70. for(int i = 0; i < percent.size();i++)
  71. {
  72. p = percent.get(i);
  73.  
  74. paint.setColor(strokeColor);
  75. paint.setStyle(paint.Style.STROKE);
  76.  
  77. canvas.drawArc(enclosing,alpha,p*p2a,true,paint);
  78. alpha += p*p2a;
  79. }
  80. }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement