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();
}

}

解决方法:

理念:

>实现Runnable和处理程序
>按随机创建随机坐标.

public class DrawingTheBall extends View implements Runnable {

final Bitmap bball;
Random randX, randY;
double theta;
Handler handler = new Handler(){
    public void handleMessage(android.os.Message msg) {
        invalidate();
        System.out.println("redraw");
    };
};

public DrawingTheBall(Context context) {
    super(context);
    bball = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);
    randX = new Random();
    randY = new Random();
    theta = 45;
    new Thread(this).start();
}

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.nextInt(100) + (int) (a + r * Math.cos(theta)); // create
                                                                // randX
                                                                // integer
    } else {
        x = 0;
    }
    if (y < canvas.getHeight()) {
        y = randY.nextInt(100) + (int) (b + r * Math.sin(theta));// create
                                                                // randX
                                                                // integer
    } else {
        y = 0;
    }
    Paint p = new Paint();
    canvas.drawBitmap(bball, x, y, p);
}

public void run() {
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printstacktrace();
        }
        handler.sendEmptyMessage(0);
    }
}

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...