我嘗試下面的代碼到LinearLayout
轉換爲圖像:如何將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>
謝謝:)你的回答顯示了我設置背景的重要提示,它像魅力一樣起作用。 –