| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.test.hwui; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.content.Context; |
| 21 |
import android.graphics.Canvas; |
| 22 |
import android.graphics.Paint; |
| 23 |
import android.graphics.Path; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.util.MathUtils; |
| 26 |
import android.view.View; |
| 27 |
|
| 28 |
import java.util.Random; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class PathDestructionActivity extends Activity { |
| 38 |
|
| 39 |
private static final int MIN_SIZE = 20; |
| 40 |
@Override |
| 41 |
protected void onCreate(Bundle savedInstanceState) { |
| 42 |
super.onCreate(savedInstanceState); |
| 43 |
|
| 44 |
MyView view = new MyView(this); |
| 45 |
setContentView(view); |
| 46 |
} |
| 47 |
|
| 48 |
private static class MyView extends View { |
| 49 |
Paint strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 50 |
Paint fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 51 |
Paint fillAndStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 52 |
|
| 53 |
private MyView(Context context) { |
| 54 |
super(context); |
| 55 |
strokePaint.setStyle(Paint.Style.STROKE); |
| 56 |
fillPaint.setStyle(Paint.Style.FILL); |
| 57 |
fillAndStrokePaint.setStyle(Paint.Style.FILL_AND_STROKE); |
| 58 |
} |
| 59 |
|
| 60 |
private Path getRandomPath() { |
| 61 |
float left, top, right, bottom; |
| 62 |
Random r = new Random(); |
| 63 |
left = r.nextFloat() * (getWidth() - MIN_SIZE); |
| 64 |
top = r.nextFloat() * (getHeight() - MIN_SIZE); |
| 65 |
right = left + r.nextFloat() * (getWidth() - left); |
| 66 |
bottom = top + r.nextFloat() * (getHeight() - top); |
| 67 |
Path path = new Path(); |
| 68 |
path.moveTo(left, top); |
| 69 |
path.lineTo(right, top); |
| 70 |
path.lineTo(right, bottom); |
| 71 |
path.lineTo(left, bottom); |
| 72 |
path.close(); |
| 73 |
return path; |
| 74 |
} |
| 75 |
|
| 76 |
private int getRandomColor() { |
| 77 |
Random r = new Random(); |
| 78 |
int red = r.nextInt(255); |
| 79 |
int green = r.nextInt(255); |
| 80 |
int blue = r.nextInt(255); |
| 81 |
return 0xff000000 | red << 16 | green << 8 | blue; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
protected void onDraw(Canvas canvas) { |
| 86 |
Path path; |
| 87 |
for (int i = 0; i < 15; ++i) { |
| 88 |
path = getRandomPath(); |
| 89 |
strokePaint.setColor(getRandomColor()); |
| 90 |
canvas.drawPath(path, strokePaint); |
| 91 |
path = null; |
| 92 |
path = getRandomPath(); |
| 93 |
fillPaint.setColor(getRandomColor()); |
| 94 |
canvas.drawPath(path, fillPaint); |
| 95 |
path = null; |
| 96 |
path = getRandomPath(); |
| 97 |
fillAndStrokePaint.setColor(getRandomColor()); |
| 98 |
canvas.drawPath(path, fillAndStrokePaint); |
| 99 |
path = null; |
| 100 |
} |
| 101 |
|
| 102 |
invalidate(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |