2013-04-05 2 views
0

참고 : 여기에 내가 끌어 내 응용 프로그램에서보기를 드롭 https://github.com/eskim/android_drag_sample 코드를 사용하고저장 뷰 그룹 상태 : 안드로이드

onSaveInstanceState (...)를 통해 활동의 상태를 저장에 대해서 이야기하고 있지 않다. 사용자는 내 응용 프로그램과 각 장면에 여러 장면을 만들 수 있으므로 사용자는 원하는대로 여러보기를 추가하고 끌어서 놓을 수 있습니다. 장면을 전환하는 동안 모델 객체에 추가 된 자식 수, 속성 등의 모든 장면 정보를 저장하고 작동합니다. 사용자는 장면을 저장하고 pdf로 메일을 통해 보낼 수 있습니다. 문제는 PDF 파일을 준비하려면 이미지가 필요합니다 (이미지를 pdf로 변환하는 iText 라이브러리를 사용하고 있습니다).하지만 이미지에 다른 숨겨진 장면 (보이지 않음)을 어떻게 변환 할 수 있습니까? 현재 보이는 장면 나는 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; 
} 

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; 
     } 
    } 
} 

로드 맵 뷰에서 이미지로 SD 카드로도 저장

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; 
}