How to convert Views to bitmaps?
Asked Answered
X

7

36

I have two Views (Textview & ImageView) in the FrameLayout, I want to save the image with text. For this, I covert the View to a bitmap.

My xml is:

<FrameLayout 
     android:id="@+id/framelayout"
     android:layout_marginTop="30dip"
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent">

     <ImageView 
          android:id="@+id/ImageView01"
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content"/>

    <TextView android:id="@+id/text_view"
          android:layout_marginTop="30dip"
          android:layout_width="wrap_content" 
          android:maxLines="20"
          android:scrollbars="vertical"
          android:layout_height="wrap_content"/>

</FrameLayout>
Xanthochroid answered 26/8, 2011 at 5:56 Comment(0)
A
86

How to convert View into Bitmap

FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bm = view.getDrawingCache();
Antebellum answered 26/8, 2011 at 6:0 Comment(6)
@CapDroid and if i have to get bitmap with transparent background?Unclog
can we do the exact vice-versa of it, i mean can we convert bitmap again into view..???Palua
you can set bitmap on imageview or background of any view.Antebellum
Worth noting that this bitmap can be disposed anytime, so it is safer to copy bitmap into your own, Source: arpitonline.com/2012/07/17/…Drome
@NiranjPatel I tried above code but it returns the bitamap with as screen sizeStudio
Previously, getDrawingCache() returned null. Then I found this answer: https://mcmap.net/q/153302/-android-view-getdrawingcache-returns-null-only-nullDevelopment
L
24

I used to use the buildDrawingCache() method to get a bitmap of my layout, but I was having trouble with it when the view was large. Now I use the following method:

FrameLayout view = findViewById(R.id.framelayout);
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
Lazes answered 30/6, 2015 at 6:11 Comment(5)
Thank you!!! I was searching the whole week to find a solution to my problem with large view and this solution worked perfectly!Atiana
Thank youuuu.Its worked for large views to bitmap convertion.Subtonic
java.lang.IllegalArgumentException: width and height must be > 0Pavilion
@V.Y. This answer assumes that the view has already been laid out. If it hasn't then you will need to create it programmatically (rather than with findViewById).Lazes
what if you are not adding the view to activity? just convert it by reference layout idSillimanite
I
3

Hi you can get a bitmap of a view using the following snippet

mView.setDrawingCacheEnabled(true);
mView.getDrawingCache();
Intercessor answered 26/8, 2011 at 6:0 Comment(0)
S
1

why don't you write your class that extends ImageView and override method onDraw and put there your image and text, it's more easy

Saleratus answered 26/8, 2011 at 6:3 Comment(1)
i tried this but i am getting same image without text.int bw = originalBitmap.getWidth(); int bh = originalBitmap.getHeight(); nb = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);c = new Canvas(nb);Paint paint = new Paint() c.drawText(my_text, 0, 0, paint);Xanthochroid
D
0

First Of all, you need to add dependency

implementation 'com.github.vipulasri.layouttoimage:library:1.0.0'

Then Convert Layout to Bitmap

RelativeLayout pdfmain;
Layout_to_Image layout_to_image;
Bitmap mBitmap;



    layout_to_image = new Layout_to_Image(AllotmentDoc.this, pdfmain);
    mBitmap = layout_to_image.convert_layout();
Drinkable answered 4/2, 2021 at 7:20 Comment(0)
P
0
    FrameLayout v = (FrameLayout)findViewById(R.id.frme1);

    v.setDrawingCacheEnabled(true);

    // this is the important code :)
  // Without it the view will have a dimension of 0,0 and the bitmap will be null
    v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);

    v.post(new Runnable() {
        @Override
        public void run() {
           // Bitmap b = v.getDrawingCache();
            Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
            v.setDrawingCacheEnabled(false); // clear drawing cache
            Log.e("ss","ss"+b.getHeight());
        }
    });

Here I have added a post Runnable thread which ensure the createBitmap method will execute only after v.buildDrawingCache(true);. v.buildDrawingCache(true); takes few milisec time in some mobile and that is the reason it crash in some mobile. Please try this solution if you face null pointer exception for Bitmap object.

Pomerania answered 3/6, 2021 at 11:14 Comment(0)
V
0
public static Bitmap loadBitmapFromView(View view) {
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    final Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}
Voltaic answered 20/12, 2023 at 8:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.