view.getDrawingCache() 只工作一次 [英] view.getDrawingCache() only works once

查看:13
本文介绍了view.getDrawingCache() 只工作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Pragmatic Bookshelf 的 Touch V2 示例加载了一个带有加载位图图像的 RelativeLayout -- http://media.pragprog.com/titles/eband3/code/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();
    }
}

单击下一个按钮,我使用以下方法获取相对布局的图像:

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

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

这个过程非常棒——对于第一张图片.当我再次加载另一个图像时,传递的位图仍然与原始图像相同.我在 getDrawingCache() 之前尝试了 thepicture.invalidate() 和 thepicture.resetDrawableState() 但似乎都没有将图像更新为新加载的图片,尽管框架布局显示了正确的图像.

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.

关于刷新我需要为加载的第二张图片实现的 drawCache 有什么我不明白的地方吗?

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

推荐答案

为了让它更有效,你必须在每次之前使用 view.setDrawingCacheEnabled(true)view.setDrawingCacheEnabled(false) 每次调用 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);

这篇关于view.getDrawingCache() 只工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆