0

In the below image red color is the canvas. The black color part is the figure I am drawing in my canvas. I want to crop the black color part from my canvas. How shall I do it?.

I tried using canvas.clipPath() but I guess its no longer supported in Android now, due to hardware acceleration. I tried disableing the hardware acceleration also but it dint work so I gave up on canvas.clipPath().

Is there any other way I can use to crop path within canvas?

Any help will be appreciated. Thanks in advance :)

enter image description here

enter image description here

enter image description here

ik024
  • 3,566
  • 7
  • 38
  • 61
  • "crop the black color" what do. you exactly mean? – pskink Apr 07 '14 at 10:34
  • i dont want the red color part as part of my view (red color is the custom view where I am drawing) – ik024 Apr 07 '14 at 10:36
  • so dont draw the red part? draw only black part – pskink Apr 07 '14 at 10:38
  • the red part is the entire canvas. – ik024 Apr 07 '14 at 10:40
  • the red part is the extra space of the canvas I want to remove that – ik024 Apr 07 '14 at 10:41
  • I will b adding this custom view to my acyivity so I dont want that red color (extra spcace) in my activity – ik024 Apr 07 '14 at 10:42
  • @pskink i hope u understood my question ? – ik024 Apr 07 '14 at 10:47
  • who draws that red color? – pskink Apr 07 '14 at 11:08
  • i have set the bg color as red just to show u guys the extra space of canvas being generated which I want to remove – ik024 Apr 07 '14 at 11:09
  • @pskink basically i want to remove the extra space of my canvas after I finish drawing so that my view is exactly of the shape wat I draw – ik024 Apr 07 '14 at 11:11
  • so make its dimensions to mach the black part – pskink Apr 07 '14 at 11:20
  • @pskink then the tail part wont be visible of the black fig – ik024 Apr 07 '14 at 12:05
  • so i dont understand your question at all, what do you want as the output? a view? bitmap? drawable? – pskink Apr 07 '14 at 12:14
  • I just want the complete black color fig without red color as my output it doesnt have to be a view even bitmap or drawable will do – ik024 Apr 07 '14 at 12:20
  • ok so what do you need it for? – pskink Apr 07 '14 at 12:30
  • I need it to place it in my activity. Where I will b having touch listener added to it so that user can move it – ik024 Apr 07 '14 at 12:31
  • you cannot place a Canvas in your activity, activity has no concept of Canvas, Canvas can be attached to View or Bitmap only – pskink Apr 07 '14 at 12:37
  • Currently I am placing it in my activity as a view – ik024 Apr 07 '14 at 12:39
  • The grey bg in my image above is my activity (empty) the black+red color image is my custom view. I want that red color part to b excluded from my view – ik024 Apr 07 '14 at 12:41
  • so create a big custom view that covers. the whole screen and draw whatever you want in onDraw method – pskink Apr 07 '14 at 12:49
  • @pskink the red color is the bg color of the canvas. I can set it to Color.TRANSPARENT and the red color will go away. The reason I hv set it to red color is to show u guys that extra space is being generated. When I place the view in my activity and add the touch listener to it, and if user touches the extra space which transparent and not visible the touch event gets fired. I want to avoid it which is y I want to remove the extra space i.e red color in the above img – ik024 Apr 07 '14 at 12:49
  • @pskink i will b having multiple views added dynamically in my activity – ik024 Apr 07 '14 at 12:50
  • @pskink I think i will be approaching this prob in a diff way cud u pls ans this question http://stackoverflow.com/questions/22930048/adding-multiple-touch-listeners-inside-canvas-android?lq=1 – ik024 Apr 08 '14 at 07:21
  • did you see my answer? – pskink Apr 08 '14 at 08:12
  • @pskink yes i saw ur ans but its for two finger touch – ik024 Apr 08 '14 at 08:35
  • active Layer selection has nothing to do with multitouch, it simply finds the Layer under the finger and makes it active sending any other MorionEvents to that Layer – pskink Apr 08 '14 at 11:01
  • @pskink I have added two images in my question above. The first image has two custom views added in my activity. One has red color representing its extra space and other has grey color as its extra space. In the second image the two images are overlapping, the "Orange" pointer shows wer the user is touching on screen. Since I will be setting canvas bg as transparent the red and grey color wont b visible to user. Now the user wants to move the blue circle but instead the black square will move since v are actually touching the grey part which will activate the black color square custom view – ik024 Apr 08 '14 at 11:40
  • did you try my code? if you dont want scale/rotate gestures just remove them – pskink Apr 08 '14 at 11:52
  • @pskink no i havent tried it out yet. are u sure it will work in my case? – ik024 Apr 08 '14 at 11:55
  • @pskink were is ur MoveGestureDetector class? – ik024 Apr 08 '14 at 11:59
  • it will work, just create three .png images: circle, triangle, star and test it – pskink Apr 08 '14 at 12:00
  • @pskink okay. I got the class from the link u provided – ik024 Apr 08 '14 at 12:01
  • @pskink awesome :) its working however I tried it with some imageviews I had my how can I put my custom views inside ids array ?? – ik024 Apr 08 '14 at 12:13
  • @pskink anyways thanks a lot man I really appreciate ur help :) can u also explain me ur ViewPort Class and also how to put custom views instead of imageviews ?? – ik024 Apr 08 '14 at 12:14
  • what ImageViews? i dont use any ImageViews, i use Layer class which draws a Bitmap – pskink Apr 08 '14 at 12:20
  • i mean, int[] ids = {R.drawable.handle_move, R.drawable.handle_scale, R.drawable.ic_launcher}; here we are giving the images from res folder how can I pass my custom views which I am drawing instead of those images – ik024 Apr 08 '14 at 12:25
  • Layer.draw draws a Bitmap, so you can put your custom drawing there – pskink Apr 08 '14 at 12:35
  • @pskink i am drawing my drawings on a bitmap and passing it to the ViewPort class and adding it to the Layer class and its working fine :). But one thing I dint understand is that how is it recognizing the bitmap ??? can u pls explain that. As in v have arrays of bitmaps how is it recognizing each one ? – ik024 Apr 08 '14 at 13:03
  • recognizing the bitmap ??? what you mean? – pskink Apr 08 '14 at 13:16
  • as in even if the image is below another image it brings the image above and moves it. If i take my example of above two images that I hv added. If I am clicking on the grey part (which is invisible) it still brings the blue circle above and moves it. – ik024 Apr 08 '14 at 13:21
  • target = l;layers.remove(l);layers.add(l);invalidate(); I knw that this code shifts the bitmaps arrangements but how is it determining the bitmap ? – ik024 Apr 08 '14 at 13:25
  • the code you mentioned is executed in "if": if (l.contains(event)) {... – pskink Apr 08 '14 at 13:31
  • no wat I meant was is - how is it recognizing the blue circle if I click on the grey portion of the black box ? – ik024 Apr 08 '14 at 13:35
  • I think this function is the key in identifying the bitmaps can u pls explain this func - public boolean contains(MotionEvent event) { matrix.invert(inverse); float[] pts = {event.getX(), event.getY()}; inverse.mapPoints(pts); if (!bounds.contains(pts[0], pts[1])) { return false; } return Color.alpha(bitmap.getPixel((int) pts[0], (int) pts[1])) != 0; } – ik024 Apr 08 '14 at 13:40
  • just debug it, place the breakpoint in the first line of contains() method, "step over" each line and see the variables – pskink Apr 08 '14 at 13:46

0 Answers0