gpt4 book ai didi

android - getDrawingCache 始终返回相同的位图

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:49 25 4
gpt4 key购买 nike

我目前正在处理一个项目,该项目需要显示一个带有灰色(黑色/白色)背景的对话框。为此,我截取了整个应用程序的屏幕截图,将此屏幕截图放在全屏对话框的背景上,并在其上放置一个 ColorFilter 使其变灰。

这在第一次使用时非常完美,但如果我在底层内容中滚动并再次请求对话框,它会显示与之前相同的背景。

我使用代码:

Bitmap bitmap;
View rootView = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
rootView.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
imageView.setImageBitmap(bitmap);

换句话说,getDrawingCache() 始终返回应用程序的相同屏幕截图。

最佳答案

我认为那是因为您的旧位图仍在绘图缓存中。正因为如此,首先需要将其从缓存中删除,然后将新的Image放入缓存中。看看这个问题,似乎是同一个话题:

Deletion of drawing cache

编辑:所以,这是为我工作的代码。我使用按钮保存位图,然后将位图设置为 ImageView :

private View rootView;
private ImageView bitmapView;
private Button switchButton;
public Bitmap capturedScreen;
public boolean bitmapNeeded = false;

...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// do the other stuff
rootView.setDrawingCacheEnabled(true); //enable Drawing cache on your view
switchButton.setOnClickListener(this);
}

...

@Override
public void onClick(View v) {
if (v == switchButton) { //when the button is clicked
captureScreen();
}
}

public void captureScreen() {
rootView.buildDrawingCache();
capturedScreen = Bitmap.createBitmap(rootView.getDrawingCache());
imageView.setImageBitmap(capturedScreen);
rootView.destroyDrawingCache();
}

....

//In the onDraw method of your View:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(capturedScreen, 0, 0, paint);
}

它是这样工作的:每次用户单击按钮时,rootView 中的所有内容都会保存为位图,然后绘制到 imageView 中。如果需要,您当然可以从代码中的任何位置调用 captureScreen 方法。

希望这个例子对你有帮助。

关于android - getDrawingCache 始终返回相同的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610699/

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