我尝试使用以下代码将LinearLayout
转换为图像:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lyt = (LinearLayout) findViewById(R.id.lyt);
lyt.setDrawingCacheEnabled(true);
lyt.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(lyt.getDrawingCache());
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(b);
}
但是我在以下位置遇到了NullPointerException
:
Bitmap b = Bitmap.createBitmap(lyt.getDrawingCache());
布局 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" >
<LinearLayout
android:id="@+id/lyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>