Android ImageView picture fillet display (reprint)

Source: Internet
Author: User

Reprinted from: http://www.w2bc.com/Article/3623

Android ImageView can only display the picture of the rectangle, so that does not meet our other needs, such as to display a circular picture, this time, we need to customize the ImageView, the principle is to first get to the image of the bitmap, It then trims the rounded bitmap and then draws the circular picture output in OnDraw ().

The implementation code for the custom circular ImageView class is as follows:

 Packagecom.rainwii.entity;//Package NameImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.Config;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff.Mode;ImportAndroid.graphics.PorterDuffXfermode;ImportAndroid.graphics.Rect;Importandroid.graphics.drawable.BitmapDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ImageView;/*** * Custom circular ImageView can be used directly when the component is in the layout. *  * @authorcaizhiming **/ Public classXcroundimageviewextendsImageView {PrivatePaint paint;  PublicXcroundimageview (Context context) { This(Context,NULL); }     PublicXcroundimageview (Context context, AttributeSet attrs) { This(context, Attrs, 0); }     PublicXcroundimageview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); Paint=NewPaint (); }    /*** * Draw a circular picture * *@authorcaizhiming*/@Overrideprotected voidOnDraw (canvas canvas) {drawable drawable=getdrawable (); if(NULL!=drawable) {Bitmap Bitmap=((bitmapdrawable) drawable). Getbitmap (); Bitmap b= Getcirclebitmap (Bitmap, 14); FinalRect RECTSRC =NewRect (0, 0, B.getwidth (), B.getheight ()); FinalRect rectdest =NewRect (0, 0, GetWidth (), getheight ());            Paint.reset ();        Canvas.drawbitmap (b, Rectsrc, rectdest, paint); } Else {            Super. OnDraw (canvas); }    }    /*** * Get round picture method * *@paramBitmap *@parampixels *@returnBitmap *@authorcaizhiming*/    PrivateBitmap Getcirclebitmap (Bitmap Bitmap,intpixels) {Bitmap Output=Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Canvas Canvas=NewCanvas (output); Final intcolor = 0xff424242; FinalRect rect =NewRect (0, 0, Bitmap.getwidth (), Bitmap.getheight ()); Paint.setantialias (true); Canvas.drawargb (0, 0, 0, 0);        Paint.setcolor (color); intx =bitmap.getwidth (); Canvas.drawcircle (x/2, X/2, X/2, paint); Paint.setxfermode (NewPorterduffxfermode (mode.src_in));        Canvas.drawbitmap (Bitmap, rect, rect, paint); returnoutput; }}

Once you have completed this custom class, you can use this class as a component in the layout, such as:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
Android:layout_height= "Match_parent" > <Com.rainwii.entity.XCRoundImageViewAndroid:id= "@+id/roundimageview"
Android:layout_centerinparent= "true"
Android:layout_width= "200DP"
Android:layout_height= "200DP"
ANDROID:SRC= "@drawable/roundimageview" /></Relativelayout>

Additional Student Information: http://my.oschina.net/sammy1990/blog/316400

http://blog.csdn.net/lmj623565791/article/details/24555655

Android ImageView picture fillet display (reprint)

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.