`

球体运动效果

阅读更多
看个效果图:





自定义的View:
package eas.org;

import android.content.Context;
import android.graphics.Canvas;
import android.view.View;

public class DrawView extends View {
	private ColorBall colorball1, colorball2, colorball3, colorball4,
			colorball5;

	public DrawView(Context context) {
		super(context);
		setFocusable(true);
		// not yet necessary, but you never know what the future brings

		// declare each ball with the ColorBall class
		colorball1 = new ColorBall(context, R.drawable.bol_groen);
		colorball2 = new ColorBall(context, R.drawable.bol_rood);
		colorball3 = new ColorBall(context, R.drawable.bol_blauw);
		colorball4 = new ColorBall(context, R.drawable.bol_geel);
		colorball5 = new ColorBall(context, R.drawable.bol_paars);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(0xFFCCCCCC);
		// if you want another background color

		// move the balls at every canvas draw
		colorball1.moveBall(5, 3);
		colorball2.moveBall(3, 4);
		colorball3.moveBall(2, 2);
		colorball4.moveBall(4, 5);
		colorball5.moveBall(5, 1);

		// draw the balls on the canvas
		canvas.drawBitmap(colorball1.getBitmap(), colorball1.getX(),
				colorball1.getY(), null);
		canvas.drawBitmap(colorball2.getBitmap(), colorball2.getX(),
				colorball2.getY(), null);
		canvas.drawBitmap(colorball3.getBitmap(), colorball3.getX(),
				colorball3.getY(), null);
		canvas.drawBitmap(colorball4.getBitmap(), colorball4.getX(),
				colorball4.getY(), null);
		canvas.drawBitmap(colorball5.getBitmap(), colorball5.getX(),
				colorball5.getY(), null);

		// refresh the canvas
		invalidate();
	}

}



球体运动控制:
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 ball
	private int coordX = 0; // the x coordinate at the canvas
	private int coordY = 0; // the y coordinate at the canvas
	private int id;

	// gives every ball his own id, for now not necessary
	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 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;
		}
		// move the x and y
		if (goRight) {
			coordX += goX;
		} else {
			coordX -= goX;
		}
		if (goDown) {
			coordY += goY;
		} else {
			coordY -= goY;
		}

	}

}
  • 大小: 21.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics