繁体   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