Android development of custom rounded rectangle picture ImageView implementation

Source: Internet
Author: User

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

As follows:

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

Package Com.xc.xcskin.view;import Android.content.context;import android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.rectf;import Android.graphics.porterduff.mode;import Android.graphics.porterduffxfermode;import Android.graphics.rect;import Android.graphics.drawable.bitmapdrawable;import android.graphics.drawable.Drawable; Import Android.util.attributeset;import Android.widget.ImageView;/** * Custom Rounded Rectangle ImageView that can be used directly when the component is in the layout. * @author caizhiming **/ Public classXcroundrectimageview extends imageview{PrivatePaint paint;  PublicXcroundrectimageview (Context context) { This(Context,NULL); }         PublicXcroundrectimageview (Context context, AttributeSet attrs) { This(Context, Attrs,0); }         PublicXcroundrectimageview (context context, AttributeSet attrs,intDefstyle)         {Super (context, attrs, Defstyle); Paint=NewPaint (); }        /** * Draw Rounded Rectangle picture * @author caizhiming*/@Overrideprotected voidOnDraw (canvas canvas) {drawable drawable=getdrawable (); if(NULL!=drawable) {Bitmap Bitmap=((bitmapdrawable) drawable). Getbitmap (); Bitmap b= Getroundbitmap (Bitmap, -); Final Rect rectsrc=NewRect (0,0, B.getwidth (), B.getheight ()); Final Rect rectdest=NewRect (0,0, GetWidth (), getheight ());              Paint.reset ();            Canvas.drawbitmap (b, Rectsrc, rectdest, paint); } Else{super.ondraw (canvas); }      }        /** * Get Rounded Rectangle picture method * @param bitmap * @param roundpx, generally set to * @return Bitmap * @author caizhiming */    PrivateBitmap Getroundbitmap (Bitmap Bitmap,introundpx) {Bitmap Output=Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Canvas Canvas=NewCanvas (output); Finalintcolor =0xff424242; Final rect rect=NewRect (0,0, Bitmap.getwidth (), Bitmap.getheight ()); Final RECTF RECTF=NewRECTF (rect); Paint.setantialias (true); Canvas.drawargb (0,0,0,0);          Paint.setcolor (color); intx =bitmap.getwidth ();        Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, 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:

<relativelayout xmlns: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.xc.xcskin.view.XCRoundRectImageView Android:id="@+id/roundrectimageview"android:layout_centerinparent="true"Android:layout_width="200DP"Android:layout_height="200DP"android:src="@drawable/roundimageview"/></relativelayout>

Android development of custom rounded rectangle picture ImageView implementation

Related Article

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.