Supports the circular cropping box, which is cropped to create a circular pattern.
Code based on open source project modification, GitHub on Project Link: https://github.com/shengge/android-crop
Or put it down:
Say the circular cropping implementation section:
1.UI Aspects, the custom Circlehighlightview inherits to Highlightview (the original Rectangle cropping box implementation), directly see the Draw method implementation
@Override protected void Draw (canvas canvas) {canvas.save (); Path PATH = new Path (); Outlinepaint.setstrokewidth (Outlinewidth); if (!hasfocus ()) {//No focus is drawn, draw directly a black rectangular box outlinepaint.setcolor (color.black); Canvas.drawrect (DrawRect, outlinepaint); } else {rect viewdrawingrect = new Rect (); Viewcontext.getdrawingrect (Viewdrawingrect); Cropped box DrawRect, calculates the radius of the circle float radius = (drawrect.right-drawrect.left)/2; Add a circular path.addcircle (drawrect.left + radius, Drawrect.top + radius, radius, DIRECTION.CW); Outlinepaint.setcolor (HighlightColor); Crop the canvas, outside of path, to Outsidepaint fill canvas.clippath (Path, Region.Op.DIFFERENCE); Canvas.drawrect (Viewdrawingrect, outsidepaint); Canvas.restore (); Draw a highlight line on a circle, here Outlinepaint defined Paint.Style.STROKE: Represents the outline of the drawing geometry only. Canvas.drawpath (path, outlinepaint); When Modifymode is grow, draw handles, that is, the four small round if (Handlemode = = Handlemode.always | | (Handlemode = = handlemode.changing && Modifymode = = Modifymode.grow)) {drawhandles (canvas); } } }
This enables the operation of a circular cropping box.
2. Respond and handle user Touch Event 1). Determine touch point coordinates and the position of the circle
/** * Calculates its relationship to the circle based on X, y coordinates (circle, inside Circle, outside) * @param x * @param y * @return */private int gethitoncircle (Floa T x, float y) {Rect r = computelayout (); int retval = Grow_none; Final float hysteresis = 20F; int radius = (r.right-r.left)/2; int CenterX = r.left + radius; int centery = r.top + radius; Determine if the touch position is on a circle float ret = (x-centerx) * (X-centerx) + (y-centery) * (y-centery); Double Rradius = math.sqrt (ret); Double gap = Math.Abs (Rradius-radius); if (Gap <= hysteresis) {//Circle on. Because it is inherited to Highlightview (draw a rectangle) to handle, so the simulation returned to the left and right, rather than the pure circle, the pro-test is available. You can also customize it. if (x < CenterX) {//left retval |= Grow_left_edge; } else {retval |= grow_right_edge; } if (Y < CenterY) {//up retval |= Grow_top_edge; } else {retval |= grow_bottom_edge; }} else if (Rradius > Radius) {//outside retval = Grow_none; } else if (Rradius < radius) {//Inside, execute move retval = move within the circle; } return retval; }Because it is inherited to the highlightview (rectangular box) to achieve, if the point (x, y) position circle, you also need to determine the other quadrant, corresponding to the upper and lower sides of the rectangle position.
2). Move the Crop box in the previous step, if the touch point is inside the circle, it will return to move and process the move.
Grows the cropping rectangle by (dx, dy) in image space void Moveby (float dx, float dy) { Rect invalrect = new Rect (drawrect); Mobile croprect.offset (dx, dy); Put the cropping rectangle inside image Rectangle croprect.offset ( math.max (0, Imagerect.left-croprect.left ), math.max (0, Imagerect.top -Croprect.top)); Croprect.offset ( math.min (0, Imagerect.right -croprect.right), math.min (0, Imagerect.bottom- Croprect.bottom)); DrawRect = Computelayout (); Invalrect.union (drawrect); Invalrect.inset (-(int) Handleradius,-(int) handleradius); Viewcontext.invalidate (Invalrect); }
Move the crop box and ensure that other image images appear in the range.
3). Zoom Crop Box This process is similar to the previous step, and the croprect matrix is scaled so that it is detailed here, as described in the code: Highlightview.growby (float dx, float dy)
3. Save the cropped picture as a circle
/** * @param bitmap src image * @return * /public static bitmap Getcirclebitmap (bitmap bitmap) { bitmap Output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (output); final int color = 0xff424242; Final Paint paint = new paint (); Final rect rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ()); Paint.setantialias (true); Paint.setfilterbitmap (true); Paint.setdither (true); Canvas.drawargb (0, 0, 0, 0); Paint.setcolor (color); Draw a circle on the canvas canvas.drawcircle (Bitmap.getwidth ()/2, Bitmap.getheight ()/2, Bitmap.getwidth ()/2, paint); Paint.setxfermode (New Porterduffxfermode (mode.src_in)); Canvas.drawbitmap (Bitmap, rect, rect, paint); return output; }
Note: When saving bitmap as file, choose PNG, otherwise a black background will appear.
In view of the limited level, from the small language did not learn, describe the more messy, need to understand the source code.
Attached: Another very good open source Project Https://github.com/edmodo/cropper Original: Http://blog.csdn.net/jiantao_yang/article/details/43635581#t0
Android imagecropper Rectangle round Crop Box