簡體   English   中英

如何在Android中將ScrollView內容轉換為Bitmap(image)

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

我正在開發一個具有ScrollView的應用程序。 我想要的是將ScrollView中的所有內容轉換為圖像Bitmap。 目前,我正在使用一段代碼來執行此操作,但問題是我只獲得了ScrollView的一部分,而不是全部內容。 我不明白為什么會這樣。 這是我的xml代碼:-

<?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>

在onCreate函數中,我正在像這樣繪制位圖:-

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);

但是每次我只得到整個Child布局的一部分時。 我想獲取所有內容,因為它在ImageView內部。 任何幫助將是可觀的。 謝謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM