java随机创建形状_java – Android – 连续绘制形状到随机位置

我对

android动画有点新意.我正在开展一个项目,将一个球的图片放在一个随机的位置 – 之后它会以圆圈移动.到目前为止我已经成功,但现在我想在不同的随机坐标上不断绘制新的形状.我想过每隔几秒钟使用一个线程来绘制形状,但我似乎无法实现它而不会搞砸一切.

有谁知道我怎么解决这个问题?我也知道每次都要不断重置我的随机坐标.有谁知道我怎么做到这一点?谢谢你的帮助.我的代码如下:

public class DrawingTheBall extends View {

Bitmap bball;

int randX, randY;

double theta;

public DrawingTheBall(Context context) {

super(context);

// TODO Auto-generated constructor stub

bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);

randX = 1 + (int)(Math.random()*500);

randY = 1 + (int)(Math.random()*500);

theta = 45;

}

public void onDraw(Canvas canvas){

super.onDraw(canvas);

//Radius, angle, and coordinates for circle motion

float a = 50;

float b = 50;

float r = 50;

int x = 0;

int y = 0;

theta = theta + Math.toRadians(2);

//move ball in circle

if(x < canvas.getWidth()){

x = randX + (int) (a +r*Math.cos(theta));

}else{

x = 0;

}

if(y < canvas.getHeight()){

y = randY + (int) (b +r*Math.sin(theta));

}else{

y = 0;

}

Paint p = new Paint();

//Implement Thread here

thread = new Thread(new Runnable(){

@Override

public void run(){

for(int j = 0; j <= 60; j++){

//It tells me to change variables to Final

//But if I do that it messes up my if statements above

canvas.drawBitmap(bball, x, y, p);

}

};

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} //wait one second

}

}

});

thread.start();

//canvas.drawBitmap(bball, x, y, p);

invalidate();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值