gpt4 book ai didi

android - 在 Android 2D 游戏中使对象(位图)在 Canvas 上滚动

转载 作者:行者123 更新时间:2023-11-30 04:17:53 26 4
gpt4 key购买 nike

我们知道,在 Android 2D 游戏中移动 2D 游戏中的对象非常容易。只需使用 SurfaceView 并使用 Canvas.drawBitmap() 绘制单个位图。

但是当涉及到滚动物体时,例如一个球,我怎样才能做到这一点?使用变换矩阵会导致渲染位图的质量很差,不是吗?

最佳答案

如果您想使用基于 Canvas 的绘图旋转位图,您别无选择,只能通过 Matrix 使用变换矩阵。您传递给 Canvas.drawBitmap() 或通过直接在 Canvas 上调用的转换操作。如果您使用 Paint.setFilterBitmap(boolean) 启用位图过滤质量会很好,至少按照我的标准。

这是一个完整的示例(基于矩阵),您可以尝试一下,特别是看看打开和关闭位图过滤有什么不同(或者看看我下面的屏幕截图)。虽然它不使用 SurfaceView,只是一个普通的自定义 View ,但它应该很容易移植到 SurfaceView:

CustomView.java:

package com.example.android;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.TypedValue;
import android.view.View;

public class CustomView extends View {
private static final float DP_PER_SECONDS = 10;

private final float mBallCirfumference;
private final float mBallRadius;
private final Bitmap mBallBitmap;
private final Paint mBallBitmapPaint;
private final Matrix mBallTransformMatrix = new Matrix();
private final float mPxPerSecond;

private long mStartTime = -1;

public CustomView(Context context) {
super(context);
final Resources res = getResources();

// Load the ball bitmap. You probably want to use a better bitmap ;)
mBallBitmap = BitmapFactory.decodeResource(res, R.drawable.icon);

// We need the radius and circumference of the ball for our calculations
// later
mBallRadius = mBallBitmap.getHeight() / 2;
mBallCirfumference = mBallRadius * 2 * (float)Math.PI;

// Create ourself a paint object so we can adjust the quality of the
// bitmap drawing
mBallBitmapPaint = new Paint();

// Significantly improves quality when drawing transformed bitmaps. Compare
// with when you disable this, which is the default
mBallBitmapPaint.setFilterBitmap(true);

// Calculate speed of ball in pixels
mPxPerSecond = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP_PER_SECONDS,
res.getDisplayMetrics());
}

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

// Calculate how far into the animation we are
if (mStartTime == -1) {
mStartTime = getDrawingTime();
}
long currentTime = getDrawingTime();
float secondsPassed = (currentTime - mStartTime) / 1000.0f;

// Calculate how far the ball has moved and how many degrees it has been
// rotated as a consequence of the movement
float movedDistance = secondsPassed * mPxPerSecond;
float fullRotationsMade = movedDistance / mBallCirfumference;
float rotationInDegrees = fullRotationsMade * 360;

// Setup the transformation matrix to simulate a rolling ball
mBallTransformMatrix.reset();
mBallTransformMatrix.postRotate(rotationInDegrees, mBallRadius, mBallRadius);
mBallTransformMatrix.postTranslate(movedDistance, 0);
canvas.drawBitmap(mBallBitmap, mBallTransformMatrix, mBallBitmapPaint);

// Force redraw so we get an animation
invalidate();
}
}

ExampleActivity.java:

package com.example.android;

import android.app.Activity;
import android.os.Bundle;

public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomView(this));
}
}

位图过滤启用的示例屏幕截图。如果你运行这个例子,位图会像球一样滚动(请原谅我选错了球图形):

enter image description here

位图过滤禁用的示例屏幕截图。

enter image description here

关于android - 在 Android 2D 游戏中使对象(位图)在 Canvas 上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711202/

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