简体   繁体   English

如何设置图像显示在当前画布的后面?

[英]How to set the Image to appear behind the current canvas?

I am using canvas to draw the Different colour. 我正在使用画布绘制不同的颜色。 I want to set the Image that should be appear as the background of the canvas. 我想设置应显示为画布背景的图像。 And while i am drawing on that image it should be drawn on the Image not behind the image. 当我在该图像上绘制时,应该在图像上绘制图像,而不是在图像后面。 Right now with below code it is drawing behind the Image. 现在,使用下面的代码将其绘制在图像后面。

 @Override
    public void run() {
        Canvas canvas = null;
        while (_run){
            try{
                canvas = mSurfaceHolder.lockCanvas(null);

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

                commandManager.executeAll(canvas);
            } finally {
                Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
                canvas.drawBitmap(kangoo, 130, 100, null);

                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

    }

So what should i have to do to make drawing on the image ??? 所以我应该怎么做才能在图像上绘图?

As you redrawing the canvas in While(1) loop you can put the statement canvas.drawBitmap(kangoo, 130, 100, null) at the top inside the loop so that its drawn again and again then do you painting over top of it. 在While(1)循环中重新绘制画布时,可以将语句canvas.drawBitmap(kangoo, 130, 100, null)放在循环内部的顶部,以便一次又一次地绘制它,然后在其顶部绘画。

If you want to save the drawn part to you do something like converting the Canvas to bitmap like: 如果要保存绘制的零件,请执行以下操作,例如将Canvas转换为位图:

Bitmap  bitmapToBeDrawnFromNextTime = Bitmap.createBitmap( canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas imageCanvas = new Canvas(bitmapToBeDrawnFromNextTime);
imageCanvas.draw(canvas);

No one have giving me answer. 没有人给我答案。 But i got solution, I have to set the image after clearing canvas. 但是我有解决方案,我必须在清除画布后设置图像。

code: 码:

 @Override
    public void run() {
        //Canvas canvas = null;
        while (_run){


            try{
                canvas = mSurfaceHolder.lockCanvas(null);

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

                Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
                canvas.drawBitmap(kangoo, 130, 100, null);
                commandManager.executeAll(canvas);

            } finally {

                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM