Custom View, circular avatar, and view circular Avatar

Source: Internet
Author: User

Custom View, circular avatar, and view circular Avatar

1.

2. xml

<com.etoury.etoury.ui.view.CircleImg            android:id="@+id/user_info_head_img"            android:layout_width="120dp"            android:layout_height="120dp"            android:layout_centerInParent="true"            android:src="@drawable/ic_user" ></com.etoury.etoury.ui.view.CircleImg>

3. Custom view

CircleImg. java

Package com. etoury. etoury. ui. view; import android. content. context; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. pixelFormat; import android. graphics. porterduxfermode; import android. graphics. rect; import android. graphics. bitmap. config; import android. graphics. porterDuff. mode; import android. graphics. draw Able. drawable; import android. util. attributeSet; import android. widget. imageView; public class CircleImg extends ImageView {public CircleImg (Context context) {super (context); // TODO Auto-generated constructor stub} public CircleImg (Context context, AttributeSet attrs) {super (context, attrs);} public CircleImg (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle );} @ Override protected void onDraw (Canvas canvas) {Drawable drawable = getDrawable (); if (drawable = null) {return;} if (getWidth () = 0 | getHeight () = 0) {return;} int width = drawable. getIntrinsicWidth (); int height = drawable. getIntrinsicHeight (); Bitmap. config config = drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. config. ARGB_8888: Bitmap. config. RGB_565; Bitmap B = Bitmap. createBitmap (width, height, config); // Bitmap B = (BitmapDrawable) drawable ). getBitmap (); // note that the following three lines of code are required. Are they canvas in View or surfaceview. drawBitmap will not see the figure Canvas can = new Canvas (B); drawable. setBounds (0, 0, width, height); drawable. draw (can); if (null = B) {return;} Bitmap bitmap = B. copy (Bitmap. config. ARGB_8888, true); int w = g EtWidth (), h = getHeight (); Bitmap roundBitmap = getCroppedBitmap (bitmap, w); canvas. drawBitmap (roundBitmap, 0, 0, null);} public static Bitmap getCroppedBitmap (Bitmap bmp, int radius) {Bitmap sbmp; if (bmp. getWidth ()! = Radius | bmp. getHeight ()! = Radius) sbmp = Bitmap. createScaledBitmap (bmp, radius, radius, false); else sbmp = bmp; Bitmap output = Bitmap. createBitmap (sbmp. getWidth (), sbmp. getHeight (), Config. ARGB_8888); Canvas canvas = new Canvas (output); final int color = 0xffa19774; final Paint paint = new Paint (); final Rect rect = new Rect (0, 0, sbmp. getWidth (), sbmp. getHeight (); paint. setAntiAlias (true); paint. setFilterBitmap (true); paint. setDither (true); canvas. drawARGB (0, 0, 0, 0); paint. setColor (Color. parseColor ("# BAB399"); canvas. drawCircle (sbmp. getWidth ()/2 + 0.7f, sbmp. getHeight ()/2 + 0.7f, sbmp. getWidth ()/2 + 0.1f, paint); paint. setXfermode (new porterduduxfermode (Mode. SRC_IN); canvas. drawBitmap (sbmp, rect, rect, paint); return output ;}}

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.