2012-01-30 82 views
2

我必须得到一个布局的位图(如第二个屏幕)之前(可以说没有)将它带到前台屏幕。我参考了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 

我的预览总是空白。谢谢。

+0

你可以问我澄清:)我认为我在做错误的充气视图.... – 2012-01-30 09:59:27

+0

你试过调试宽度和高度?如果我没记错,这两个变量等于0时,这种方式检索... – Sephy 2012-01-30 10:00:22

+0

@Sephy你是对的其实我打印位图的高度和宽度。高度和secondView的宽度是0,0。但如果我提到视图组在扩大视图它会出现在屏幕上...我如何获得我的要求的位图... – 2012-01-30 10:28:16

回答

2

我认为改变:

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

到:

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

,那么你将需要声明布局params用于在膨胀的观点,财产以后这样的:

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

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

然后,你应该能够将该视图变成一个bmp。

+1

http://stackoverflow.com/a/4348228/199364及其评论也可能是有用的。 – ToolmakerSteve 2016-12-30 22:59:51