首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Android SurfaceView保存为位图,部分元素丢失

将Android SurfaceView保存为位图,部分元素丢失
EN

Stack Overflow用户
提问于 2012-05-24 18:14:18
回答 1查看 6.9K关注 0票数 1

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

代码语言:javascript
运行
复制
@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);

        }
    }
}

这是用来尝试保存画布的方法。

代码语言:javascript
运行
复制
    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运行良好,我在后台拍摄了我的相机,可以在顶部添加所有项目并移动它们。正当我试图截取屏幕截图时,上面的项目都不存在。

谢谢你的帮助!!

-更新--

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

代码语言:javascript
运行
复制
    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();
    }
}

我不明白为什么这不是在顶部绘制其他图像...

EN

Stack Overflow用户

回答已采纳

发布于 2012-05-24 18:33:21

在我的例子中,我使用的是:

代码语言:javascript
运行
复制
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;
}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10735448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档