gpt4 book ai didi

java - 如何逆时针旋转 Canvas android一圈

转载 作者:行者123 更新时间:2023-12-02 01:46:06 26 4
gpt4 key购买 nike

我无法解决问题,我需要帮助。我创建了围绕太阳旋转的行星,我需要一颗行星逆时针旋转,我的问题就开始了。我尝试一切我熟悉的事情。我是初学者,这是简单的代码,但我无法解决它。请帮忙。提前致谢。

我尝试逆时针旋转,只是加上减号,但这不起作用。我尝试使用半径和角度,但没有成功。我在代码中某个地方失败了,但我不知道在哪里。

...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

screenDimensions = getDisplayDimensions();

Bitmap bitmap = Bitmap.createBitmap(1200, 2000, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.parseColor("#FFFFFF"));
canvas = new Canvas();
canvas.setBitmap(bitmap);

paint = new Paint();
paint1 = new Paint();
paint2 = new Paint();
paint3 = new Paint();
paint4 = new Paint();

paint.setColor(Color.YELLOW);
paint1.setColor(Color.RED);
paint2.setColor(Color.BLUE);
paint3.setColor(Color.BLACK);
paint4.setColor(Color.MAGENTA);


imageView = findViewById(R.id.imageView3);
imageView.setImageBitmap(bitmap);

timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(
new Runnable() {
@Override
public void run() {
animationFrame();

}
}
);
}
}, 2000, 80);


}

private int[] getDisplayDimensions() {
DisplayMetrics displayMetrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
return new int[]{width, height};
}

private void animationFrame() {

canvas.drawColor(Color.parseColor("#EBEBEB"));

paint.setColor(Color.YELLOW);
canvas.drawCircle(500f, 750f, 130f, paint);


if (paint1 != null | paint3 != null) {

paint1.setColor(Color.RED);
canvas.drawCircle(400f, 560f, 51f, paint1);
canvas.rotate(10f, 500f, 750f);


paint3.setColor(Color.BLACK);
canvas.rotate(30f, 500f, 750f);
canvas.drawCircle(350f, 1050f, 45, paint3);


}
if (paint2 != null | paint4 != null) {

paint2.setColor(Color.MAGENTA);
canvas.rotate(-10f, 500f, 750f);
canvas.drawCircle(720f, 950f, 48, paint2);

paint4.setColor(Color.GRAY);
canvas.rotate(-25f, 500f, 750f);
canvas.drawCircle(290f, 330f, 42, paint4);

}

imageView.invalidate();
}

}

...

我预计其中一颗行星会逆时针旋转。

最佳答案

编辑

这段代码非常适合我:

在你的例子中,rotationPoint是太阳的中心点,bodyCenter是行星的中心点。

最终点 cx 和 cy 是行星绕太阳旋转后的中心点。

    class Vector {
float x;
float y;

Vector(float x, float y){
this.x = x;
this.y = y;
}
}
    public Vector getRotatedPoint(boolean isClockwise, float rotation, Vector bodyCenter, Vector rotationPoint){
float sin = MathUtils.sin(rotation * MathUtils.degreesToRadians);
float cos = MathUtils.cos(rotation * MathUtils.degreesToRadians);
float xSin = (bodyCenter.x - rotationPoint.x) * sin;
float xCos = (bodyCenter.x - rotationPoint.x) * cos;
float ySin = (bodyCenter.y - rotationPoint.y) * sin;
float yCos = (bodyCenter.y - rotationPoint.y) * cos;
float cx, cy;
if (isClockwise) {
//For clockwise rotation
cx = rotationPoint.x + xCos + ySin;
cy = rotationPoint.y - xSin + yCos;
} else {
//For counter clockwise rotation
cx = rotationPoint.x + xCos - ySin;
cy = rotationPoint.y + xSin + yCos;
}
return new Vector(cx, cy);
}

要使用它,您必须先调用该方法,然后画一个圆

Vector newCenter = getRotatedPoint(true, 10f, planetCenter, sunCenter)
canvas.drawCircle(newCenter.x, newCenter.y, 48, paint2);

关于java - 如何逆时针旋转 Canvas android一圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57453690/

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