2

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.

Community
  • 1
  • 1
Mahendran
  • 2,719
  • 5
  • 28
  • 50
  • You can ask me for clarification :) I think I am doing mistake in inflating view.... – Mahendran Jan 30 '12 at 09:59
  • Have you tried debugging width and height ? If I remember well, these two variables equals 0 when retrieved this way... – Sephy Jan 30 '12 at 10:00
  • @Sephy you are right Actually I printed the bitmap's height and width.height and width of secondView is 0,0. But if I mention viewgroup in inflating the view It will come forth in screen... How can I get bitmap with my requirements... – Mahendran Jan 30 '12 at 10:28

1 Answers1

2

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.

DeaMon1
  • 572
  • 5
  • 10