Java 将部分图像绘制到屏幕(无需将全部加载到内存)

Java 将部分图像绘制到屏幕(无需将全部加载到内存),java,android,image,Java,Android,Image,我想做的是,在我选择的位置,在屏幕上画一幅图像的剪影 我可以很容易地将其加载到位图中。然后画一个小节 但当图像较大时,这显然会耗尽内存 我的屏幕是表面视图。还有画布等等 那么我如何在给定偏移量下绘制图像的一部分并调整大小呢。不将原始区域加载到内存中 我找到了一个大致正确的答案,但它不能正常工作。使用文件中的可提取项。下面是代码尝试。除了随机调整大小之外,它也是不完整的 例如: Drawable img = Drawable.createFromPath(Files.SDCARD + image.

我想做的是,在我选择的位置,在屏幕上画一幅图像的剪影

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

但当图像较大时,这显然会耗尽内存

我的屏幕是表面视图。还有画布等等

那么我如何在给定偏移量下绘制图像的一部分并调整大小呢。不将原始区域加载到内存中

我找到了一个大致正确的答案,但它不能正常工作。使用文件中的可提取项。下面是代码尝试。除了随机调整大小之外,它也是不完整的

例如:

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);


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

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();
}
结果是完整图像(顶部)和图像的一个区域(底部):


看看
位图区域编码器
。我想这正是你想要的。@bobnoble谢谢你翻阅javadocs,看起来这样就可以了。这是一个非常直接的用法。把它写下来作为@bobnoble的答案——它很有用,并且将继续为其他人所用。