我正在开发一个我有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函数里面我正在绘制这样的Bitmap: -
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中的所有内容。任何帮助都会很明显。 谢谢。
答案 0 :(得分:0)
public Bitmap getBitmapFromView(View view){
view.setDrawingCacheEnabled(true);
view.refreshDrawableState();
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
return bitmap;
}