http://blog.csdn.net/l448288137/article/details/48276681
Recent project development to use the fillet picture, the web found the round corner picture control is more rigid, only can be rounded corners. One of the best feeling is the semicircular corner link here. Thinking about it, I made a little improvement on the basis of this, so that the picture can be set to a rounded corner.
First on:
Core code
[Java]View PlainCopy
- Package fillet.sgn.com.filletimage;
- Import Android.graphics.Bitmap;
- Import Android.graphics.Canvas;
- Import Android.graphics.Color;
- Import Android.graphics.Paint;
- Import Android.graphics.PorterDuff;
- Import Android.graphics.PorterDuffXfermode;
- Import Android.graphics.Rect;
- Import Android.graphics.RectF;
- /**
- * Created by Liujinhua on 15/9/7.
- */
- Public class Bitmapfillet {
- public static final int corner_none = 0;
- public static final int corner_top_left = 1;
- public static final int corner_top_right = 1 << 1;
- public static final int corner_bottom_left = 1 << 2;
- public static final int corner_bottom_right = 1 << 3;
- public static final int corner_all = Corner_top_left | Corner_top_right | Corner_bottom_left | Corner_bottom_right;
- public static final int corner_top = Corner_top_left | Corner_top_right;
- public static final int corner_bottom = Corner_bottom_left | Corner_bottom_right;
- public static final int corner_left = Corner_top_left | Corner_bottom_left;
- public static final int corner_right = Corner_top_right | Corner_bottom_right;
- public static Bitmap fillet (Bitmap Bitmap, int roundpx,int corners) {
- try {
- //The principle is: first create a transparent bitmap artboard with the same size as the picture
- ///Then draw an area of the desired shape on the artboard.
- //finally post the source image.
- final int width = bitmap.getwidth ();
- final int height = bitmap.getheight ();
- Bitmap Paintingboard = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
- Canvas canvas = new Canvas (Paintingboard);
- Canvas.drawargb (Color.transparent, Color.transparent, Color.transparent, color.transparent);
- Final Paint paint = new paint ();
- Paint.setantialias (true);
- Paint.setcolor (Color.Black);
- //Draw 4 rounded corners
- final RECTF RECTF = new RECTF (0, 0, width, height);
- Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
- //Remove unwanted fillets
- int notroundedcorners = Corners ^ Corner_all;
- if ((Notroundedcorners & corner_top_left)! = 0) {
- Cliptopleft (Canvas,paint,roundpx,width,height);
- }
- if ((Notroundedcorners & corner_top_right)! = 0) {
- Cliptopright (canvas, Paint, roundpx, width, height);
- }
- if ((Notroundedcorners & corner_bottom_left)! = 0) {
- Clipbottomleft (Canvas,paint,roundpx,width,height);
- }
- if ((Notroundedcorners & corner_bottom_right)! = 0) {
- Clipbottomright (canvas, Paint, roundpx, width, height);
- }
- Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN));
- //Post map
- final rect src = new rect (0, 0, width, height);
- final Rect dst = src;
- Canvas.drawbitmap (bitmap, SRC, DST, paint);
- return paintingboard;
- } catch (Exception exp) {
- return bitmap;
- }
- }
- private static void Cliptopleft (final canvas canvas, final paint paint, int offset, int width, int height) {
- Final rect block = new rect (0, 0, offset, offset);
- Canvas.drawrect (block, paint);
- }
- private static void Cliptopright (final canvas canvas, final paint paint, int offset, int Widt h, int height) {
- Final rect block = new rect (Width-offset, 0, width, offset);
- Canvas.drawrect (block, paint);
- }
- private static void Clipbottomleft (final canvas canvas, final paint paint, int offset, int wid th, int height) {
- Final rect block = new rect (0, Height-offset, offset, height);
- Canvas.drawrect (block, paint);
- }
- private static void Clipbottomright (final canvas canvas, final paint paint, int offset, int WI DTH, int height) {
- Final rect block = new rect (Width-offset, height-offset, width, height);
- Canvas.drawrect (block, paint);
- }
- }
Set any corner of the bitmap picture in Android to rounded corners