将Android SurfaceView保存到位图并且缺少某些元素

时间:2012-05-24 10:14:18

标签: android canvas bitmap surfaceview

我的SurfaceView启动并运行了一个按钮来打开相机并拍摄一张用作背景的照片和另一个按钮来添加位于顶部且可以移动的项目。这一切都正常,直到我尝试将SurfaceView保存为位图时,我得到的只是背景而没有顶部的图像。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(_mGotImage){

        canvas.drawBitmap(_mImage, 0, 0, null);
    }else{

            canvas.drawColor(Color.BLACK);
    }

    //if the array is not empty
    if(!_mJazzItems.isEmpty()){

        //step through each item in the array
        for(JazzItem item: _mJazzItems){

            //get the bitmap it is using
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item.getBitmap());
            //and draw that bitmap at its X and Y coords
            canvas.drawBitmap(bitmap, item.getX(), item.getY(), null);

        }
    }
}

这是尝试并保存Canvas的方法。

    public void screenGrab(){

    Bitmap image = Bitmap.createBitmap(_mPanelWidth, _mPanelHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);
    this.onDraw(canvas);

    String path=Environment.getExternalStorageDirectory() + "/test2.png";
    File file = new File(path);

    try{

        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        image.compress(CompressFormat.PNG, 100, ostream);
        ostream.flush();
        ostream.close();

    }catch (Exception e){

        e.printStackTrace();
    }
}

onDraw工作正常,我在后台拍摄相机,可以将所有项目添加到顶部并移动它们。就在我尝试拍摄屏幕截图时,顶部的物品都没有出现。

感谢您的帮助!!

- 更新 -

我已经将屏幕抓取方法修改为:

    public void screenGrab(){

    Bitmap image = Bitmap.createBitmap(_mPanelWidth, _mPanelHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);

    canvas.drawBitmap(_mImage, 0, 0, null);

    //if the array is not empty
    if(!_mJazzItems.isEmpty()){

        //step through each item in the array
        for(JazzItem item: _mJazzItems){

            //get the bitmap it is using
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item.getBitmap());
            //and draw that bitmap at its X and Y coords
            canvas.drawBitmap(bitmap, item.getX(), item.getY(), null);

        }
    }

    String path=Environment.getExternalStorageDirectory() + "/test2.png";
    File file = new File(path);

    try{

        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        image.compress(CompressFormat.PNG, 100, ostream);
        ostream.flush();
        ostream.close();

    }catch (Exception e){

        e.printStackTrace();
    }
}

我无法理解为什么这不会将其他图像吸引到顶部......

1 个答案:

答案 0 :(得分:1)

就我而言,我正在使用它:

public static Bitmap combineImages(Bitmap c, Bitmap overLayImage, Context con) {
    Bitmap cs = null;
    int width, height = 0;
    width     = c.getWidth();
    height     = c.getHeight();
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(cs);
    comboImage.drawBitmap(c, 0, 0, null);
    String left = yourleftPosition;
    String top = yourtopPosition;

    comboImage.drawBitmap(overLayImage, Float.parseFloat(left), Float.parseFloat(top),null);
    /******
    *
    * Write file to SDCard
    *
    * ****/
    String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
    OutputStream os = null;
    try {
        String pathis = Environment.getExternalStorageDirectory()
        + "/DCIM/Camera/" + tmpImg;
        os = new FileOutputStream(pathis);
        cs.compress(CompressFormat.PNG, 100, os);
    }
    catch (IOException e) {
        Log.e("combineImages", "problem combining images", e);
    }
    return cs;
}