且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加图像在Android的画布

更新时间:2023-11-21 14:03:10

当你想画过一个观点,你必须做的,在的onDraw(),使用在画布通过那里。这画布已经绑定到位图这是你认为的实际绘制。

我不得不做类似的事情,我的方法是这样的:


  • 我曾作为类中的一员将在视图绘制的事的名单。

  • 每当我打听到这个列表,我叫无效(),使的onDraw()将得到调用。

  • 我的的onDraw()是这样的:

...

 保护无效的onDraw(帆布油画){
    super.onDraw(画布); //默认绘图    对于(ThingToBeDrawn件事:mListOfThingsToBeDrawn){
         thing.drawThing(画布); //画出每一件事情上的看法
    }
}

A 画布只是用于绘制工具位图,和它的作品相当不同于 SurfaceView

Good Day Everyone

I was hoping if you could help me understand the concepts of understanding how to add an image into a canvas on a OnTouchEvent implemented on a View. So far, this is what i've come up with.
parent is the Activity where in this customized view is instantiated and is added into.

@Override
protected void onDraw(Canvas canvas)
{
    // TODO Auto-generated method stub
    super.onDraw(canvas);
}

public void insertImage()
{
    if (parent.selected_icon.contentEquals("image1"))
    {
        image = getResources().getDrawable(R.drawable.image1);
    }
    else if (parent.selected_icon.contentEquals("image1"))
    {
        image = getResources().getDrawable(R.drawable.image2);
    }
    else if (parent.selected_icon.contentEquals("iamge3"))
    {
        image = getResources().getDrawable(R.drawable.image3);
    }

    Rect srcRect = new Rect(0, 0, image.getIntrinsicWidth(), 
            image.getIntrinsicHeight());
    Rect dstRect = new Rect(srcRect);

    Bitmap bitmap = Bitmap.createBitmap(image.getIntrinsicWidth(), 
            image.getIntrinsicHeight(), Bitmap.Config.ALPHA_8);

    Canvas canvas = new Canvas();
    canvas.drawBitmap(bitmap, srcRect, dstRect, null);
    invalidate();
}

When you want to draw over a view, you have to do that in onDraw(), using the Canvas passed there. That Canvas is already bound to the Bitmap that is the actual drawing of your view.

I had to do something similar and my approach was like this:

  • I had a list of "things to be drawn over the view" as a member of the class.
  • whenever I added something to that list, I called invalidate(), so that onDraw() would get called.
  • My onDraw() looked like this:

...

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); // the default drawing

    for(ThingToBeDrawn thing : mListOfThingsToBeDrawn) {
         thing.drawThing(canvas); // draw each thing over the view
    }
}

A Canvas is just a tool used to draw a Bitmap, and it works quite differently than SurfaceView.