SHOW:
|
|
- or go back to the newest paste.
| 1 | package com.example; | |
| 2 | ||
| 3 | import java.util.Random; | |
| 4 | ||
| 5 | import android.app.Activity; | |
| 6 | import android.content.Context; | |
| 7 | import android.graphics.Bitmap; | |
| 8 | import android.graphics.Canvas; | |
| 9 | import android.graphics.Color; | |
| 10 | import android.graphics.Paint; | |
| 11 | import android.os.Bundle; | |
| 12 | import android.os.Handler; | |
| 13 | import android.os.Message; | |
| 14 | import android.util.AttributeSet; | |
| 15 | import android.util.Log; | |
| 16 | import android.view.View; | |
| 17 | ||
| 18 | public class Test1 extends Activity {
| |
| 19 | /** Called when the activity is first created. */ | |
| 20 | private Handler mHandler; | |
| 21 | private DrawView draw; | |
| 22 | private boolean running; | |
| 23 | ||
| 24 | @Override | |
| 25 | public void onCreate(Bundle savedInstanceState) {
| |
| 26 | super.onCreate(savedInstanceState); | |
| 27 | draw = new DrawView(this, null); | |
| 28 | mHandler = new Handler(new Handler.Callback() {
| |
| 29 | @Override | |
| 30 | public boolean handleMessage(Message msg) {
| |
| 31 | Log.v("tag", "add random point in 500 ms");
| |
| 32 | draw.addRandomPoint(); | |
| 33 | if (running) | |
| 34 | mHandler.sendMessageDelayed(new Message(), 500); | |
| 35 | return true; | |
| 36 | } | |
| 37 | }); | |
| 38 | setContentView(draw); | |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public void onResume() {
| |
| 43 | super.onResume(); | |
| 44 | running = true; | |
| 45 | mHandler.sendMessage(new Message()); | |
| 46 | } | |
| 47 | ||
| 48 | @Override | |
| 49 | public void onPause() {
| |
| 50 | super.onPause(); | |
| 51 | running = false; | |
| 52 | } | |
| 53 | ||
| 54 | public class DrawView extends View {
| |
| 55 | public static final String TAG = "DrawView"; | |
| 56 | Paint paint = new Paint(); | |
| 57 | Bitmap cache; | |
| 58 | ||
| 59 | public DrawView(Context context, AttributeSet attrs) {
| |
| 60 | super(context, attrs); | |
| 61 | paint.setStyle(Paint.Style.FILL_AND_STROKE); | |
| 62 | paint.setColor(Color.WHITE); | |
| 63 | paint.setAntiAlias(true); | |
| 64 | ||
| 65 | } | |
| 66 | ||
| 67 | @Override | |
| 68 | public void onDraw(Canvas canvas) {
| |
| 69 | if (cache != null) | |
| 70 | canvas.drawBitmap(cache, 0, 0, paint); | |
| 71 | } | |
| 72 | ||
| 73 | Random random = new Random(); | |
| 74 | private final int[] RAINBOW = {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN,
| |
| 75 | Color.parseColor("#FF7F00"), //orange
| |
| 76 | Color.parseColor("#00ffff"),//azure
| |
| 77 | Color.parseColor("#8b00ff") //purple
| |
| 78 | }; | |
| 79 | ||
| 80 | public void addRandomPoint() {
| |
| 81 | int w = getWidth(); | |
| 82 | int h = getHeight(); | |
| 83 | if (w == 0 || h == 0) | |
| 84 | return; | |
| 85 | ||
| 86 | Point point = new Point( | |
| 87 | random.nextFloat() * getWidth(), | |
| 88 | random.nextFloat() * getHeight(), | |
| 89 | random.nextFloat() * 30, | |
| 90 | RAINBOW[random.nextInt(RAINBOW.length)]); | |
| 91 | ||
| 92 | if (cache == null) {
| |
| 93 | cache = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); | |
| 94 | } | |
| 95 | ||
| 96 | Canvas canvas = new Canvas(cache); | |
| 97 | paint.setColor(point.color); | |
| 98 | paint.setStyle((random.nextFloat()> 0.5) ? Paint.Style.STROKE : Paint.Style.FILL_AND_STROKE); | |
| 99 | paint.setStrokeWidth(point.radius / 5); | |
| 100 | canvas.drawCircle(point.x, point.y, point.radius, paint); | |
| 101 | postInvalidate(); | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | class Point {
| |
| 106 | Point(float x, float y, float radius, int color) {
| |
| 107 | this.x = x; | |
| 108 | this.y = y; | |
| 109 | this.radius = radius; | |
| 110 | this.color = color; | |
| 111 | } | |
| 112 | ||
| 113 | float x, y, radius; | |
| 114 | int color; | |
| 115 | } | |
| 116 | } |