gpt4 book ai didi

机器人旋转图像

转载 作者:行者123 更新时间:2023-11-30 04:52:49 27 4
gpt4 key购买 nike

我正在尝试创建两个纺车,就像在滑轮中一样,因此每次连接的绳索移动时,两个滑轮都会旋转。我尝试了两种方法:

1) 在 View 类的 onDraw() 方法中使用 Matrix.postRotate,它调用以下内容:

private void drawSpinningWheel(Canvas canvas)
{
try
{
canvas.save();

Bitmap bitmapOrg = null;

int iDrawable = R.drawable.spinning_button;

bitmapOrg = BitmapFactory.decodeResource(getResources(), iDrawable);

if(bitmapOrg != null)
{
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 24;
int newHeight = 24;

// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate((float) mDegrees++);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);

canvas.drawBitmap(resizedBitmap, matrix, null);

}

canvas.restore();
}
catch(Exception e)
{
Log.e(TAG + "drawSpinningWheel", e.getMessage());
}

}

但看起来图像不仅旋转而且还绕着另一个轴旋转

2) 使用 SurfaceView 和一个单独的线程,在 run() 中调用:

    private void doDraw(Canvas canvas) {
// Draw the background image. Operations on the Canvas accumulate
// so this is like clearing the screen.
canvas.drawBitmap(mBackgroundImage, 0, 0, null);

int yTop = mCanvasHeight - ((int) mY + mSpinningWheelImageHeight / 2);
int xLeft = (int) mX - mSpinningWheelImageWidth / 2;

...

        // Draw the ship with its current rotation
canvas.save();
canvas.rotate((float) mHeading++, (float) mX, mCanvasHeight
- (float) mY);

mSpinningWheelImage.setBounds(xLeft, yTop, xLeft + mSpinningWheelImageWidth, yTop
+ mSpinningWheelImageHeight);
mSpinningWheelImage.draw(canvas);


canvas.restore();
}

我可以让纺车工作,但我无法添加另一个纺车。我什至试图为第二个纺车创建另一个线程,只有一个出现。有人能指出我正确的方向吗?谢谢。

3) 好的,现在我也尝试了 RotateAnimation:

公共(public)类 GraphicsView 扩展 View { 私有(private)静态最终字符串引用 = “没有人再使用 Java。这是一个重量级的大链条。”;

private Animation anim;
private Animation anim2;

private Bitmap jobs;
private Bitmap wheel;

private int jobsXOffset;
private int jobsYOffset;

private int wheelXOffset;
private int wheelYOffset;

public GraphicsView(Context context)
{
super(context);
jobs = BitmapFactory
.decodeResource(getResources(), R.drawable.spinning_button);

jobsXOffset = jobs.getWidth() / 2;
jobsYOffset = jobs.getHeight() / 2;

wheel = BitmapFactory
.decodeResource(getResources(), R.drawable.wheel);

wheelXOffset = jobs.getWidth() / 2;
wheelYOffset = jobs.getHeight() / 2;


}

private void createAnim(Canvas canvas)
{
anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
.getHeight() / 2);
anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(10000L);
anim.setInterpolator(new AccelerateDecelerateInterpolator());

startAnimation(anim);
}

private void createAnim2(Canvas canvas)
{
anim2 = new RotateAnimation(0, 360, canvas.getWidth() / 2+30, canvas
.getHeight() / 2);
anim2.setRepeatMode(Animation.INFINITE);
anim2.setRepeatCount(Animation.INFINITE);
anim2.setDuration(10000L);
anim2.setInterpolator(new AccelerateDecelerateInterpolator());

startAnimation(anim);
}

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

// creates the animation the first time
if (anim == null) {
createAnim(canvas);
}

int centerX = canvas.getWidth() / 2;
int centerY = canvas.getHeight() / 2;

canvas.drawBitmap(jobs, centerX - jobsXOffset,
centerY - jobsYOffset, null);

canvas.drawBitmap(wheel, centerX - wheelXOffset + 30,
centerY - wheelYOffset, null);

}

但结果类似于 Canvas.Rotate,我能够让第一张图片旋转得很好,但第二张图片只会围绕第一张图片旋转,尽管我注意到第二张图片实际上也在旋转。看起来我需要一个单独的 View 来查看第二张图片,我希望避免这种情况,因为我不确定如何让轮子旋转但连接的绳子不会旋转。

最佳答案

你试过RotateAnimation了吗?

关于机器人旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2634183/

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