かなり長い間、私の問題を解決しました。アイデアは、モデルオブジェクトにあるデータを任意のレイアウトに追加し、そこから画像を取得することです。以下はコードです。
/** 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;
}