How to implement an Android fillet border _android

Source: Internet
Author: User

First I will post several Dmeo program effects that implement rounded corners:

Way one: Fill the background with the shape element, set the fillet/radian angle

1, first in \res\drawable under the new shape is the root element of the resource file: Corners_bg.xml, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android=
"http://schemas.android.com/apk/res/" Android >
<!--set the fill color to white-->
<solid android:color= "#FFFFFF"/>
<!--set the curvature of four corners of the geometry-- >
<corners android:radius= "10DP"/>
<!--set geometric shape draw border red border-->
<stroke android:color= "# CD2626 "
android:width=" 2DP "/>
</shape>

2, in the layout layout file for the control that needs to refer to the first step of the XML file as a background. As shown in figure

3, the effect shown as a demo program effect diagram of the effect

Mode two: Use .9< nine sister > with rounded corner picture set as background


As shown in Figure mm_title_act_btn_normal.9.png is a picture with rounded corners, just reference it as a control [here is a button] background resource. Display effect for demo program effect diagram of the second effect

Mode three: Overriding the control's Draw method [here I inherit ImageView as an example]: Re-draw the method to draw the rounded effect of the control

1, Cornersimageview.java for the custom fillet ImageView code as follows:

Package Com.ice.cornersdemo;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapShader;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.RectF;
Import Android.graphics.Shader;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.ColorDrawable;
Import android.graphics.drawable.Drawable;
Import Android.util.AttributeSet;
Import Android.widget.ImageView;
/** * Custom ImageView * Created by ice in 14-8-6 with rounded corners. * * public class Cornersimageview extends imageview{private Bitmap mbitmap; private bitmapshader mbitmapshader; private fi
NAL Matrix Mshadermatrix = new Matrix ();
Private final Paint mbitmappaint = new Paint ();
Private final RECTF mdrawablerect = new RECTF ();
private float Mdrawableradius;
private int mbitmapwidth;
private int mbitmapheight;
private static final Bitmap.config bitmap_config = Bitmap.Config.ARGB_8888; private static final int colordrAwable_dimension = 1; Public Cornersimageview {Super [context];} public Cornersimageview (context, AttributeSet attrs  {This [context, attrs, 0];} public Cornersimageview (context, AttributeSet attrs, int defstyle) {Super (context,
Attrs, Defstyle);
Init (); private void init () {if (Mbitmap = null) {return;} Mbitmapshader = new Bitmapshader (Mbitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
Set anti-aliasing Mbitmappaint.setantialias (true);
Using a bit Tuping paving rendering effect Mbitmappaint.setshader (Mbitmapshader);
Mbitmapwidth = Mbitmap.getwidth ();
Mbitmapheight = Mbitmap.getheight ();
Mdrawablerect.set (0, 0, mbitmapwidth, mbitmapheight);
Get the circular radius of the picture Show Mdrawableradius = Math.min (Mdrawablerect.width ()/2, Mdrawablerect.height ()/2);
Invalidate (); @Override public void Draw (Canvas Canvas) {if (getdrawable () = null) {return;} canvas.drawcircle (GetWidth ()/2, gethe
Ight ()/2, Mdrawableradius, mbitmappaint); @Override public void Setimagebitmap (Bitmap bm) {Super.setimagebitMap (BM);
Mbitmap = BM;
Init (); @Override public void setimagedrawable (drawable drawable) {super.setimagedrawable (drawable); mbitmap =
Getbitmapfromdrawable (drawable);
Init (); @Override public void Setimageresource (int resid) {super.setimageresource (resid); mbitmap = Getbitmapfromdrawable (
Getdrawable ());
Init (); /** * Converts drawable to Bitmap object * @param drawable * @return/private Bitmap getbitmapfromdrawable (drawable drawable) {if ( Drawable = = null) {return null;} if (drawable instanceof bitmapdrawable) {return (bitmapdrawable) drawable). Getbitmap (
); try {Bitmap Bitmap; if (drawable instanceof colordrawable) {Bitmap = Bitmap.createbitmap (colordrawable_dimension, COLO
Rdrawable_dimension, Bitmap_config);
else {bitmap = Bitmap.createbitmap (Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), bitmap_config);
Canvas Canvas = new Canvas (bitmap);
Drawable.setbounds (0, 0, canvas.getwidth (), Canvas.getheight ());
Drawable.draw (canvas);
return bitmap; catch (OutofmemorYerror e) {return null;}} }

2, use the custom control in the layout file Cornersimageview the following figure:

3, the display effect for the demo program Effect diagram of the three effects

Other: In fact, for the implementation of the image of the circular display before the rewrite ImageView draw method is to handle the control itself, there is a simpler way is to display the image of the processing, and the picture itself to make a circular cut, cut after the display on the control ImageView.
The picture is cut in a circular manner as follows:

/**
* Cut the square picture into a circle figure
* @param bitmap
* @return
/public
static bitmap Getcirclebitmap (bitmap bitmap) {
Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888);
Canvas Canvas = new Canvas (output);
Rect Rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());
RECTF RECTF = new RECTF (rect);
Paint Paint = new Paint ();
Paint.setantialias (TRUE); Prevent jagged
Paint.setfilterbitmap (true) of edges;//Filter the bitmap
canvas.drawoval (RECTF, paint)///////////Circle by RECTF
//Set the display mode of the two pictures at the intersection of Src_in
Paint.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (Bitmap, rect, RECTF, paint);
return output;
}

On the code we just get the bitmap object for the picture to be displayed, and then call the method above

Getcirclebitmap (Bitmap Bitmap) gets a new Bitmap object after a round cut, and then the method ImageView (SETIMAGEBITMA) that invokes Bitmapbitmap is displayed on the control. The display effect is the diagram four effect in the demo program effect diagram
[Same as figure three effect].

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.