作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个显示多个壁纸的应用程序,只需按一下按钮即可设置这些壁纸。因此,我创建了一个包含 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/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!