绘制Android中的特定区域

时间:2013-07-09 09:53:19

标签: android bitmap android-canvas

我想使用Line绘制特定的RectangleBitmapCanvas。如果我在Bitmap上绘制,它也将采用方形空白背景。

所以我只想在那个特定的Bitmap区域上画画。

2 个答案:

答案 0 :(得分:2)

从您想要的图像中创建一个带有“bmp1”名称的位图 创建自定义视图
创建一个类并像这样扩展View

class MyCustomView extends View{

private Rect m_ImageRect;
private Rect m_TextRect ;

//you need these constructor
//you can init paint object or anything on them
public MyCustomView (Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    m_Context = context;

}

public MyCustomView (Context context, AttributeSet attrs)
{
    super(context, attrs);
    m_Context = context;

}

public MyCustomView (Context context)
{
    super(context);
    m_Context = context;

}

//then override on draw method
@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
            //here frist create two rectangle
            //one for your image and two for text you want draw on it
    m_ImageRect = canvas.getClipBounds();
        m_TextRect = canvas.getClipBounds();
            //it gives you an area that can draw on it,
            //the width and height of your rect depend on your screen size device
            canvas.drawBitmap(your bitmap(bmp1), null, m_ImageRect , paint);
            canvas.save();
    canvas.clipRect(m_TextRect);

            canvas.drawText("your text", the x position you want to start draw,
            the y position you want to start draw, m_paintText);

            canvas.restore();
}
}

最后在您的布局上放置自定义视图,并在其上设置字段以发送值以查看绘制您想要的所有内容

如果这不是你想要的,我希望它对你有所帮助! 发布你的代码所以也许我可以帮助你更多

答案 1 :(得分:0)

好像你需要剪裁。请参阅示例:http://www.example8.com/category/view/id/15543Understanding Android Canvas Clippinghttp://jtomlinson.blogspot.com/2008/10/clipping.html

使用剪辑,您可以指定哪些区域应该是“可编辑的”。

相关问题