2

I a developing an application in which I have a ScrollView. What I want is to convert all of the content inside ScrollView into image Bitmap. Currently I am doing this using a piece of code but the problem is that I am getting only a part of the ScrollView not whole content. I dont understand why is this happening. Here is my xml code :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/draw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw"
        android:textSize="20sp"/>

    <ScrollView
        android:id="@+id/mScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp" >

        <LinearLayout
            android:id="@+id/mRelLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button
                android:id="@+id/mBtn1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Btn1"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn2"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn3"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn4"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn5"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn6"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn7"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn8"
                android:textSize="20sp" />

            <Button
                android:id="@+id/mBtn9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn9"
                android:textSize="20sp" />

             <Button
                android:id="@+id/mBtn10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn10"
                android:textSize="20sp" />

              <Button
                android:id="@+id/mBtn11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Btn11"
                android:textSize="20sp" />

            <ImageView 
                android:id="@+id/finalImage"
                android:layout_width="150dp"
                android:layout_height="150dp"/>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

and inside onCreate function I am drawing the Bitmap like this :-

View u = ((Activity) mContext).findViewById(R.id.mScrollView);

    ScrollView z = (ScrollView) ((Activity) mContext).findViewById(R.id.mScrollView);
    int totalHeight = z.getChildAt(0).getHeight();
    int totalWidth = z.getChildAt(0).getWidth();

    Bitmap b = getBitmapFromView(u,totalHeight,totalWidth);  
    Toast.makeText(mContext, "Bitmap "+b.toString(), 0).show();

    finalImage.setImageBitmap(b);

But everytime I am getting only some part of the whole Child layout. I want to get all content as it is inside the ImageView. Any help would be appreciable. Thanks.

Salman Khan
  • 2,822
  • 5
  • 32
  • 53
  • please see this: http://stackoverflow.com/questions/8738448/how-to-convert-all-content-in-a-scrollview-to-a-bitmap?rq=1 – abbasalim Apr 03 '16 at 08:05

1 Answers1

0
 public Bitmap getBitmapFromView(View view){
      view.setDrawingCacheEnabled(true);
      view.refreshDrawableState();
      Bitmap bitmap = view.getDrawingCache();
      view.setDrawingCacheEnabled(false);
      return bitmap;
 }
Dmitriy Puchkov
  • 1,530
  • 17
  • 41