gpt4 book ai didi

android - view.getDrawingCache() 只工作一次

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:37 27 4
gpt4 key购买 nike

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

我添加了一个带有 onclicklistener 的单独按钮,单击该按钮将从图库中加载图像。在 Activity 结果中,图像作为位图加载到 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,我有什么不明白的地方吗?

最佳答案

要使其多次工作,您必须使用 view.setDrawingCacheEnabled(true)每次之前和view.setDrawingCacheEnabled(false)每次打电话后view.getDrawingCache() .看例子:

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

关于android - view.getDrawingCache() 只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10988847/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com