При касании экрана создавать мыльные пузыри разного цвета Android Studio. не показываются пузырьки

Рейтинг: 0Ответов: 0Опубликовано: 09.01.2023
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.Random;
public class BubbleView extends View {
    private static final int BUBBLE_RADIUS_MIN = 20;
    private static final int BUBBLE_RADIUS_MAX = 50;
    private static final int BUBBLE_SPEED_MIN = 5;
    private static final int BUBBLE_SPEED_MAX = 20;
    private ArrayList<Bubble> bubbles;
    private Random random;
    private Paint paint;
    private ValueAnimator animator;
    public BubbleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        random = new Random();
        paint = new Paint();
        bubbles = new ArrayList<>();
        animator = ValueAnimator.ofInt(0, 1).setDuration(16);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
       
                for (Bubble bubble : bubbles) {
                    bubble.floatUp();
                }
         
                invalidate();
            }
        });
    }
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        animator.start();
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        animator.cancel();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < bubbles.size(); i++) {
            Bubble bubble = bubbles.get(i);
            bubble.draw(canvas, paint);
        
            if (bubble.getY() + bubble.getRadius() < 0) {
                bubbles.remove(i);
                i--;
            }
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
  
            int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
    
            int radius = random.nextInt(BUBBLE_RADIUS_MAX - BUBBLE_RADIUS_MIN) + BUBBLE_RADIUS_MIN;
     
            int speedX = random.nextInt(BUBBLE_SPEED_MAX - BUBBLE_SPEED_MIN) + BUBBLE_SPEED_MIN;
            int speedY = random.nextInt(BUBBLE_SPEED_MAX - BUBBLE_SPEED_MIN) + BUBBLE_SPEED_MIN;
     
            Bubble bubble = new Bubble(event.getX(), event.getY(), radius, color, speedX, speedY);
            bubbles.add(bubble);
            invalidate();
        }
        return true;
    }
    class Bubble {
        private float x;
        private float y;
        private int radius;
        private int color;
        private float speedX;
        private float speedY;
        public Bubble(float x, float y, int radius, int color, float speedX, float speedY) {
            this.x = x;
            this.y = y;
            this.radius = radius;
            this.color = color;
            this.speedX = speedX;
            this.speedY = speedY;
        }
        public void draw(Canvas canvas, Paint paint) {
            paint.setColor(color);
            canvas.drawCircle(x, y, radius, paint);
        }
        public void floatUp() {
            y -= speedY;
        }
        public float getY() {
            return y;
        }
        public int getRadius() {
            return radius;
        }
    };
}```

Ответы

Ответов пока нет.