球体运动效果
看个效果图:
自定义的View:
package eas.org;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class ColorBall {private Bitmap img; // the image of the ballprivate int coordX = 0; // the x coordinate at the canvasprivate int coordY = 0; // the y coordinate at the canvasprivate int id;// gives every ball his own id, for now not necessaryprivate 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 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 reachedif (coordX > 270) {goRight = false;}if (coordX < 0) {goRight = true;}if (coordY > 400) {goDown = false;}if (coordY < 0) {goDown = true;}// move the x and yif (goRight) {coordX += goX;} else {coordX -= goX;}if (goDown) {coordY += goY;} else {coordY -= goY;}}}