【问题标题】:Paint over the specific area in Android在Android中的特定区域上绘画
【发布时间】:2014-01-29 15:05:52
【问题描述】:

我想使用Canvas 绘制特定的LineRectangleBitmap。如果我在位图上绘制,它也会采用方形空背景。

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

【问题讨论】:

  • 您想在位图周围画一个空矩形吗?像边框?
  • @Leon_SFS hii leon..我只能在画布上绘制字母..我该怎么做..你有什么想法..如果我使用位图绘制,它会采取额外的方形背景表面..我只想在图像上绘制..你能理解吗???
  • 从绘图区域创建一个矩形,然后使用 canvas.clipRect(rect);裁剪您的绘图区域,然后使用 canvas.drawtext 或 .... 在其上绘制所有内容
  • @Leon_SFS 很抱歉经常问...我们可以只从位图中剪辑图像吗..没有空白背景(如 .png 图像)..等待您的回复。
  • 是的,图像是形状的,并且有宽度和高度,你可以在屏幕上绘制时获得左、右、上和下的位置,用它们的位置创建一个矩形并用画布剪辑它(别抱歉,你可以问十几个问题,我们很乐意回答)

标签: android bitmap android-canvas


【解决方案1】:

从您想要的图像中创建一个名称为“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();
}
}

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

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

【讨论】:

    【解决方案2】:

    看来您需要剪辑。参见示例:http://www.example8.com/category/view/id/15543Understanding Android Canvas Clippinghttp://jtomlinson.blogspot.com/2008/10/clipping.html

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多