Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.student.test;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.support.annotation.Nullable;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.View;
- import java.util.ArrayList;
- import java.util.Random;
- public class Circles extends View {
- class Circle {
- public Circle(float x, float y, float r, int number, int color) {
- this.x = x;
- this.y = y;
- this.r = r;
- this.number = number;
- this.color = color;
- }
- float x, y, r;
- int number, color;
- public void Draw (Canvas canvas) {
- Paint p = new Paint();
- p.setColor(color);
- canvas.drawCircle(x,y,r,p);
- // add text here
- }
- }
- float x = 0, y =0; int n = 1;
- ArrayList<Circle> list = new ArrayList<>();
- ArrayList<Rect> cells = new ArrayList<>();
- Circle dragCircle = null;
- public Circles(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- cells.add(new Rect(10,10, 100, 100));
- Random r = new Random();
- list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.BLUE));
- list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.RED));
- list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.YELLOW));
- list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.DKGRAY));
- list.add(new Circle(r.nextInt(400), r.nextInt(400), 50,1, Color.GREEN));
- // 1) добавить несколько прямоугольников и кружков со
- // случайными параметрами
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Paint p = new Paint();
- p.setColor(Color.RED);
- p.setColor(Color.rgb(80,30,20));
- for (Circle c: list) {
- c.Draw(canvas);
- }
- // 2) отобразить все фигуры
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- x = event.getX(); y = event.getY();
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- // проверить, в каком кружке точка касания
- for (Circle c: list) {
- //
- if (Math.hypot(c.x - x, c.y - y) < c.r) {
- dragCircle = c;
- }
- }
- }
- if (dragCircle != null) {
- // если мы тажим круг
- dragCircle.x = x; dragCircle.y = y;
- invalidate();
- }
- if (event.getAction() == MotionEvent.ACTION_UP) {
- // 3) проверить, переместился ли круг в нужную точку
- // (его координаты внутри какого-нибудь прямоугольника)
- // если так, удалить кружок из коллекции
- dragCircle = null;
- }
- /*if (event.getAction() == MotionEvent.ACTION_DOWN) {
- Circle c = new Circle(event.getX(), event.getY(), 30, n, Color.BLUE);
- list.add(c);
- n ++;
- invalidate();
- }
- */
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement