1

I want to draw over the specific Line, Rectangle or Bitmap using Canvas. If i draw over the Bitmap, it will take the square shape empty background also.

So i want to draw over that particular Bitmap area only.

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
karthik
  • 123
  • 13
  • you want draw a empty rectangle around the bitmap? like border? – Saeed-rz Jul 09 '13 at 09:56
  • @Leon_SFS hii leon..i have to paint over the alphabet letters only in canvas..how can i do that..do you have any idea..if i using bitmap to draw,it will take the extra square shape background surface..i want to draw only over the image..can u understand this??? – karthik Jul 09 '13 at 10:32
  • create a rectangle from your drawing area,then use canvas.clipRect(rect); to crop your drawing area, then use canvas.drawtext or .... to draw every thing on it – Saeed-rz Jul 09 '13 at 10:43
  • @Leon_SFS sorry to ask frequently...can we clip image only from bitmap..without the empty background(like .png image)..waiting for your reply. – karthik Jul 09 '13 at 12:01
  • yes,the image is shape,and have width and height,you can get the left,right,top and bottom position when draw on screen,create a rect with their position and clip it with canvas (dont be sorry,you can ask dozen question and we enjoy to answer them) – Saeed-rz Jul 09 '13 at 12:43
  • @Leon_SFS Thanks Leon,by this method we will get the square or rectangle shape image only..but i want to clip the image as like as .png image.For example i have to draw alphabet letter A over the bitmap image A..but If i create the bitmap in screen or clipping the bitmap.it would appear as a .jpg square shaped background image..i need only the image without that empty background..can you get my question clearly. – karthik Jul 09 '13 at 13:58
  • @Leon_SFS can i send my code through comment??????? – karthik Jul 10 '13 at 09:11
  • @Leon_SFS im working with your code but i cant draw over the bitmap only..it draw over full screen..but i need to draw only inside the bitmap. – karthik Jul 10 '13 at 09:26
  • edit your question and post code inside it @karthik – Saeed-rz Jul 10 '13 at 09:36
  • @Leon_SFS i posted a question 1 hour before i didn't get any solution. – karthik Jul 10 '13 at 12:37

2 Answers2

3

create a bitmap with "bmp1" name from your desire image
create a custom view
create a class and extend View like this

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();
}
}

at the end put the custom view on your layout,and set field on it to send value to view for draw every thing you want

i hope it's help you,if this is not what you want!
post your code so maybe i can help you more

Saeed-rz
  • 1,435
  • 1
  • 20
  • 38
0

Seems like you need clipping. See exampls: http://www.example8.com/category/view/id/15543 , Understanding Android Canvas Clipping , http://jtomlinson.blogspot.com/2008/10/clipping.html

With clipping you can specify, which regions should be 'editable'.

Community
  • 1
  • 1
Zielony
  • 16,239
  • 6
  • 34
  • 39