Custom View Imageviw

Source: Internet
Author: User

Create a new class to inherit ImageView

Package Com.exaple.myselfview;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.porterduffxfermode;import Android.graphics.rect;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.util.attributeset;import Android.view.view;import Android.widget.imageview;public class MyView extends ImageView {public MyView (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//TODO auto- Generated constructor Stub}public MyView (context context) {super (context);//TODO auto-generated constructor stub} Public MyView (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor stub}@ overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubdrawable drawable = Getdrawable (); if (draw able = = null) {return;} If(getwidth () = = 0 | | getheight () = = 0) {return;} Bitmap B = ((bitmapdrawable) drawable). Getbitmap (); if (null = = b) {return;} Bitmap Bitmap = B.copy (Bitmap.Config.ARGB_8888, true);///Get the picture of the width of the high int w = getwidth (), H = getheight ();//Through GETCROPPEDBITM AP function, return a circular picture bitmap Roundbitmap = Getcroppedbitmap (bitmap, w);//show Circleimageex on a custom Canvas.drawbitmap (roundbitmap , 0, 0, NULL);/* Super.ondraw (canvas); */}public static Bitmap Getcroppedbitmap (Bitmap bmp, int radius) {Bitmap p;//determines whether the size of the picture is equal to the incoming radius, if not, then//The picture is set to grow wide The radius of the picture if (Bmp.getwidth ()! = Radius | | bmp.getheight () = radius) p = bitmap.createscaledbitmap (BMP, radius, radius, Fals e) Elsep = bmp;//The last output of the picture information Bitmap output = Bitmap.createbitmap (P.getwidth (), P.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, p.getwidth (), P.getheight ());//brush with anti-aliasing flag, image smoother Paint.setantialias (true);//If the key is set to True, The image will filter out the optimized operation of the bitmap image in the animation and speed up the display of PAInt.setfilterbitmap (TRUE);//Anti-jitter Paint.setdither (TRUE);//Transparent Color Canvas.drawargb (0, 0, 0, 0);//Brush Color Paint.setcolor ( Color.parsecolor ("#BAB399"));//Draw a circular canvas.drawcircle (P.getwidth ()/2, P.getheight ()/2,p.getwidth ()/2 + 0.1f, pain t);//Set the pattern at the intersection of two pictures, which is to cover the picture information of the circle on the canvas Paint.setxfermode (New Porterduffxfermode (android.graphics.PorterDuff.Mode.SRC_ in)); Canvas.drawbitmap (P, rect, rect, paint); return output;}}

Be sure to SRC instead of background in XML

Custom View Imageviw

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.