当前位置:  开发笔记 > Android > 正文

缩放位图以适合画布

如何解决《缩放位图以适合画布》经验,为你挑选了0个好方法。

我试图在机器人上写一个游戏,我照顾不同的屏幕分辨率的方式是我制作一个目标分辨率(320x480)的位图,从它制作一个画布,并使用固定坐标绘制其上的所有元素,然后我只是在surfaceView画布上绘制此位图,它在不同的屏幕上重新缩放.问题是,当我在框架上绘制内容时,即使框架与位图的大小相同,位图也会超出范围.(位图是320x480,它得到拉伸,不适合屏幕)这是代码:

public class Graphics {

private Bitmap frameBuffer, grid;
private Canvas canvas;

public Graphics(Context context) {

    frameBuffer = Bitmap.createBitmap(320, 480, Config.ARGB_8888);
    canvas = new Canvas(frameBuffer);

    grid = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.grid);
}

public Bitmap present() {
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(grid, 0, 0, null);
    return frameBuffer;
}

和SurfaceView类:

public class GameView extends SurfaceView implements Runnable {

private SurfaceHolder holder;
private Boolean running;
private Thread renderThread;
private Graphics graphics;

public GameView(Context context, Graphics graphics) {
    super(context);
    this.holder = getHolder();
    this.graphics = graphics;
}

public void resume() {
    running = true;
    renderThread = new Thread(this);
    renderThread.start();
}

public void run() {
    Rect dstRect = new Rect();
    while (running) {
        if (!holder.getSurface().isValid())
            continue;

        Bitmap frameBuffer = graphics.present();

        Canvas canvas = holder.lockCanvas();
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(frameBuffer, null, dstRect, null);
        holder.unlockCanvasAndPost(canvas);
    }
}

public void pause() {
    running = false;
    while (true) {
        try {
            renderThread.join();
            break;
        } catch (InterruptedException e) {
            // retry
        }
    }
}

}

任何人都可以给出一些提示吗?PS如果我只是将位图直接绘制到SurfacView画布上,它就会完美缩放.

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有