将图像作为画布背景并与用户绘图一起保存

发布于 2024-12-09 03:22:39 字数 959 浏览 1 评论 0原文

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

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

非常感谢您的帮助! :)

@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);
            }


        }

    }

}

I'm using the code below. My app was able to draw on the canvas and save it.

But what I want to do is make an image as the background of the canvas so when I save it, it will look like an image with the user's drawing on top of it.

Thank you so much for any help! :)

@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);
            }


        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-12-16 03:22:39

尝试这个东西,

这将返回一个位图,它将是两个位图图像之一的合并,它也会保存在SD卡

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;
    }

Try this stuff,

This will return a Bitmap that will be a Merged one of two Bitmap Images, also it will save in the 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;
    }
紫﹏色ふ单纯 2024-12-16 03:22:39

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

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

这有助于您实现您想要的目标吗?
*完成后请务必回收这些位图。

bitmap.recycle();

For any view that you are creating, you can create a bitmap of what it is currently displaying.
use:

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

Does this help you in achieving what you want ?
*be sure to recycle these bitmaps when you are done.

bitmap.recycle();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文