view.getDrawingCache()只能一次view、getDrawingCache

2023-09-13 23:40:42 作者:浪漫的hero.

我有一个用从语书架触摸V2例如加载的位图图像RelativeLayout的 - http://media.pragprog.com/titles/eband3/$c$c/Touchv2/src/org/example/touch/Touch.java

I have a RelativeLayout with a loaded bitmap image using the Touch V2 example from Pragmatic Bookshelf -- http://media.pragprog.com/titles/eband3/code/Touchv2/src/org/example/touch/Touch.java

我添加了一个单独的按钮,onclicklistener,当点击将加载图像从画廊。在活动结果的图像加载为位图到RelativeLayout的:

I've added a separate button with onclicklistener that when clicked will load an image from the gallery. On the activity result the image is loaded as a bitmap into the RelativeLayout:

    public void getPictureFromFile(Uri targetUri){
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = scale(getContentResolver()
                .openInputStream(targetUri));
        workinprogress = BitmapFactory.decodeStream(
                getContentResolver().openInputStream(targetUri),
                null, options);
        view.setImageBitmap(workinprogress);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

一单击Next按钮,我抢RelativeLayout的形象使用:

One the next button click, I grab the image of the relativelayout using:

                thepicture.buildDrawingCache(true);
            Bitmap bm = Bitmap.createBitmap(thepicture.getDrawingCache());

这个过程是了不起的 - 在第一张图像。当我再次加载另一幅图像,通过位图仍和原来一样。我试过thepicture.invalidate()和thepicture.resetDrawableState()getDrawingCache(前),但似乎都不更新形象的新加载的图片,虽然框架布局显示正确的图像。

The process works terrific -- for the first image. When I load another image again, the bitmap passed is still the same as the original. I've tried the thepicture.invalidate() and thepicture.resetDrawableState() before getDrawingCache() but neither seem to update the image to the newly loaded picture, although the frame layout displays the correct image.

有什么我不明白有关刷新drawingCache,我需要实现第二图像我加载?

Is there something I don't understand about refreshing drawingCache that I need to implement for the second image I load?

推荐答案

我不认为答案仍然是迫切需要你,但可能是其他人面临这样的问题,所以...

I don't think the answer is still urgent for you, but may be someone else faced such problem, so...

要使其工作更加那么,一旦你必须使用 view.setDrawingCacheEnabled(真)每次前后 view.setDrawingCacheEnabled(假)调用后每次 view.getDrawingCache()。见例如:

To make it work more then once you have to use view.setDrawingCacheEnabled(true) each time before and view.setDrawingCacheEnabled(false) each time after calling view.getDrawingCache(). See the example:

imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache(true);
File imageFile = new File(Environment.getExternalStorageDirectory(),
        "Pictures/image.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
imageView.getDrawingCache(true).compress(CompressFormat.JPEG, 100,
        fileOutputStream);
fileOutputStream.close();
imageView.setDrawingCacheEnabled(false);