Here is my scenario:
I have one layout with a view where I can cal and get canvas on its onDraw() method. Now, upon an event, say onTouchEvent(), I would like to clip a portion of the canvas and display this portion into another view of another layout.
I found code snippet doing similar thing but they are based on the java.awt package which does not supported by the Android API, especially the core component like java.awt.Graphics2D
I would like to follow the similar algorithm using canvas instead of the Graphics2D in Android to draw its content into another canvas. How do we achieve this?
Basically, I would like to achieve the same result as done in the following code in java.awt:
protected void paint(Graphics2D g2) {
Ellipse2D ellipse = //create ellipse which is to be displayed in another surface
g2.fill(ellipse); //how to achieve the same result in Android?
g2.draw(ellipse); //how to achieve the same result in Android?
g2.clip(ellipse); //how to achieve the same result in Android?
....
...
//note: Afaik, in Android, the code like Drawable.draw(canvas, ....),
//will achieve result in the opposite way where the Drawable would be drawn into
//the Canvas whereas G2.fill(ellipse) would fill the ellipse with the G2's content
}
If I understood correctly what you need, you could try the code as follows.
This is how the activity’s xml layout file look like:
The activity’s source code:
The custom view I called SourceImageView:
Be careful when dealing with bitmaps, they can be an expensive source of memory leaks, specially if you are working with older versions of android. I haven’t checked for leaks in this code. Probably this can be improved (refactored, optimized).
if this isn’t what you want, I hope that helps giving you ideas and pointing you to the right direction.
==EDIT==
It may not be clear so I must say that I assumed as an example that the cropped part of the image is an area of a circle of 20 px radius.