将部分图像绘制到屏幕(不加载所有内存)

我想要做的是在我选择的位置绘制一个图像的剪切屏幕。

我可以很容易地将其加载到位图中。 然后画一个小节。

但是当图像很大时,这显然会耗尽记忆力。

我的屏幕是一个表面视图。 有帆布等

那么如何在给定的偏移处绘制部分图像并resize。 无需将原始内容加载到内存中

我找到了一个沿着正确的方向看的答案,但它无法正常工作。 使用文件中的drawables。 下面的代码尝试。 除了它产生的随机resize外,它也是不完整的。

例:

例

Drawable img = Drawable.createFromPath(Files.SDCARD + image.rasterName); int drawWidth = (int) (image.GetOSXWidth()/(maxX - minX)) * m_canvas.getWidth(); int drawHeight = (int)(image.GetOSYHeight()/(maxY - minY)) * m_canvas.getHeight(); // Calculate what part of image I need... img.setBounds(0, 0, drawWidth, drawHeight); // apply canvas matrix to move before draw...? img.draw(m_canvas); 

BitmapRegionDecoder可用于加载图像的指定区域。 这是一个在两个ImageView设置位图的示例方法。 第一个是完整图像,发送只是完整图像的一个区域:

 private void configureImageViews() { String path = externalDirectory() + File.separatorChar + "sushi_plate_tokyo_20091119.png"; ImageView fullImageView = (ImageView) findViewById(R.id.fullImageView); ImageView bitmapRegionImageView = (ImageView) findViewById(R.id.bitmapRegionImageView); Bitmap fullBitmap = null; Bitmap regionBitmap = null; try { BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder .newInstance(path, false); // Get the width and height of the full image int fullWidth = bitmapRegionDecoder.getWidth(); int fullHeight = bitmapRegionDecoder.getHeight(); // Get a bitmap of the entire image (full plate of sushi) Rect fullRect = new Rect(0, 0, fullWidth, fullHeight); fullBitmap = bitmapRegionDecoder.decodeRegion(fullRect, null); // Get a bitmap of a region only (eel only) Rect regionRect = new Rect(275, 545, 965, 1025); regionBitmap = bitmapRegionDecoder.decodeRegion(regionRect, null); } catch (IOException e) { // Handle IOException as appropriate e.printStackTrace(); } fullImageView.setImageBitmap(fullBitmap); bitmapRegionImageView.setImageBitmap(regionBitmap); } // Get the external storage directory public static String externalDirectory() { File file = Environment.getExternalStorageDirectory(); return file.getAbsolutePath(); } 

结果是完整图像(顶部),只是图像的一个区域(底部):

在此处输入图像描述