"Android Learning Notes" Rounded rectangle imageview the implementation and use of custom controls

Source: Internet
Author: User

In the process of doing an Android project, we always encounter the need to use rounded rectangle controls to display icons, pictures, or buttons, one solution is to create a shape layout XML file under Drawable, and a custom control class that inherits from ImageView. Here is a concrete way to achieve this.

First we name a Xcroundrectimageview class and inherit from ImageView. The code is as follows:

1 ImportAndroid.content.Context;2 ImportAndroid.graphics.Bitmap;3 ImportAndroid.graphics.Bitmap.Config;4 ImportAndroid.graphics.Canvas;5 ImportAndroid.graphics.Paint;6 ImportAndroid.graphics.PixelFormat;7 ImportAndroid.graphics.RectF;8 ImportAndroid.graphics.PorterDuff.Mode;9 ImportAndroid.graphics.PorterDuffXfermode;Ten ImportAndroid.graphics.Rect; One Importandroid.graphics.drawable.BitmapDrawable; A Importandroid.graphics.drawable.Drawable; - ImportAndroid.util.AttributeSet; - ImportAndroid.widget.ImageView; the  - 
- - /** + * Created by Yang on 2016-03-18. - * Custom Rounded rectangle ImageView that can be used directly when the component is in the layout. + */ A Public classXcroundrectimageviewextendsImageView { at Privatepaint paint; - - PublicXcroundrectimageview (Context context) { - This(Context,NULL); - } - in PublicXcroundrectimageview (Context context, AttributeSet attrs) { - This(context, Attrs, 0); to } + - PublicXcroundrectimageview (context context, AttributeSet attrs,intDefstyle) { the Super(context, attrs, defstyle); *Paint =NewPaint (); $ }Panax Notoginseng - /** the * Draw Rounded Rectangle picture + */ A @Override the protected voidOnDraw (canvas canvas) { +Drawable drawable =getdrawable (); - if(NULL!=drawable) { $Bitmap Bitmap =NULL; $ if(drawableinstanceofbitmapdrawable) { -Bitmap =((bitmapdrawable) drawable). Getbitmap (); -}Else if(drawableinstanceofasyncdrawable) { theBitmap =Bitmap.createbitmap (GetWidth (), - getheight (),WuyiDrawable.getopacity ()! = Pixelformat.opaque?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565); theCanvas CANVAS1 =NewCanvas (bitmap); -Drawable.setbounds (0, 0, GetWidth (), getheight ()); Wu Drawable.draw (CANVAS1); - } About //10 rounded angular angle size for the set fillet, modifiable $Bitmap B = Getroundbitmap (Bitmap, 10); - FinalRect RECTSRC =NewRect (0, 0, B.getwidth (), B.getheight ()); - FinalRect rectdest =NewRect (0, 0, GetWidth (), getheight ()); - Paint.reset (); A Canvas.drawbitmap (b, Rectsrc, rectdest, paint); +}Else { the Super. OnDraw (canvas); - } $ } the the /** the * Get Rounded Rectangle picture method the * @paramBitmap - * @returnBitmap in */ the PrivateBitmap Getroundbitmap (Bitmap Bitmap,introundpx) { theBitmap output =Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); AboutCanvas Canvas =NewCanvas (output); the Final intcolor = 0xff424242; the FinalRect rect =NewRect (0, 0, Bitmap.getwidth (), Bitmap.getheight ()); the FinalRECTF RECTF =NewRECTF (rect); +Paint.setantialias (true); -Canvas.drawargb (0, 0, 0, 0); the paint.setcolor (color);Bayi intx =bitmap.getwidth (); the Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); thePaint.setxfermode (NewPorterduffxfermode (mode.src_in)); - canvas.drawbitmap (Bitmap, rect, rect, paint); - returnoutput; the } the}

Just refer to the custom control where the rounded rectangle is needed, as shown in the code below

< <!-- your package name-- > . View. Xcroundrectimageview                    android:id= "@+id/xcroundrectimageview"                    android:layout_width= "Wrap_content"                    android:layout_height= "Wrap_content"/>

Android Learning notes Rounded rectangle ImageView the implementation and use of custom controls

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.