【问题标题】:Large scrolled image android studio大型滚动图像 android studio
【发布时间】:2023-03-06 21:02:02
【问题描述】:

我在加载大图片时遇到问题。 我必须制作尺寸为 3556 x 2000 像素的地图/背景。

我试试这个: https://developer.android.com/topic/performance/graphics/load-bitmap.html 但它看起来对我来说不能正常工作。 (例外:内存不足)

这是我的背景: scr.hu/8p0mdz - 屏幕截图

我用黑色方块标记了用户在手机上可见的区域。当然,用户可以放大或缩小可见区域。

我无法使用 libgdx。我只想使用 android 库。我不知道我应该如何开始我的工作。我不要求代码(我很乐意接受代码),但我想找出我应该从什么开始。

在此背景下,将绘制其他图像(建筑物)。游戏开始时,需要将资源加载到内存中。在 libgdx 中,我可以使用 AssetManager。就我而言,当我使用

https://developer.android.com/reference/android/content/res/AssetManager.html

应该够了吧?

我希望你能理解我的问题。

【问题讨论】:

  • 你可以试试滑翔吗?看看有没有效果
  • 没有。我什至没有听说过。感谢帮助。如果我找不到解决方案,我会尝试。 :)

标签: java android image


【解决方案1】:

这不是一个小问题,所以我认为没有简单的解决方案。但是有一些事情可以提供帮助:

  1. 您可以控制为位图分配的内存量:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    //RGB_565 color format requires way less memory than ARGB_8888
    
  2. BitmapRegionDecoder 类只加载位图的一部分。

    BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance("path", false);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap partOfBitmap = decoder.decodeRegion(new Rect(0, 0, 100, 100), options);
    

使用BitmapRegionDecoder,您可以开发您的自定义View,它处理用户滚动事件并仅将可见图像区域加载到内存中并销毁不可见区域。

【讨论】:

  • 我认为这可行。但我不知道为什么位图的大小为 9335 x 5250,当我放 3556 x 2000px 时。
【解决方案2】:

我尝试如上解决这个问题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_planet);
    int WIDTH = 0;
    int HEIGHT = 0;
    backgroundJungle = (ImageView)findViewById(R.id.backgroundJungle);

    InputStream is = null;
    try {
        Drawable drawable = getResources().getDrawable(R.drawable.junglemap);
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();
        WIDTH = bitmap.getWidth();
        HEIGHT = bitmap.getHeight();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        is = new ByteArrayInputStream(stream.toByteArray());
    } catch (Exception ex) {
        ManagerException(ex);
    }

    try {
        if (is != null) {
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            Bitmap partOfBitmap = decoder.decodeRegion(new Rect(0,0,2500,2500),options);
            backgroundJungle.setImageBitmap(partOfBitmap);
            Toast.makeText(getApplicationContext(), WIDTH + " " + HEIGHT, Toast.LENGTH_SHORT).show();
        }
    } catch (IOException ex) {
        ManagerException(ex);
    }
}

但位图的大小为 9335 x 5250,但我的图像大小为 3556 x 2000。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 2019-12-25
    • 2022-01-06
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多