1.画一个球,在遇到四周的墙壁会反弹。
package com.example.draw2d;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;
public class Draw2D extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new BallView(this));
}
/**
* 定义一个View,用来显示球
* @author siqi
*
*/
public class BallView extends View {
private Ball ball = new Ball();
private BallThread ballThread = new BallThread(this);
public BallView(Context context) {
super(context);
/**
* 启动线程
*/
ballThread.start();
}
public Ball getBall() {
return ball;
}
/**
* onDraw在每次View被刷新的时候执行。
*/
@Override
protected void onDraw(Canvas canvas) {
ball.draw(canvas);
super.onDraw(canvas);
}
}
/**
* 定义一个线程来刷新View,处理球的运动
* @author siqi
*
*/
public class BallThread extends Thread {
/**
* 当bAlive是false的时候退出线程
*/
private boolean bAlive = true;
private BallView ballView;
/**
* 球运动的速度
*/
private int speed = 3;
/**
* x方向的运