创建屏幕外布局的位图

发布于 2024-12-29 21:49:15 字数 1548 浏览 1 评论 0原文

我必须获取布局的位图(例如第二个屏幕)之前(可以说没有)将其带到前台屏幕。我引用了这篇文章。我在夸大视图的某个地方犯了错误。 注意:

1. Must not show the inflated screen (second screen)
2. Must not attach it to the parent view

我的代码片段在这里:

private Bitmap renderNextScreen() {
    
    // Getting width, height device
    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    
    Log.i(TAG, "Width- " + width+" Height - "+height);

    // Create a mutable bitmap
    Bitmap secondScreen = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
    // Created a canvas using the bitmap
    Canvas c = new Canvas(secondScreen);
    
    // Inflated the second screen
    LayoutInflater inflater = LayoutInflater.from(this);
    View secondView = inflater.inflate(R.layout.secondscreen, null);
    
    Log.i(TAG, "secondView.Height- "+secondView.getHeight()+" , secondView.Width " + secondView.getWidth());
    // Drawn the inflated view to canvas
    secondView.draw(c);
    
    preview.setImageBitmap(secondPage); // preview is the Imageview inside first screen
    return secondScreen;
}

我的高度、宽度日志:

01-28 02:12:35.926: I/FirstActivity(21831): Width- 480 Height - 800
01-28 02:30:08.287: I/FirstActivity(22207): secondView.Height- 0 , secondView.Width 0

我的预览始终为空白。谢谢。

I have to get a bitmap of a layout(say second screen) Before (can say without) bring it to the foreground screen. I referred this post. I am doing mistake somewhere in inflating the view.
Note:

1. Must not show the inflated screen (second screen)
2. Must not attach it to the parent view

My code snippet is here:

private Bitmap renderNextScreen() {
    
    // Getting width, height device
    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    
    Log.i(TAG, "Width- " + width+" Height - "+height);

    // Create a mutable bitmap
    Bitmap secondScreen = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
    // Created a canvas using the bitmap
    Canvas c = new Canvas(secondScreen);
    
    // Inflated the second screen
    LayoutInflater inflater = LayoutInflater.from(this);
    View secondView = inflater.inflate(R.layout.secondscreen, null);
    
    Log.i(TAG, "secondView.Height- "+secondView.getHeight()+" , secondView.Width " + secondView.getWidth());
    // Drawn the inflated view to canvas
    secondView.draw(c);
    
    preview.setImageBitmap(secondPage); // preview is the Imageview inside first screen
    return secondScreen;
}

My Log for height, width:

01-28 02:12:35.926: I/FirstActivity(21831): Width- 480 Height - 800
01-28 02:30:08.287: I/FirstActivity(22207): secondView.Height- 0 , secondView.Width 0

My preview was always blank. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿嘿嘿 2025-01-05 21:49:15

我认为将:更改

View secondView = inflater.inflate(R.layout.secondscreen, null);

为:

View secondView = inflater.inflate(R.layout.secondscreen, null, false);

然后您将需要为膨胀视图声明布局参数,例如:

secondView.setLayoutParams(new LayoutParams(displaymetrics.widthPixels, 
displaymetrics.heightPixels/3));

Displaymetrics 是一个保存手机屏幕尺寸的变量。

然后你应该能够将该视图转换为 bmp。

I think that changing:

View secondView = inflater.inflate(R.layout.secondscreen, null);

to:

View secondView = inflater.inflate(R.layout.secondscreen, null, false);

you will then need to declare layout params for the inflated view, somthing like:

secondView.setLayoutParams(new LayoutParams(displaymetrics.widthPixels, 
displaymetrics.heightPixels/3));

Displaymetrics was a variable that held the dimension of the phones screen.

Then you should be able to turn that view into a bmp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文