麻烦使对象移动一圈

Dav*_*vid 2 java animation android bitmap android-canvas

我是android新手.我正在使用我在youtube上找到的教程.该应用程序在画布上绘制一个球的图片,然后对角移动.我想让球移动一圈.我已经找到了一些关于圆周运动基本数学的信息,但是我在实现它时遇到了麻烦.有人可以看看我的代码并告诉我我做错了什么.谢谢.

这是我的代码:

public class DrawingTheBall extends View {

Bitmap bball; 
int x,y;

public DrawingTheBall(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
    x = 0;
    y = 0;
}

@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);

    Rect ourRect = new Rect();
    ourRect.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
    float a = 10;
    float b = 10;
    float r = 20;
    double theta = 0;
    theta = Math.toRadians(45);

    Paint blue = new Paint();
    blue.setColor(Color.BLUE);
    blue.setStyle(Paint.Style.FILL);

    canvas.drawRect(ourRect, blue);

    if(x < canvas.getWidth()){
        //x += 10;
        x = (int) (a +r*Math.cos(theta));
    }else{
        x = 0;
    }
    if(y < canvas.getHeight()){
        //y += 10;
        y = (int) (b +r*Math.sin(theta));
    }else{
        y = 0;
    }
    Paint p = new Paint();
    canvas.drawBitmap(bball, x, y, p);
    invalidate();
}
Run Code Online (Sandbox Code Playgroud)

}

oo_*_*uel 5

基本上你需要使用正弦和余弦三角函数,给定角度将给出屏幕上相应的x和y坐标的比率.

一些东西:

double x = a + r * sin(angle);
double y = b + r * cos(angle);
Run Code Online (Sandbox Code Playgroud)

应该管用.

哪里:

r - is the radius of the circle
(a,b) - is the center of the circle
angle - is the desired angle
Run Code Online (Sandbox Code Playgroud)

当然,您需要增加角度,以便您的对象移动.


ary*_*naq 5

在数学上,圆上的点由角度theta和距中心的距离定义radius.在你的代码中,角度是一个常数,100所以它永远不会在圆上移动.您要做的是增加更新中的角度.

theta = theta + Math.toRadians(10);
x = a + r*Math.cos(theta);
y = b + r*Math.sin(theta);
Run Code Online (Sandbox Code Playgroud)

这将让你在该中心圆上的移动(a,b)半径r,10 degrees在同一时间.

对于你的评论,添加theta为一个字段,不要将其设置为45内部onDraw,如果你想从45度开始,你可以在你的构造函数中将它初始化为45.

int x,y; 
to
int x,y, theta;
Run Code Online (Sandbox Code Playgroud)

对你的评论

int x,y, theta;

public DrawingTheBall(Context context) {
    super(context);
    bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
    x = 0;
    y = 0;
    theta = 45;
}
Run Code Online (Sandbox Code Playgroud)

public void onDraw(Canvas canvas){
super.onDraw(canvas);

Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
float a = 10;
float b = 10;
float r = 20;
//    double theta = 0;  //You are using a local variable that shadows the field, it starts at 0 everytime
 //   theta = Math.toRadians(45); //You are setting it to 45 degrees everytime, instead:
    theta = theta + Math.toRadians(10); //Increase of 10 degrees

Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);

canvas.drawRect(ourRect, blue);

if(x < canvas.getWidth()){
    //x += 10;
    x = (int) (a +r*Math.cos(theta));
}else{
    x = 0;
}
if(y < canvas.getHeight()){
    //y += 10;
    y = (int) (b +r*Math.sin(theta));
}else{
    y = 0;
}
Paint p = new Paint();
canvas.drawBitmap(bball, x, y, p);
invalidate();
}
Run Code Online (Sandbox Code Playgroud)