Advertisement
Kosheen

Circles to Rects

Feb 20th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package com.example.student.test;
  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.Rect;
  8. import android.support.annotation.Nullable;
  9. import android.util.AttributeSet;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.Random;
  15.  
  16.  
  17. public class Circles extends View {
  18.  
  19. class Circle {
  20. public Circle(float x, float y, float r, int number, int color) {
  21. this.x = x;
  22. this.y = y;
  23. this.r = r;
  24. this.number = number;
  25. this.color = color;
  26. }
  27.  
  28. float x, y, r;
  29. int number, color;
  30. public void Draw (Canvas canvas) {
  31. Paint p = new Paint();
  32. p.setColor(color);
  33. canvas.drawCircle(x,y,r,p);
  34. // add text here
  35.  
  36. }
  37. }
  38.  
  39. float x = 0, y =0; int n = 1;
  40. ArrayList<Circle> list = new ArrayList<>();
  41. ArrayList<Rect> cells = new ArrayList<>();
  42. Circle dragCircle = null;
  43.  
  44. public Circles(Context context, @Nullable AttributeSet attrs) {
  45. super(context, attrs);
  46. cells.add(new Rect(10,10, 100, 100));
  47. Random r = new Random();
  48. list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.BLUE));
  49. list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.RED));
  50. list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.YELLOW));
  51. list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.DKGRAY));
  52. list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.GREEN));
  53. // 1) добавить несколько прямоугольников и кружков со
  54. // случайными параметрами
  55. }
  56.  
  57. @Override
  58. protected void onDraw(Canvas canvas) {
  59. super.onDraw(canvas);
  60. Paint p = new Paint();
  61. p.setColor(Color.RED);
  62. p.setColor(Color.rgb(80,30,20));
  63. for (Circle c: list) {
  64. c.Draw(canvas);
  65. }
  66. // 2) отобразить все фигуры
  67. }
  68.  
  69. @Override
  70. public boolean onTouchEvent(MotionEvent event) {
  71. x = event.getX(); y = event.getY();
  72.  
  73. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  74. // проверить, в каком кружке точка касания
  75. for (Circle c: list) {
  76. //
  77. if (Math.hypot(c.x - x, c.y - y) < c.r) {
  78. dragCircle = c;
  79. }
  80. }
  81. }
  82.  
  83. if (dragCircle != null) {
  84. // если мы тажим круг
  85. dragCircle.x = x; dragCircle.y = y;
  86. invalidate();
  87. }
  88.  
  89. if (event.getAction() == MotionEvent.ACTION_UP) {
  90. // 3) проверить, переместился ли круг в нужную точку
  91. // (его координаты внутри какого-нибудь прямоугольника)
  92. // если так, удалить кружок из коллекции
  93. dragCircle = null;
  94. }
  95.  
  96. /*if (event.getAction() == MotionEvent.ACTION_DOWN) {
  97. Circle c = new Circle(event.getX(), event.getY(), 30, n, Color.BLUE);
  98. list.add(c);
  99. n ++;
  100. invalidate();
  101. }
  102. */
  103. return true;
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement