【问题标题】:Rescaling Image to Fit Screen from Assets Folder从资产文件夹重新缩放图像以适应屏幕
【发布时间】:2016-04-02 18:54:16
【问题描述】:

我在资产文件夹中有各种不同大小的图像,我正在按照制作 android 游戏的教程here 进行操作,我想知道如何缩放图像以适应屏幕尺寸? 我正在使用 4 个不同的类和一个主菜单类,我正在尝试加载菜单图像:

Graphics.java:

package com.ashmore.framework;


import android.graphics.Paint;

public interface Graphics {
public static enum ImageFormat {
    ARGB8888, ARGB4444, RGB565
}

public Image newImage(String fileName, ImageFormat format);

public void clearScreen(int color);

public void drawLine(int x, int y, int x2, int y2, int color);

public void drawRect(int x, int y, int width, int height, int color);

public void drawImage(Image image, int x, int y, int srcX, int srcY,
        int srcWidth, int srcHeight);

public void drawImage(Image Image, int x, int y);

void drawString(String text, int x, int y, Paint paint);

public int getWidth();

public int getHeight();

public void drawARGB(int i, int j, int k, int l);

}

AndroidGraphics.java:

package com.ashmore.framework.Implementation;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

import com.ashmore.framework.Graphics;
import com.ashmore.framework.Image;

public class AndroidGraphics implements Graphics {
AssetManager assets;
Bitmap frameBuffer;
Canvas canvas;
Paint paint;
Rect srcRect = new Rect();
Rect dstRect = new Rect();

public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
    this.assets = assets;
    this.frameBuffer = frameBuffer;
    this.canvas = new Canvas(frameBuffer);
    this.paint = new Paint();
}

@Override
public Image newImage(String fileName, ImageFormat format) {
    Config config = null;
    if (format == ImageFormat.RGB565)
        config = Config.RGB_565;
    else if (format == ImageFormat.ARGB4444)
        config = Config.ARGB_4444;
    else
        config = Config.ARGB_8888;

    Options options = new Options();
    options.inPreferredConfig = config;


    InputStream in = null;
    Bitmap bitmap = null;
    try {
        in = assets.open(fileName);
        bitmap = BitmapFactory.decodeStream(in, null, options);
        if (bitmap == null)
            throw new RuntimeException("Couldn't load bitmap from asset '"
                    + fileName + "'");
    } catch (IOException e) {
        throw new RuntimeException("Couldn't load bitmap from asset '"
                + fileName + "'");
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }

    if (bitmap.getConfig() == Config.RGB_565)
        format = ImageFormat.RGB565;
    else if (bitmap.getConfig() == Config.ARGB_4444)
        format = ImageFormat.ARGB4444;
    else
        format = ImageFormat.ARGB8888;

    return new AndroidImage(bitmap, format);
}

@Override
public void clearScreen(int color) {
    canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8,
            (color & 0xff));
}


@Override
public void drawLine(int x, int y, int x2, int y2, int color) {
    paint.setColor(color);
    canvas.drawLine(x, y, x2, y2, paint);
}

@Override
public void drawRect(int x, int y, int width, int height, int color) {
    paint.setColor(color);
    paint.setStyle(Style.FILL);
    canvas.drawRect(x, y, x + width - 1, y + height - 1, paint);
}

@Override
public void drawARGB(int a, int r, int g, int b) {
    paint.setStyle(Style.FILL);
   canvas.drawARGB(a, r, g, b);
}

@Override
public void drawString(String text, int x, int y, Paint paint){
    canvas.drawText(text, x, y, paint);


}


public void drawImage(Image Image, int x, int y, int srcX, int srcY,
        int srcWidth, int srcHeight) {
    srcRect.left = srcX;
    srcRect.top = srcY;
    srcRect.right = srcX + srcWidth;
    srcRect.bottom = srcY + srcHeight;


    dstRect.left = x;
    dstRect.top = y;
    dstRect.right = x + srcWidth;
    dstRect.bottom = y + srcHeight;

    canvas.drawBitmap(((AndroidImage) Image).bitmap, srcRect, dstRect,
            null);
}

@Override
public void drawImage(Image Image, int x, int y) {
    canvas.drawBitmap(((AndroidImage)Image).bitmap, x, y, null);
}

public void drawScaledImage(Image Image, int x, int y, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight){


 srcRect.left = srcX;
    srcRect.top = srcY;
    srcRect.right = srcX + srcWidth;
    srcRect.bottom = srcY + srcHeight;


    dstRect.left = x;
    dstRect.top = y;
    dstRect.right = x + width;
    dstRect.bottom = y + height;



    canvas.drawBitmap(((AndroidImage) Image).bitmap, srcRect, dstRect, null);

}

@Override
public int getWidth() {
    return frameBuffer.getWidth();
}

@Override
public int getHeight() {
    return frameBuffer.getHeight();
}
}

资产.java

package com.ashmore.ballescape;

import com.ashmore.framework.Image;
import com.ashmore.framework.Music;
import com.ashmore.framework.Sound;

public class Assets {

public static Image background;
public static Image menu;
public static Image loading;

public static Sound click;
public static Music theme;

public static void load (SampleGame sampleGame) {
    theme = sampleGame.getAudio().createMusic("menutheme.mp3");
    theme.setLooping(true);
    theme.setVolume(0.85f);
    theme.play();
}
}

LoadingScreen.java:

public class LoadingScreen extends Screen {
public LoadingScreen(Game game) {
    super(game);
}


@Override
public void update(float deltaTime) {
    Graphics g = game.getGraphics();
    Assets.menu = g.newImage("menu.jpg", ImageFormat.RGB565); 
    // Assets.background = g.newImage("background.jpg", ImageFormat.RGB565);
    Assets.loading = g.newImage("loading.png", ImageFormat.RGB565);

    //Assets.click = game.getAudio().createSound("explode.ogg");

    game.setScreen(new MainMenuScreen(game));


}

最后,例如,在这个主菜单类中,我想从我的资产文件夹中将菜单图像绘制到屏幕上。但是该图像不是正确的屏幕尺寸,我希望能够将其考虑到调整后的尺寸,以便我的游戏可以在多个设备上运行。

MainMenuScreen.java(相关部分):

@Override
public void paint(float deltaTime) {
    Graphics g = game.getGraphics();
    g.drawImage(Assets.menu, 0, 0);
}

我该怎么做呢?我不想在 ImageView 中设置图像,因为我使用纯 java 并且没有用于游戏的 xml ......至少对于我将使用的背景图像......我可能应该保留其他资产(如角色) 一定的大小,对吧?

【问题讨论】:

    标签: java android image


    【解决方案1】:

    得到它

    Bitmap bitmapBob;
    bitmapBob = BitmapFactory.decodeResource(this.getResources(), R.drawable.wall);
    

    然后申请你的背景

        Rect dest = new Rect(0, 0, getWidth(), getHeight());
        // Draw bob as background with dest size
        canvas.drawBitmap(bitmapBob, null, dest, paint);
    

    如有必要,请使用此功能

    // Resize Bitmap function to Handle all the Images from resources the right size
    public Bitmap getResizedBitmap(Bitmap bm, float newWidth, int newHeight) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = newWidth / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);
    
        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
                bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2011-05-12
      • 2014-05-09
      • 2014-01-18
      • 2021-06-22
      相关资源
      最近更新 更多