Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使物体作圆周运动有困难_Java_Android_Animation_Bitmap_Android Canvas - Fatal编程技术网

Java 使物体作圆周运动有困难

Java 使物体作圆周运动有困难,java,android,animation,bitmap,android-canvas,Java,Android,Animation,Bitmap,Android Canvas,我对android中的动画还不熟悉。我正在使用youtube上的一个教程。该应用程序在画布上绘制一个球的图片,然后沿对角线移动。我想让球绕一圈移动。我已经找到了一些关于圆周运动基本数学的信息,但是我在实现它时遇到了困难。有人能看看我的代码,告诉我我做错了什么吗。谢谢 这是我的密码: public class DrawingTheBall extends View { Bitmap bball; int x,y; public DrawingTheBall(Context context)

我对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();
}
公共类绘图球扩展视图{
位图bball;
int x,y;
公共绘图球(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
bball=BitmapFactory.decodeResource(getResources(),R.drawable.blueball);
x=0;
y=0;
}
@凌驾
公共空白onDraw(画布){
super.onDraw(帆布);
Rect ourRect=新Rect();
ourRect.set(0,0,canvas.getWidth(),canvas.getHeight()/2);
浮点数a=10;
浮动b=10;
浮点数r=20;
双θ=0;
θ=数学托拉迪安(45);
蓝色油漆=新油漆();
blue.setColor(Color.blue);
蓝色。设置样式(油漆。样式。填充);
canvas.drawRect(ourRect,蓝色);
if(x

}

从另一张票上看一下这篇文章,看起来很相似


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

一些事情:

double x = a + r * sin(angle);
double y = b + r * cos(angle);
应该有用

其中:

r - is the radius of the circle
(a,b) - is the center of the circle
angle - is the desired angle

当然,为了使对象移动,您需要增加角度。

从数学上讲,圆上的点由角度
θ
和距离圆心
半径
定义。在您的代码中,角度是一个常数
100
,因此它不会在圆上移动。您要做的是增加更新中的角度

theta = theta + Math.toRadians(10);
x = a + r*Math.cos(theta);
y = b + r*Math.sin(theta);
这将允许您在以
(a,b)
为中心的圆上移动,半径
r
10度

在注释中,添加
theta
作为一个字段,不要在
onDraw
内部将其设置为
45
,如果要从45度开始,可以在构造函数内部将其初始化为45度

int x,y; 
to
int x,y, theta;
欢迎您的评论

int x,y, theta;

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

public void onDraw(画布){
super.onDraw(帆布);
Rect ourRect=新Rect();
ourRect.set(0,0,canvas.getWidth(),canvas.getHeight()/2);
浮点数a=10;
浮动b=10;
浮点数r=20;
//double theta=0;//您使用的是一个局部变量,它会对字段进行阴影处理,每次从0开始
//θ=Math.toRadians(45);//您每次都将其设置为45度,而不是:
θ=θ+数学。托拉半径(10);//增加10度
蓝色油漆=新油漆();
blue.setColor(Color.blue);
蓝色。设置样式(油漆。样式。填充);
canvas.drawRect(ourRect,蓝色);
if(x
它看起来是正确的,尽管我认为你应该试着改变cos和sin,所以x=(int)(a+r*Math.sin(100))和y=(int)(b+r*Math.cos(100)),然后增加100的值,让它继续在一个圆中移动。我不认为当他的坐标依赖于一个恒定的角度时,他会在圆上移动,他需要做的是增加每次更新的角度。“a”和“b”只决定圆的中心。你可能想声明θ为θ=数学。toRadians(45);其次,您每次都使用55度(每次调用onDraw并将其设置为相同值时都会声明变量),因此您应该将其设置为字段。我在哪里使用55度?我切换到θ=Math.toRadians(45),但它保持静止。同时,将θ设置为onDraw的一个区域只会使球不出现。
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();
}