您可以使用BitmapRegionDecoder
来拆分较大的位图(要求API等级10)。我已经写了,将利用这个类,并返回一个Drawable
可以放在里面的方法的ImageView
:
private static final int MAX_SIZE = 1024;
private Drawable createLargeDrawable(int resId) throws IOException {
InputStream is = getResources().openRawResource(resId);
BitmapRegionDecoder brd = BitmapRegionDecoder.newInstance(is, true);
try {
if (brd.getWidth() <= MAX_SIZE && brd.getHeight() <= MAX_SIZE) {
return new BitmapDrawable(getResources(), is);
}
int rowCount = (int) Math.ceil((float) brd.getHeight()/(float) MAX_SIZE);
int colCount = (int) Math.ceil((float) brd.getWidth()/(float) MAX_SIZE);
BitmapDrawable[] drawables = new BitmapDrawable[rowCount * colCount];
for (int i = 0; i < rowCount; i++) {
int top = MAX_SIZE * i;
int bottom = i == rowCount - 1 ? brd.getHeight() : top + MAX_SIZE;
for (int j = 0; j < colCount; j++) {
int left = MAX_SIZE * j;
int right = j == colCount - 1 ? brd.getWidth() : left + MAX_SIZE;
Bitmap b = brd.decodeRegion(new Rect(left, top, right, bottom), null);
BitmapDrawable bd = new BitmapDrawable(getResources(), b);
bd.setGravity(Gravity.TOP | Gravity.LEFT);
drawables[i * colCount + j] = bd;
}
}
LayerDrawable ld = new LayerDrawable(drawables);
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
ld.setLayerInset(i * colCount + j, MAX_SIZE * j, MAX_SIZE * i, 0, 0);
}
}
return ld;
}
finally {
brd.recycle();
}
}
的方法将检查,看看是否绘制资源比都MAX_SIZE
(1024)小轴。如果是,它只是返回drawable。如果不是,它会将图像分开并对图像块进行解码并将它们放在LayerDrawable
中。
我选择了1024,因为我相信大多数可用的手机将支持至少那么大的图像。如果你想找到手机的实际纹理大小限制,你必须通过OpenGL做一些时髦的东西,这不是我想要深入的东西。
我不确定你是如何访问你的图片,所以我认为他们在你的可绘制文件夹。如果情况并非如此,那么重构该方法以获取所需的任何参数应该相当容易。
我不确定这个,但你有没有尝试过使用WebView? – 2013-03-27 09:52:52
不是,我有多个非常大的图像在列表视图上随机定位的实例。我必须考虑到性能,这听起来像一个非常糟糕的主意.. – woot 2013-03-27 12:12:31
看看这个不错的项目:https://github.com/ened/Android-Tiling-ScrollView – 2013-04-05 14:53:30