简体   繁体   中英

How to convert ScrollView content into Bitmap(image) in android

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.

 public Bitmap getBitmapFromView(View view){
      view.setDrawingCacheEnabled(true);
      view.refreshDrawableState();
      Bitmap bitmap = view.getDrawingCache();
      view.setDrawingCacheEnabled(false);
      return bitmap;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM