球体运动效果

看个效果图:

[img]http://dl.iteye.com/upload/attachment/293113/25472332-0976-3a82-a2e2-2f81fa538fab.png[/img]


自定义的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;
}

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值