Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.View;
- public class Test1 extends Activity {
- /** Called when the activity is first created. */
- private Handler mHandler;
- private DrawView draw;
- private boolean running;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- draw = new DrawView(this, null);
- mHandler = new Handler(new Handler.Callback() {
- @Override
- public boolean handleMessage(Message msg) {
- Log.v("tag", "execute");
- draw.addRandomPoint();
- if (running)
- mHandler.sendMessageDelayed(new Message(), 500);
- return true;
- }
- });
- mHandler.sendMessage(new Message());
- setContentView(draw);
- }
- @Override
- public void onResume() {
- running = true;
- }
- @Override
- public void onPause() {
- running = false;
- }
- public class DrawView extends View {
- public static final String TAG = "DrawView";
- List<Point> points = new ArrayList<Point>();
- Paint paint = new Paint();
- public DrawView(Context context, AttributeSet attrs) {
- super(context, attrs);
- paint.setStyle(Paint.Style.FILL_AND_STROKE);
- paint.setColor(Color.WHITE);
- paint.setAntiAlias(true);
- }
- @Override
- public void onDraw(Canvas canvas) {
- canvas.drawColor(Color.WHITE);
- for (Point point : points) {
- paint.setColor(point.color);
- canvas.drawCircle(point.x, point.y, point.radius, paint);
- }
- }
- Random random = new Random();
- private final int[] RAINBOW = {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN};
- public void addRandomPoint() {
- points.add(new Point(
- random.nextFloat() * getWidth(),
- random.nextFloat() * getHeight(),
- random.nextFloat() * 30,
- RAINBOW[random.nextInt(RAINBOW.length)]));
- postInvalidate();
- }
- }
- class Point {
- Point(float x, float y, float radius, int color) {
- this.x = x;
- this.y = y;
- this.radius = radius;
- this.color = color;
- }
- float x, y, radius;
- int color;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement