gpt4 book ai didi

java - 创建位图缩略图的ArrayList

转载 作者:行者123 更新时间:2023-12-02 05:21:22 24 4
gpt4 key购买 nike

我正在尝试创建一个显示多个壁纸的应用程序,只需按一下按钮即可设置这些壁纸。因此,我创建了一个包含 10 个不同 fragment 的 ViewPager。每个 fragment 应全屏显示壁纸的一部分。问题是,壁纸的清晰度非常高(3000*2500),这会导致 OutOfMemoryException。这就是为什么我首先想生成一个充满屏幕尺寸位图而不是这么高分辨率的 ArrayList。尽管如此,我还是收到了 OutOfMemoryException。这是我尝试实现此目标的代码:

public ArrayList<Bitmap> createThumbnails() {
ArrayList<Bitmap> result = new ArrayList<Bitmap>();
for(int i = 0; i < NUM_PAGES; i++) {

//Bitmaps will be loaded here by resource, they're called w_0, w_1, w_2...

Bitmap b0 = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("w_" + i, "drawable", getPackageName()));

//Here the thumbnail gets created with new Dimensions (screenWidth and screenHeight)

Bitmap b1 = Bitmap.createBitmap(b0, 0, 0, screenWidth, screenHeight);

//The full-sized Bitmap gets recycled to gain some memory back

b0.recycle();

//The resized Bitmap 'b1' gets added to the ArrayList and the loop repeats

result.add(b1);
}
return result;
}

对于我的问题有更好的解决方案吗?我有几张具有不同边长比的壁纸。我只想从这张壁纸中切出屏幕的尺寸并以全屏显示。我怎样才能实现这个目标?

提前致谢

最佳答案

您尝试在缩略图中加载的图像通常需要大约 7MB,这可能会导致内存不足。在将图像显示在缩略图中之前,您需要将其缩小到较小的尺寸。

Android培训中心有这个专题handling bitmaps efficiently将帮助您解决您的问题。

关于java - 创建位图缩略图的ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26492783/

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