如何动态地绘制位图在Android的画布? [英] How to dynamically draw bitmaps to a Canvas in Android?

查看:269
本文介绍了如何动态地绘制位图在Android的画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是问题的方案。
自定义视图这是在我需要绘制的画布。
它下面的2个按钮,让我们称他们为A和B
当点击A - >图像A被吸引到上面的画布。
当被点击。B - >图像B被绘制于上述帆布

Here is the scenario of the issue . A custom view which is a canvas on which I need to draw . 2 buttons beneath it ,lets call them A and B When A is clicked -> Image A is drawn to the above canvas . When B is clicked -> Image B is drawn to the above canvas .

问题要求pviously画在画布上的图像的$ P $必须是preserved。也就是说,如果你点击一个按钮之后按钮B,然后在画布上必须包含两个图像。因此,previous图像需要preserved。

The issue demands that the previously drawn image on canvas must be preserved . That is if you click button A followed by button B then the canvas must contain two images . So the previous images need to be preserved.

问题:你是如何实现这一目标?
可能的解决方法1:创建一个ArrayList和不断增加图像此ArrayList。通过更新的ArrayList以帆布的onDraw方法和重绘每一个形象,每按一下按钮。

Problem : How do you achieve this ? Possible solution 1 : Create an ArrayList and keep adding images to this arrayList . Pass the updated arrayList to canvas onDraw method and redraw every single image for every button click .

可能的解决方法2:必须有某种方法preserve画布的状态,使每个按钮上点击你画在画布上的最后preserved状态,仅绘制的新形象。

Possible solution 2 : There has to be some method to preserve the state of the canvas so that on each button click you draw on canvas's last preserved state and draw only the new image .

进一步的要求:吸引到画布上的图像可以拖所以需要跟踪更新的职位。

Further Requirements : The Images drawn to canvas could be dragged so need to keep track of updated positions .

我陷入了僵局,但没有找到一个很好的教程或书籍等攻坚要求,任何帮助是AP preciated。

I am at an impasse and couldn't find a good tutorial or a book tackling such requirement , any help is appreciated .

推荐答案

您可以创建位图,并画出你想要它是什么。

You can create the Bitmap, and draw what you want on it.

Bitmap mBitmap;

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if (changed) {
        if (mBitmap != null) {
            mBitmap.recycle();
            mBitmap = null;
        }
        mBitmap = Bitmap.createBitmap(right - left, bottom - top, Bitmap.Config.ARGB_8888);
        redrawAllYourStuffTo(mBitmap);
    }
}

当按钮被pressed,可以直接绘制位图,像这样的:

when button is pressed, you can draw directly to bitmap, like this:

Canvas canvas = new Canvas(mBitmap);
canvas. ... // draw operations.

// after the bitmap canvas drawing finished, call
invalidate();

的onDraw 的只是画位图

protected void onDraw(Canvas canvas) {
   canvas.drawBitmap(mBitmap, 0, 0, null);
}

这篇关于如何动态地绘制位图在Android的画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆