0

注意:这里我不是在谈论通过 onSaveInstanceState(...) 保存活动状态

我正在使用https://github.com/eskim/android_drag_sample在我的应用程序中拖放视图的代码。用户可以在我的应用程序中创建多个场景,并且对于每个场景,用户可以添加多个视图并将它们拖放到他们想要的任何地方。在切换场景时,我将所有场景信息(例如添加的儿童数量、它们的属性等)存储在模型对象中,并且工作正常。用户可以保存场景并以 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;
}
4

1 回答 1

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;
}
于 2013-07-09T13:52:05.820 回答