0

I'm trying to save a view on a Canvas like this:

    View view = getView();

    Rect rect = new Rect();
    rect.set(0, 0, width, height);

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);

    view.layout(0, 0, rect.width(), rect.height());
    view.draw(canvas);

    imageView.setImageBitmap(bitmap);

The view is relatively complex and when showing the view on screen half of the layouts are missing. If I do not use a Canvas and ImageView, the view displays fine.

I have seen this question: Canvas not displaying all drawn parts in Custom View?, however I do not have Hardware Acceleration turned on (view.isHardwareAccelerated() returns false).

What can be the problem here? I've tried omitting some layouts from the view to make it less complex, but still the view doesn't display fully. It seems like it always stops displaying at the same point.

This is how to image looks like without using Canvas/Bitmap: How it should look like:

This is how it is displayed in the ImageView using Canvas/Bitmap: How it is displayed in the Bitmap saved to ImageView

Community
  • 1
  • 1
Christopher Masser
  • 809
  • 13
  • 25
  • What parts are missing? Kind of hard to tell from what you have posted. – zgc7009 Dec 15 '14 at 18:00
  • I've added the images. – Christopher Masser Dec 15 '14 at 18:06
  • Are you sure everything that is being displayed is being drawn on that canvas and not just what is saved? – zgc7009 Dec 15 '14 at 18:08
  • What do you mean by 'not just what is saved'? I'm calling view.draw(canvas); at the very end, everything is on the same thread. Is there some other way to 'save' views to a canvas? – Christopher Masser Dec 15 '14 at 18:18
  • What I am saying as are you sure that the blue views, the day, the month text, etc. are all being drawn on that views canvas, or is it possible that those views are being drawn on top of the canvas you are drawing on (and trying to save). – zgc7009 Dec 15 '14 at 18:22
  • Under what circumstances could views be drawn on top of the canvas that I am drawing on? Do you mean z-indexes? All the views on the right (Monday, 15, December, 2014) are added within the same function call. – Christopher Masser Dec 15 '14 at 18:27
  • Where are you adding/handling what is displayed vs. what isn't displayed? Is there a difference? You could overlap if you are using a RelativeLayout, FrameLayout, or SurfaceView to name a few. – zgc7009 Dec 15 '14 at 18:30
  • All the views on the right (Monday, 15, December, 2014) are added to a LinearLayout in the same way. The LinearLayout is added to a RelativeLayout. The RelativeLayout is part of another RelativeLayout. Can that be a problem? – Christopher Masser Dec 15 '14 at 18:39
  • wow, I just realized that the missing TextViews are having TextView.setSingleLine();. If I remove that from the code, the TextViews display. Why the blue boxes don't appear still doesn't make sense though. – Christopher Masser Dec 15 '14 at 18:50
  • It is possible that your RelativeLayout is drawing on top of the other RelativeLayout instead of onto it like you are expecting. I do find it weird that taking out the single line fixed the text issue though. – zgc7009 Dec 15 '14 at 18:55

0 Answers0