【问题标题】:Make image as canvas background and save it with user's drawing将图像作为画布背景并与用户的绘图一起保存
【发布时间】:2011-10-07 14:45:45
【问题描述】:

我正在使用下面的代码。我的应用能够在画布上绘图并保存。

但我想做的是制作一个图像作为画布的背景,所以当我保存它时,它看起来就像一个图像,上面有用户的绘图。

非常感谢您的帮助! :)

@Override
public void run() {
    Canvas canvas = null;
    while (_run){
        if(isDrawing == true){
            try{
                canvas = mSurfaceHolder.lockCanvas(null);
                if(mBitmap == null){
                    mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                }
                final Canvas c = new Canvas (mBitmap);

                c.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0xffffffff);

                commandManager.executeAll(c,previewDoneHandler);
                previewPath.draw(c);

                canvas.drawBitmap (mBitmap, 0,  0,null);
            } finally {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }


        }

    }

}

【问题讨论】:

  • 你的意思是要合并两张图片..?
  • 感谢您的回复@Lalit Poptani,是的,类似的。
  • 回复我所知道的永远是我的荣幸。

标签: android canvas drawing paint


【解决方案1】:

试试这个东西,

这将返回一个Bitmap,它将是two Bitmap Images 之一的Merged,它还将保存在SDCard 中。

public Bitmap combineImages(Bitmap c, Bitmap s) {

        Bitmap cs = null;
        int width, height = 0;

        if (c.getWidth() > s.getWidth()) {
            width = c.getWidth();
            height = c.getHeight();
        } else {
            width = s.getWidth() + s.getWidth();
            height = c.getHeight();
        }

        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas comboImage = new Canvas(cs);

        comboImage.drawBitmap(c, 0, 0, null);
        comboImage.drawBitmap(s, 100, 300, null);

        /******
         * 
         *   Write file to SDCard
         * 
         * ****/

        String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
        OutputStream os = null;
        try {
            os = new FileOutputStream(Environment.getExternalStorageDirectory()
                    + "/"+tmpImg);
            cs.compress(CompressFormat.PNG, 100, os);
        } catch (IOException e) {
            Log.e("combineImages", "problem combining images", e);
        }
        return cs;
    }

【讨论】:

    【解决方案2】:

    对于您正在创建的任何视图,您都可以创建其当前显示内容的位图。 使用:

    view.setDrawingCacheEnabled(true);
    Bitmap bitmap=view.getDrawingCache();
    

    这能帮助你实现你想要的吗? *请务必在完成后回收这些位图。

    bitmap.recycle();
    

    【讨论】:

    • 它没有保存背景图片。只是绘图。
    • 我不知道,用根视图或装饰视图试试同样的方法。我已经使用类似的东西从表面视图上渲染的 3d 对象获取图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2013-03-02
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多