gpt4 book ai didi

java - Android - Canvas 圆必须在第二秒左右向右进行圆周运动?

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:38 25 4
gpt4 key购买 nike

我画了一个大圆圈和一个小圆圈。小圆必须围绕大圆做圆周运动,就像太阳和地球一样。我无法解决这个问题。小圆圈没有圆周运动。如何解决这个问题?

MainActivity.java这是MainActivity类的代码。

package com.canvas.animacija.animacijasuncevogsistema;

import android.support.v7.app.AppCompatActivity;
i
mport android.os.Bundle;

public class MainActivity extends AppCompatActivity {

AnimationLayout animation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

animation = new AnimationLayout(this);

setContentView(animation);
}

@Override
protected void onPause() {
super.onPause();
animation.pause();
}

@Override
protected void onResume() {
super.onResume();
animation.resume();
}
}

AnimationLayout.java这是AnimationLayout类的代码。

package com.canvas.animacija.animacijasuncevogsistema;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class AnimationLayout extends SurfaceView implements Runnable {

Thread thread;
boolean canDraw;

Canvas canvas;
SurfaceHolder holder;

Paint yellow_fill_and_stroke, red_fill, green_fill, blue_fill, magenta_fill;

int circle1_x, circle1_y, circle2_x, circle2_y, circle3_x, circle3_y, circle4_x, circle4_y,
circle5_x, circle5_y;
int radius1, radius2, radius3, radius4, radius5;

public AnimationLayout(Context context) {
super(context);

thread = null;
canDraw = false;

holder = getHolder();

circle1_x = toPxs(180);
circle1_y = toPxs(250);
radius1 = toPxs(30);

circle2_x = toPxs(180);
circle2_y = toPxs(240) - radius1;
radius2 = toPxs(7);

}

@Override
public void run() {

prepareBrushes();

while (canDraw) {

if (!holder.getSurface().isValid()) {
continue;
}

canvas = holder.lockCanvas();
canvas.drawCircle(circle1_x, circle1_y, radius1, yellow_fill_and_stroke);
canvas.drawCircle(circle2_x, circle2_y, radius2, red_fill);
moveToRight(circle2_x, circle2_y, radius2, 10);
holder.unlockCanvasAndPost(canvas);

}

}

public void pause() {

canDraw = false;

while (true) {
try {
thread.join();
break;
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}

thread = null;

}

public void resume() {

canDraw = true;
thread = new Thread(this, "Animation Thread");
thread.start();

}

private void prepareBrushes() {
yellow_fill_and_stroke = new Paint();
yellow_fill_and_stroke.setColor(Color.YELLOW);
yellow_fill_and_stroke.setStyle(Paint.Style.FILL_AND_STROKE);
yellow_fill_and_stroke.setStrokeWidth(toPxs(3));

red_fill = new Paint();
red_fill.setColor(Color.RED);
red_fill.setStyle(Paint.Style.FILL);

green_fill = new Paint();
green_fill.setColor(Color.GREEN);
green_fill.setStyle(Paint.Style.FILL);

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

magenta_fill = new Paint();
magenta_fill.setColor(Color.MAGENTA);
magenta_fill.setStyle(Paint.Style.FILL);
}

private int toPxs(int dps) {
return (int) (dps * getResources().getDisplayMetrics().density);
}

private void moveToRight(int coordinate_x, int coordinate_y, int radius, int speed) {
int d_speed = toPxs(speed);

if (coordinate_x >= coordinate_y - radius/2 && coordinate_y >= coordinate_x + radius/2 ) {
coordinate_x += d_speed;
coordinate_y += d_speed;
}

if (coordinate_x >= coordinate_y + radius/2 && coordinate_y < coordinate_x + radius/2) {
coordinate_x -= d_speed;
coordinate_y += d_speed;
}

if (coordinate_x < coordinate_y + radius/2 && coordinate_y >= coordinate_x - radius/2) {
coordinate_x -= d_speed;
coordinate_y -= d_speed;
}

if (coordinate_x < coordinate_y - radius/2 && coordinate_y < coordinate_x - radius/2) {
coordinate_x += d_speed;
coordinate_y -= d_speed;
}
}
}

最佳答案

圆周运动需要三角函数。尝试这样的事情:

while (true)
{
angle += increment;
x = cos(angle) * radius;
y = sin(angle) * radius;
// redraw circle
}

关于java - Android - Canvas 圆必须在第二秒左右向右进行圆周运动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com