2012-04-29 61 views
7

我嘗試下面的代碼到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> 

回答

8

嘗試下面的代碼,它的工作原理

添加下面的權限清單中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{ 
private LinearLayout linearLayout; 
private Button saveBtn; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    linearLayout = (LinearLayout) findViewById(R.id.linearLayout_view); 
    saveBtn = (Button) findViewById(R.id.save_btn); 
    saveBtn.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    int id = v.getId(); 
    switch (id) { 
    case R.id.save_btn: 
     File file = saveBitMap(this, linearLayout); //which view you want to pass that view as parameter 
     if (file != null) { 
      Log.i("TAG", "Drawing saved to the gallery!"); 
     } else { 
      Log.i("TAG", "Oops! Image could not be saved."); 
     } 
     break; 
    default: 
     break; 
    } 

private File saveBitMap(Context context, View drawView){ 
    File pictureFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Handcare"); 
    if (!pictureFileDir.exists()) { 
     boolean isDirectoryCreated = pictureFileDir.mkdirs(); 
     if(!isDirectoryCreated) 
      Log.i("ATG", "Can't create directory to save the image"); 
     return null; 
    } 
    String filename = pictureFileDir.getPath() +File.separator+ System.currentTimeMillis()+".jpg"; 
    File pictureFile = new File(filename); 
    Bitmap bitmap =getBitmapFromView(drawView); 
    try { 
     pictureFile.createNewFile(); 
     FileOutputStream oStream = new FileOutputStream(pictureFile); 
     bitmap.compress(CompressFormat.PNG, 100, oStream); 
     oStream.flush(); 
     oStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.i("TAG", "There was an issue saving the image."); 
    }  
     scanGallery(context,pictureFile.getAbsolutePath()); 
    return pictureFile; 
} 
//create bitmap from view and returns it 
private Bitmap getBitmapFromView(View view) { 
    //Define a bitmap with the same size as the view 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); 
    //Bind a canvas to it 
    Canvas canvas = new Canvas(returnedBitmap); 
    //Get the view's background 
    Drawable bgDrawable =view.getBackground(); 
    if (bgDrawable!=null) { 
     //has background drawable, then draw it on the canvas 
     bgDrawable.draw(canvas); 
    } else{ 
     //does not have background drawable, then draw white background on the canvas 
     canvas.drawColor(Color.WHITE); 
    } 
    // draw the view on the canvas 
    view.draw(canvas); 
    //return the bitmap 
    return returnedBitmap; 
} 
// used for scanning gallery 
private void scanGallery(Context cntx, String path) { 
    try { 
     MediaScannerConnection.scanFile(cntx, new String[] { path },null, new MediaScannerConnection.OnScanCompletedListener() { 
      public void onScanCompleted(String path, Uri uri) { 
      } 
     }); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

+1

謝謝:)你的回答顯示了我設置背景的重要提示,它像魅力一樣起作用。 –

0
Bitmap b = Bitmap.createBitmap(lyt.getDrawingCache()); 

唯一的可以是那行3210是lyt,其餘的不行。您可能尚未設置佈局,在這種情況下,findViewById()將返回null

你所要做的setContentView()之前,你可以做findViewById()

+0

不,我已經做到了 – Adham

+0

嗯,這應該意味着錯誤並不在該行發生,但在'Bitmap.createBitmap()'或'lyt.getDrawingCache()'裏面。我想雷納德是對的,如果觀點沒有佈置自己,他們不知道如何畫自己。 – zapl

+0

得到了倒像。 –

2

我的猜測是,你正在執行中的onCreate代碼。這個問題的問題是,這些意見還沒有完善。要麼調用lyt.measure,要麼稍後調用代碼。例如:在調用super.onLayout()之後調用onSizeChanged()或onLayout()。

+0

如何調用lyt.onMeasure()??! – Adham

+0

對不起,函數是measure()。但我建議稍後創建位圖並且不要調用measure()。 – Renard

0

下面是一小段代碼片段其XML佈局轉換爲位圖圖像

private Bitmap convertLayoutToImage() { 
     LinearLayout linearView = (LinearLayout) this.getLayoutInflater(null).inflate(R.layout 
       .marker_layout, null, false); //you can pass your xml layout 

     linearView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
       View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
     linearView.layout(0, 0, linearView.getMeasuredWidth(), linearView.getMeasuredHeight()); 

     linearView.setDrawingCacheEnabled(true); 
     linearView.buildDrawingCache(); 
     return linearView.getDrawingCache();// creates bitmap and returns the same 
    }