在Android中绘制大型位图

时间:2016-07-28 16:00:56

标签: java android canvas bitmap

我不知道这个问题是否得到了解答。至少我没有找到答案。

所以这是一件事:我在Android上制作了一些以空间为主题的2D游戏,我在模拟器上测试它,屏幕尺寸= 2560x1600。在这个游戏中有一个太空飞船飞行的领域。当然,它(一个领域)必须具有高分辨率的美丽背景。我的背景图片分辨率为4500x4500。我想让我的图像相对于相机移动方向相反,这就是为什么我不能使用小静态图像。当时只有该图像的一部分可见:

Example 1

当我试图绘制它时,我得到了fps = 1-2(当然它因图像大小而很低):

canvas.drawBitmap(getBigImage(), -x, -y, null); 
/* getBigImage() method does nothing but returning 
a Bitmap object (no calculation or decoding is performing in there) */

我试图从大图像中删除所需的图像,但fps仍然很低:

Bitmap b = Bitmap.createBitmap(getBigImage(), x, y, sw, sh);
canvas.drawBitmap(b, 0, 0, null);

如何用高fps绘制这个大位图?

2 个答案:

答案 0 :(得分:3)

尝试

drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)

从源图像中获取一个像素矩形,以在Canvas上的矩形中显示。当我进行滚动游戏时,我发现这会更快。

答案 1 :(得分:1)

我在思考很多,想出了将输入位图划分为小块并将它们保存到数组的想法。所以现在绘制该位图我所要做的就是绘制可见的块。

照片:

enter image description here

大黑色矩形表示输入位图,绿色矩形表示视口,红色矩形表示绘制的可见块

我已经写了一个完成所有操作的对象(我还没有检查错误:/)。我测试了它,它以~45 fps绘制了3000x3000位图。我认为这种方式非常有效。对象本身可能需要更多地开发,但我认为这个功能足以满足我的需求。希望它能帮助某人:)

P.S。 https://stackoverflow.com/a/25953122/6121671 - 用它作灵感:)

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public final class DividedBitmap {
    private final Bitmap[][] mArray;    // array where chunks is stored

    private final int mWidth;           // original (full) width of source image
    private final int mHeight;          // original (full) height of source image
    private final int mChunkWidth;      // default width of a chunk
    private final int mChunkHeight;     // default height of a chunk

    /* Init */

    public DividedBitmap(Bitmap src) {
        this(new Options(src, 100, 100));
    }

    public DividedBitmap(Options options) {
        mArray = divideBitmap(options);

        mWidth = options.source.getWidth();
        mHeight = options.source.getHeight();
        mChunkWidth = options.chunkWidth;
        mChunkHeight = options.chunkHeight;
    }

    /* Getters */

    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }

    public Bitmap getChunk(int x, int y) {
        if (mArray.length < x && x > 0 && mArray[x].length < y && y > 0) {
            return mArray[x][y];
        }

        return null;
    }

    /* Methods */

    /**
     *  x, y are viewport coords on the image itself;
     *  w, h are viewport's width and height.
     */
    public void draw(Canvas canvas, int x, int y, int w, int h, Paint paint) {
        if (x >= getWidth() || y >= getHeight() || x + w <= 0 || y + h <= 0)
            return;

        int i1 = x / mChunkWidth;           // i1 and j1 are indices of visible chunk that is
        int j1 = y / mChunkHeight;          // on the top-left corner of the screen
        int i2 = (x + w) / mChunkWidth;     // i2 and j2 are indices of visible chunk that is
        int j2 = (y + h) / mChunkHeight;    // on the right-bottom corner of the screen

        i2 = i2 >= mArray.length ? mArray.length - 1 : i2;
        j2 = j2 >= mArray[i2].length ? mArray[i2].length - 1 : j2;

        int offsetX = x - i1 * mChunkWidth;
        int offsetY = y - j1 * mChunkHeight;

        for (int i = i1; i <= i2; i++) {
            for (int j = j1; j <= j2; j++) {
                canvas.drawBitmap(
                        mArray[i][j],
                        (i - i1) * mChunkWidth - offsetX,
                        (j - j1) * mChunkHeight - offsetY,
                        paint
                );
            }
        }
    }

    /* Static */

    public static Bitmap[][] divideBitmap(Bitmap bitmap) {
        return divideBitmap(new Options(bitmap, 100, 100));
    }

    public static Bitmap[][] divideBitmap(Options options) {
        Bitmap[][] arr = new Bitmap[options.xCount][options.yCount];

        for (int x = 0; x < options.xCount; ++x) {
            for (int y = 0; y < options.yCount; ++y) {
                int w = Math.min(options.chunkWidth, options.source.getWidth() - (x * options.chunkWidth));
                int h = Math.min(options.chunkHeight, options.source.getHeight() - (y * options.chunkHeight));
                arr[x][y] = Bitmap.createBitmap(options.source, x * options.chunkWidth, y * options.chunkHeight, w, h);
            }
        }

        return arr;
    }

    public static final class Options {
        final int chunkWidth;
        final int chunkHeight;
        final int xCount;
        final int yCount;
        final Bitmap source;

        public Options(Bitmap src, int chunkW, int chunkH) {
            chunkWidth = chunkW;
            chunkHeight = chunkH;

            xCount = ((src.getWidth() - 1) / chunkW) + 1;
            yCount = ((src.getHeight() - 1) / chunkH) + 1;

            source = src;
        }

        public Options(int xc, int yc, Bitmap src) {
            xCount = xc;
            yCount = yc;

            chunkWidth = src.getWidth() / xCount;
            chunkHeight = src.getHeight() / yCount;

            source = src;
        }
    }
}