创建屏幕外布局的位图

时间:2012-01-30 09:52:35

标签: android bitmap layout-inflater

我必须获得布局的位图(比如第二个屏幕)之前(可以说没有)将它带到前台屏幕。我提到this帖子。在给视图膨胀的某个地方我犯了错误。 注意:

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

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

1 个答案:

答案 0 :(得分:2)

我认为改变了:

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。