【问题标题】:view.getDrawingCache() only works onceview.getDrawingCache() 只工作一次
【发布时间】:2012-06-11 23:42:39
【问题描述】:

我有一个使用来自 Pragmatic Bookshelf 的 Touch V2 示例加载的位图图像的 RelativeLayout -- http://media.pragprog.com/titles/eband3/code/Touchv2/src/org/example/touch/Touch.java

我添加了一个带有 onclicklistener 的单独按钮,单击该按钮时将从图库中加载图像。在活动结果中,图像作为位图加载到 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();
    }
}

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

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

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

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

【问题讨论】:

    标签: android view bitmap


    【解决方案1】:

    为了让它工作得更多,你必须在之前每次使用 view.setDrawingCacheEnabled(true) 和每次调用 view.getDrawingCache() 之后使用 view.setDrawingCacheEnabled(false)。看例子:

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

    【讨论】:

    猜你喜欢
    • 2017-01-06
    • 2018-06-22
    • 2015-09-18
    • 2013-12-31
    • 2013-09-28
    • 2013-03-30
    • 2015-06-04
    • 2019-05-18
    • 1970-01-01
    相关资源
    最近更新 更多