• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

BubbleDraws problems

Pedro Sosa

Lurker
Jun 15, 2019
1
0
Hello, I tried a tutorial: Learn Java Like a Kid Build Fun Desktop and Mobile Apps
And I have problems with Android Studio.
Indeed what I have done is only create a class BubbleView:
But I don't know what I have to do with the xml files...
thanks

Code:
import...

public class  BubbleView extends ImageView implements View.OnTouchListener {
    private ArrayList<Bubble> bubbleList;
    private final int DELAY = 16;
    private Paint myPaint = new Paint();
    private Handler h;

    public BubbleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        bubbleList = new ArrayList<Bubble>();
        myPaint.setColor(BLACK);
        h = new Handler();
        this.setOnTouchListener(this);
    }

    private class Bubble {
        public int x;
        public int y;
        public int size;
        public int color;
        public int xspeed;
        public int yspeed;
        private final int MAX_SPEED = 5;

        public Bubble(int newX, int newY, int newSize) {
            x = newX;
            y = newY;
            size = newSize;
            color = Color.argb((int) (Math.random() * 256),
                    (int) (Math.random() * 256),
                    (int) (Math.random() * 256),
                    (int) (Math.random() * 256));
            xspeed = (int) (Math.random() * MAX_SPEED * 2 - MAX_SPEED);
            yspeed = (int) (Math.random() * MAX_SPEED * 2 - MAX_SPEED);
            if (xspeed == 0 && yspeed == 0) {
                xspeed = 1;
                yspeed = 1;
            }
        }

        public void update() {
            x += xspeed;
            y += yspeed;
            if (x < 0 || x > getWidth())
                xspeed = -1 * xspeed;
            if (y < 0 || y > getHeight())
                yspeed = -yspeed;
        }
    }

    protected void onDraw(Canvas c) {
        for (int x = 0; x < 100; x++)
            for (Bubble bubble : bubbleList) {
                myPaint.setColor(bubble.color);
                c.drawOval(bubble.x - bubble.size / 2, bubble.y - bubble.size / 2,
                        bubble.x + bubble.size / 2, bubble.y + bubble.size / 2, myPaint);

            }
        h.postDelayed(r, DELAY);
    }

    private Runnable r = new Runnable() {
        public void run() {
            //update burbujas
            for (Bubble bubble : bubbleList)
                bubble.update();
            //dibuja
            invalidate();
        }
    };

    public boolean onTouch(View v, MotionEvent event) {
        bubbleList.add(new Bubble((int) event.getX(),
                (int) event.getY(),
                (int) (Math.random() * 50 + 50)));
        return true;
    }
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones