getDrawingCache总是返回相同的位图 [英] getDrawingCache always returns the same Bitmap

查看:1143
本文介绍了getDrawingCache总是返回相同的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上需要显示与灰朦(黑/白)背景对话框的项目。
要做到这一点,我采取了屏幕截图和整个应用程序,放置在全屏对话的背景这张截图,把一个ColorFilter它有它变灰。

I'm currently working on a project which needs to display a dialog with a grayout (black/white) background. To achieve this I'm taking a screenshot and of the whole app, place this screenshot on the background of the fullscreen dialog and put an ColorFilter on it to have it grayed out.

这完美的作品是第一次,但如果我在垫层内容滚动,并再次请求对话框,它显示一样的背景和以前一样的人。

This works perfect for the first time, but if I scroll in the underlaying content and request the dialog again, it shows just the same background as the one before.

我用code:

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()总是返回该应用的相同的屏幕截图。

In other words, the getDrawingCache() always returns the same screenshot of the app.

推荐答案

我想这是因为旧的位图仍然在您的绘图缓存。正因为如此,你首先需要从缓存中删除它,然后把新的图像在缓存中。看看这个问题,这似乎是关于同一主题:

I think that's because your old Bitmap is still in your drawing cache. Because of this, you first need to delete it from the cache and then put the new Image in the cache. Take a look at this question, which seems to be on the same topic:

绘图缓存缺失

编辑:
所以,这里是code这是为我工作。我用一个按钮来保存位图,然后将位图图像查看:

So, here is the code which is working for me. I use a Button to save the Bitmap and then set the Bitmap to an Image View:

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

所以,我希望我谈过了很重要的。这是怎样的code工作:
每当用户点击按钮,里面的 rootView 一切都被保存为一个位图,然后绘制到的ImageView
课的,你可以从你的code的任何地方拨打captureScreen方法。

So, I hope I covered everything important. This is how the code works: Everytime the user clicks the Button, everything inside rootView is saved as a bitmap and then drawn to the imageView. You can of coure call the captureScreen Method from anywhere in your code.

我希望这个例子可以帮助你。

I hope this example helps you.

这篇关于getDrawingCache总是返回相同的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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