【Android】38.使用SurfaceView绘制图形
程序员文章站
2024-03-23 15:20:28
...
38.使用SurfaceView绘制图形
1.下载素材。
本节素材源码,请在公众号回复" AS12192 "。
2.SurfaceView绘制单个图形。
1.新建MyView。
public class MyView extends SurfaceView implements Callback {
private Paint paint = null;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLUE);
getHolder().addCallback(this);
}
public void draw() {
Canvas canvas = getHolder().lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawRect(0, 0, 800, 300, paint);
getHolder().unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override
public void surfaceCreated(SurfaceHolder holder) {
draw();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) { }
}
2.修改MainActivity。
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
}
3.效果图。
3.SurfaceView绘制多个图形。
1.新建MyView。
public class MyView extends SurfaceView implements Callback {
private Paint paint = null;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLUE);
getHolder().addCallback(this);
}
public void draw() {
Canvas canvas = getHolder().lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.save();
canvas.rotate(90, getWidth()/2,getHeight()/2);
canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight(), paint);
canvas.restore();
canvas.drawLine(0, getHeight() / 2 + 100, getWidth(), getHeight() + 100, paint);
getHolder().unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
draw();
}
@Override
public void surfaceCreated(SurfaceHolder holder) { }
@Override
public void surfaceDestroyed(SurfaceHolder holder) { }
}
2.修改MainActivity。
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
}
3.效果图。
4.SurfaceView绘制组合图形。
1.新建Contanier。
public class Contanier {
private List<Contanier> children = null;
private float x = 0, y = 0;
public Contanier() {
children = new ArrayList<Contanier>();
}
public void draw(Canvas canvas) {
canvas.save();
canvas.translate(getX(), getY());
childrenView(canvas);
for (Contanier c : children) {
c.draw(canvas);
}
canvas.restore();
}
public void childrenView(Canvas canvas) {
}
public void addChildrenView(Contanier child) {
children.add(child);
}
public void removeChildrenView(Contanier child) {
children.remove(child);
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}
2.新建Circle。
public class Circle extends Contanier {
private Paint paint = null;
public Circle() {
paint = new Paint();
paint.setColor(Color.YELLOW);
}
@Override
public void childrenView(Canvas canvas) {
super.childrenView(canvas);
canvas.drawCircle(500, 500, 500, paint);
}
}
3.新建Rect。
public class Rect extends Contanier {
private Paint paint = null;
private GameView view = null;
public Rect() {
paint = new Paint();
paint.setColor(Color.BLACK);
}
@Override
public void childrenView(Canvas canvas) {
super.childrenView(canvas);
canvas.drawRect(0, 0, 1000, 1000, paint);
this.setY(this.getY() + 5);
this.setX(this.getX() + 5);
}
}
4.新建GameView。
public class GameView extends SurfaceView implements Callback {
private Contanier contanier;
private Rect rect;
private Circle circle;
public GameView(Context context) {
super(context);
contanier = new Contanier();
rect = new Rect();
circle = new Circle();
rect.addChildrenView(circle);
contanier.addChildrenView(rect);
getHolder().addCallback(this);
}
public void draw() {
Canvas canvas = getHolder().lockCanvas();
canvas.drawColor(Color.WHITE);
contanier.draw(canvas);
getHolder().unlockCanvasAndPost(canvas);
}
private Timer timer = null;
private TimerTask task = null;
public void startTimer() {
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
draw();
}
};
timer.schedule(task, 100, 100);
}
public void stopTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override
public void surfaceCreated(SurfaceHolder holder) {
startTimer();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
stopTimer();
}
}
5.修改MainActivity。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
}