2

I want to convert my layout that contains images,text to bitmap to implement pagecurl effect in my app..

My xml is

details.xml

 <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background= "#E0FFFF"
    android:gravity="center"
    android:orientation="vertical" >
    <FrameLayout 
       android:id="@+id/framelayout"
       android:layout_marginTop="30dip"
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent">
      <RelativeLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/Rl">
                <TextView
                  android:id="@+id/tvName"
                  android:layout_width="wrap_content"
                  android:layout_height="60dip"
                  android:layout_alignParentTop="true"
                  android:layout_centerHorizontal="true"
                  android:text="BookName"
                  android:textSize="40dip"
                  android:textStyle="bold" />
               <TextView
                  android:id="@+id/tTitle"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/tvName"
                  android:text="Page Name"
                  android:textColor="#040404"
                  android:textSize="30dip"
                  android:textStyle="bold"
                  android:typeface="sans" />
              <ImageView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignWithParentIfMissing="false"
                  android:layout_below="@id/tTitle"
                  android:baselineAlignBottom="true"
                  android:scaleType="fitXY"
                  android:src="@drawable/stub"
                  android:id="@+id/smallImage"
                />
          <TextView
               android:id="@+id/tDescription"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:textSize="15dip"
               android:layout_below="@id/smallImage" />
           <Button
               android:id="@+id/buy_button"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignBottom="@+id/tTitle"
               android:layout_alignParentRight="true"
               android:layout_alignParentTop="true"
               android:layout_marginRight="22dp"
               android:text="buy"
               android:visibility="visible"/>
           <Button
               android:id="@+id/bLib"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentLeft="true"
               android:layout_below="@+id/tDescription"
               android:text="Go to Library"
               android:textSize="20dip"
               android:textStyle="italic" 
              />
               <Button
               android:id="@+id/backD"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_toRightOf="@+id/bLib"
                 android:layout_alignParentRight="true"
                android:layout_below="@id/tDescription"
               android:text="Back to Home" 
               android:textStyle="bold"
               android:textSize="20dip"
               />
    </RelativeLayout>
    </FrameLayout>
    </ScrollView>

and my java class to convert this layout to bitmap is

    public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.details);
      FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);
      view.setDrawingCacheEnabled(true);
      view.buildDrawingCache();
      Bitmap bm = view.getDrawingCache();
      System.err.println("getting bitmap.."+bm);
    }

I am not finding the way to set the layout values here..Here i am getting bitmap bm value as null.Could anyone suggest me how to convert that layout in bitmap and set those values to bitmap?

2
  • If i am not wrong "DrawingCache" will be available only after displaying the view on screen. so if you do that in onCreate by that time view will not be displayed on the screen. so try the same in onResume(); Commented Jan 11, 2013 at 5:47
  • @Raj using onResume() didnot gave me soultion bro Commented Jan 11, 2013 at 5:52

2 Answers 2

1

to overcome with this problem I destroy the drawingcache after saving the bitmap.

view.destroyDrawingCache();

and save your bitmap as per below code.

view.measure(100, 100);
view.layout(0, 0, 100, 100);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
Sign up to request clarification or add additional context in comments.

3 Comments

oops!! getting null pointer exception at Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
You need to call measure and layout on your view before using getDrawingCache(). Otherwise it will return null.
0

Check if the it is going out of memory while getting the bitmap.

Put

try   
{....}
catch(OutOfMemoryException ex)
{}

Another problem can be this: Your view is not drawn when you are taking the bitmap. Try using button onclick function to do the same procedure.

1 Comment

You are taking the bitmap before the screen is drawn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.