Advertisement
raffaele181188

sample

Feb 11th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. package com.example;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.graphics.Canvas;
  10. import android.graphics.Color;
  11. import android.graphics.Paint;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.os.Message;
  15. import android.util.AttributeSet;
  16. import android.util.Log;
  17. import android.view.View;
  18.  
  19. public class Test1 extends Activity {
  20.     /** Called when the activity is first created. */
  21.     private Handler mHandler;
  22.     private DrawView draw;
  23.     private boolean running;
  24.    
  25.     @Override
  26.     public void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         draw = new DrawView(this, null);
  29.         mHandler = new Handler(new Handler.Callback() {
  30.             @Override
  31.             public boolean handleMessage(Message msg) {
  32.                 Log.v("tag", "execute");
  33.                 draw.addRandomPoint();
  34.                 if (running)
  35.                     mHandler.sendMessageDelayed(new Message(), 500);
  36.                 return true;
  37.             }
  38.         });
  39.         mHandler.sendMessage(new Message());
  40.         setContentView(draw);
  41.     }
  42.    
  43.     @Override
  44.     public void onResume() {
  45.         running = true;
  46.     }
  47.    
  48.     @Override
  49.     public void onPause() {
  50.         running = false;
  51.     }
  52.    
  53.     public class DrawView extends View {
  54.         public static final String TAG = "DrawView";
  55.         List<Point> points = new ArrayList<Point>();
  56.         Paint paint = new Paint();
  57.        
  58.         public DrawView(Context context, AttributeSet attrs) {
  59.             super(context, attrs);
  60.             paint.setStyle(Paint.Style.FILL_AND_STROKE);
  61.             paint.setColor(Color.WHITE);
  62.             paint.setAntiAlias(true);
  63.  
  64.         }
  65.  
  66.         @Override
  67.         public void onDraw(Canvas canvas) {
  68.             canvas.drawColor(Color.WHITE);
  69.             for (Point point : points) {
  70.                 paint.setColor(point.color);
  71.                 canvas.drawCircle(point.x, point.y, point.radius, paint);
  72.             }
  73.         }
  74.         Random random = new Random();
  75.         private final int[] RAINBOW =  {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN};
  76.         public void addRandomPoint() {
  77.             points.add(new Point(
  78.                     random.nextFloat() * getWidth(),
  79.                     random.nextFloat() * getHeight(),
  80.                     random.nextFloat() * 30,
  81.                     RAINBOW[random.nextInt(RAINBOW.length)]));
  82.             postInvalidate();
  83.         }
  84.     }
  85.  
  86.     class Point {
  87.         Point(float x, float y, float radius, int color) {
  88.             this.x = x;
  89.             this.y = y;
  90.             this.radius = radius;
  91.             this.color = color;
  92.         }
  93.        
  94.         float x, y, radius;
  95.         int color;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement