2013-04-05 45 views
0

注:在這裏,我不是在談論保存的ViewGroup狀態:安卓

我使用https://github.com/eskim/android_drag_sample代碼拖動和我的應用程序刪除視圖的onSaveInstanceState通過(...)節約活動的狀態。用戶可以在我的應用程序和每個場景中創建多個場景,用戶可以添加多個視圖並將它們拖放到任何他們想要的位置。在切換場景時,我將所有場景信息(如添加的兒童數量,屬性等)存儲在模型對象中,並且其工作正常。用戶可以保存場景並通過郵件將其作爲pdf發送。問題是,準備pdf我需要圖像(我正在使用iText庫將圖像轉換爲pdf),但是如何將其他隱藏場景(不可見)轉換爲圖像?目前可見的場景我可以轉換成圖像並存儲在SD卡中。我試圖保存模型對象中的當前場景,並在準備PDF時將其轉換爲圖像,但將所有場景改寫爲當前場景。我不知道這種方法是否正確。所以,任何幫助或線索非常感謝。

代碼視圖轉換成位圖:

public static Bitmap getBitmapFromView(final View view) { 
    if(view.getWidth() <= 0 && view.getHeight() <= 0) 
     return null; 

    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); 
    final Canvas canvas = new Canvas(returnedBitmap); 

    Drawable bgDrawable = view.getBackground(); 
    if (bgDrawable != null) 
     bgDrawable.draw(canvas); 
    else 
     canvas.drawColor(Color.WHITE); 

    ((Activity) view.getContext()).runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      view.draw(canvas); 
     } 
    }); 

    return returnedBitmap; 
} 

回答

0

經過相當長的時間,得到了我的問題的解決方案。想法是模型對象中的任何數據,將其添加到任何佈局並從中獲取圖像。以下是代碼。

/** convert MySTScene(model object) into a View */ 
public static View getView(Context context, MySTScene mySTScene) { 
    RelativeLayout sceneView = new RelativeLayout(context); 
    RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(Constants.PLAY_AREA_WIDTH_TAG, 
      LayoutParams.WRAP_CONTENT); 
    parms.setMargins(100, 100, 100, 100); 

    String sceneBackground = mySTScene.getThemeImageName(); 
    Drawable drawable = BitmapConverter.getDrawable(context, sceneBackground); 

    sceneView.setBackgroundDrawable(drawable); 

    for(MySTCharacter mySTCharacter : mySTScene.getCharacterList()) { 
     int scale = mySTCharacter.getScale(); 
     ImageView image = new ImageView(context); 
     String filePath = mySTCharacter.getCharacterName(); 
     int xPosition = (int) mySTCharacter.getCharacterPoint().x + 141; 
     if(xPosition >= Constants.PLAY_AREA_WIDTH_TAG) 
      xPosition -= 400; 
     else if(xPosition > (Constants.PLAY_AREA_WIDTH_TAG/2)) 
      xPosition -= 300; 
     else 
      xPosition -= 200; 
     int yPosition = (int) mySTCharacter.getCharacterPoint().y; 
     if(!mySTCharacter.getIsDialog()) 
      sceneView.addView(setImageProperties(image, scale, filePath, 
        xPosition, yPosition, 0, 0)); 
     else { 
      addDialog(mySTCharacter, sceneView, filePath, 
        xPosition, yPosition); 
     } 
    } 

    // add writing pad only if some text is added to it while creating/saving scene 
    boolean isTrue = mySTScene.getStoryText() != null && mySTScene.getStoryText().length() != 0; 
    if(isTrue) 
     addWritingPad(mySTScene, sceneView); 

    sceneView.setLayoutParams(parms); 

    return sceneView; 
} 

到SD卡保存視圖圖像從視圖

public static void saveViewToSD(getView(context, mySTScene)) { 

    File fullSaveDir = FileUtils.createDirectory(FileUtils.PDF_IMAGES_DIRECTORY_TAG); 

    File file = new File(fullSaveDir, ""+System.currentTimeMillis()+".PNG"); 

    Bitmap bitmap = null; 

    try { 
     if(file != null && !file.exists()) 
      file.createNewFile(); 

     FileOutputStream fileOutputStream = new FileOutputStream(file); 

     bitmap = loadBitmapFromView(view); 


     if(bitmap == null) { 
      fileOutputStream.close(); 
      return; 
     } 

     bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream); 
     fileOutputStream.flush(); 
     fileOutputStream.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if(bitmap != null && !bitmap.isRecycled()) { 

      bitmap.recycle(); 
      bitmap = null; 
     } 
    } 
} 

負載位圖

public static Bitmap loadBitmapFromView(View view) throws IllegalArgumentException { 

    if (view.getMeasuredHeight() <= 0) 
     view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    Bitmap bitmap = Bitmap.createBitmap(Constants.PLAY_AREA_WIDTH_TAG, Constants.PLAY_AREA_HEIGHT_TAG, 
      Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 

    Drawable bgDrawable = view.getBackground(); 

    Bitmap bitmapbg = ((BitmapDrawable)bgDrawable).getBitmap(); 
    canvas.drawBitmap(bitmapbg, view.getLeft(), view.getTop(), null); 

    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom()); 
    view.draw(canvas); 

    return bitmap; 
}