【发布时间】:2014-10-22 01:55:10
【问题描述】:
我正在尝试创建一个显示多个壁纸的应用程序,这些壁纸可以在按下按钮时进行设置。 Threrfore 我创建了一个包含 10 个不同片段的 ViewPager。每个片段应全屏显示壁纸的一部分。问题是,壁纸的清晰度非常高 (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;
}
我的问题有更好的解决方案吗?我有几张不同侧面比例的壁纸。我只想从这张壁纸中截取屏幕的尺寸并全屏显示。我怎样才能做到这一点?
提前致谢
【问题讨论】:
-
这种方式很消耗内存。试试看:stackoverflow.com/a/18015841/1531054
标签: java android android-fragments bitmap