這裏通過演示一個可以拖動顏色球的例子來展示Android中onTouchEvent的使用以及自定義View。
實體類ColorBall:
/**
* @author gongchaobin
*
* 實體類 顏色球
*/
public class ColorBall {
private Bitmap img; //小球的圖片
private int coordX = 0; //畫布上的X座標
private int coordY = 0; //畫布上的Y座標
private int id;
private static int count = 1;
private boolean goRight = true;
private boolean goDown = true;
public ColorBall(Context context, int drawable) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
img = BitmapFactory.decodeResource(context.getResources(), drawable);
id=count;
count++;
}
public ColorBall(Context context, int drawable, Point point) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
img = BitmapFactory.decodeResource(context.getResources(), drawable);
id=count;
count++;
coordX= point.x;
coordY = point.y;
}
public static int getCount() {
return count;
}
void setX(int newValue) {
coordX = newValue;
}
public int getX() {
return coordX;
}
void setY(int newValue) {
coordY = newValue;
}
public int getY() {
return coordY;
}
public int getID() {
return id;
}
public Bitmap getBitmap() {
return img;
}
public void moveBall(int goX, int goY) {
// check the borders, and set the direction if a border has reached
if (coordX > 270){
goRight = false;
}
if (coordX < 0){
goRight = true;
}
if (coordY > 400){
goDown = false;
}
if (coordY < 0){
goDown = true;
}
if (goRight){
coordX += goX;
}else{
coordX -= goX;
}
if (goDown){
coordY += goY;
}else{
coordY -= goY;
}
}
}
自定義DrawView:
/**
* @author gongchaobin
*
* 自定義View
*/
public class DrawView extends View {
private ColorBall[] colorballs = new ColorBall[3];
private int balID = 0;
public DrawView(Context context) {
super(context);
setFocusable(true);
/**在畫布上設置三個初始的點*/
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;
/**New三個顏色球*/
colorballs[0] = new ColorBall(context,R.drawable.bol_groen, point1);
colorballs[1] = new ColorBall(context,R.drawable.bol_rood, point2);
colorballs[2] = new ColorBall(context,R.drawable.bol_blauw, point3);
}
@Override
protected void onDraw(Canvas canvas) {
/**重繪顏色球*/
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
/**當初始進來的時候 ,向下移動的狀態*/
case MotionEvent.ACTION_DOWN:
balID = 0;
for (ColorBall ball : colorballs) {
/**獲取到小球的中心點*/
int centerX = ball.getX() + 25;
int centerY = ball.getY() + 25;
double radCircle = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
/**如果移動的距離小於23,小球沒動*/
if (radCircle < 23){
balID = ball.getID();
break;
}
}
break;
/**balId>0,小球的移動狀態*/
case MotionEvent.ACTION_MOVE:
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}
break;
case MotionEvent.ACTION_UP:
break;
}
/**invalidate方法 進行界面刷新重繪*/
invalidate();
return true;
}
}