Android Avatar Modification

Source: Internet
Author: User

http://blog.csdn.net/xiaanming/article/details/18730223 read this blog, sorting out the code they need to write the following custom avatar such a demo.


Key code:

/*** * Avatar Control * @author Lanyan * */@SuppressLint ({"Clickableviewaccessibility", "drawallocation"}) public class Photoimagev Iew extends View implements Onclicklistener{private Paint mpaint;private Bitmap mbitmap;private int newvalue;private int m width;private int Mheight;private boolean isclickable=true;public Bitmap Getmbitmap () {return mbitmap;} public void Setmbitmap (Bitmap mbitmap) {this.mbitmap = Mbitmap;} @SuppressWarnings ("deprecation") public photoimageview (context context, AttributeSet attrs, int defstyleattr) {super ( Context, attrs, defstyleattr);//TODO auto-generated constructor Stubint [] arr=screenutil.initialscreen (context); Mscreenheight=arr[1];mscreenwidth=arr[0];D rawable mdrawable=getbackground (); if (mdrawable!=null) {SetmBitmap ( Imageutils.drawabletobitmap (mdrawable)); setbackgrounddrawable (null);} Mpaint=new Paint (); Setonclicklistener (this);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated method Stubsuper.onmeasure (WidthmeaSurespec, Heightmeasurespec); MHEIGHT=MWIDTH=MSCREENWIDTH&LT;MSCREENHEIGHT?MSCREENWIDTH/3:MSCREENHEIGHT/3;} Public Photoimageview (context context, AttributeSet Attrs) {This (context, attrs,0);//TODO Auto-generated constructor Stub}public Photoimageview (Context context) {this (context,null);//TODO auto-generated constructor stub}@ overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas); Initdraw (canvas);} private void Initdraw (canvas canvas) {//TODO auto-generated method Stubnewvalue=mscreenwidth<=mscreenheight? Mscreenwidth/3/3*2:mscreenheight/3/3*2;if (Getmbitmap () ==null) {Bitmap Mnewbitmap=bitmap.createbitmap (mWidth, Mheight, config.argb_8888); Setmbitmap (Mnewbitmap);} Mbitmap=imageutils.zipimage (Getmbitmap (), newvalue, NewValue); Mbitmap=imageutils.getroundedcornerbitmap (MBitmap, Mbitmap.getwidth ()/2); Mpaint.setantialias (true); Canvas.drawbitmap (Getmbitmap (), MWIDTH/6,MHEIGHT/6, mPaint); Mpaint.setcolor (Color.parsecolor ("#CDC7C1")); Mpaint.setstyle (PAint. Style.stroke); mpaint.setstrokewidth; Mpaint.setantialias (true); Canvas.drawcircle (Mwidth/6+getmbitmap (). GetWidth ()/2,mheight/6+mbitmap.getheight ()/2, Getmbitmap (). GetWidth ()/2+5, mpaint);} @Overridepublic void OnClick (View v) {//TODO auto-generated method stubif (isclickable) {if (listener!=null) { Listener.onclick ();}}} public void SetBitmap (Bitmap mbitmap) {newvalue=mscreenwidth<=mscreenheight?mscreenwidth/3/3*2:mscreenheight/3/ 3*2; Bitmap mb=imageutils.zipimage (Mbitmap,newvalue, NewValue); Mpaint.reset (); Mpaint.setantialias (true); SetmBitmap (MB );p ostinvalidate ();} public void setbackground (int id) {newvalue=mscreenwidth<=mscreenheight?mscreenwidth/3/3*2:mscreenheight/3/3*2 ;D rawable mddrawable=getresources (). getdrawable (ID); Bitmap Bitmap=imageutils.drawabletobitmap (mddrawable); Bitmap mb=imageutils.zipimage (Bitmap,newvalue, NewValue); Mpaint.reset (); Mpaint.setantialias (true); SetmBitmap (MB) ;p ostinvalidate ();} public interface onphotolistener{void Onopencamera (); void Onopenpictures (); voiD OnClick (); void OnCancel ();} Private Onphotolistener listener;private int mscreenwidth;private int mscreenheight;public Onphotolistener GetListener () {return listener;} public void Setlistener (Onphotolistener listener) {This.listener = Listener;}}

Import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.canvas;import Android.graphics.lineargradient;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.porterduff.mode;import Android.graphics.porterduffxfermode;import Android.graphics.Rect;import Android.graphics.rectf;import Android.graphics.shader.tilemode;import android.graphics.drawable.BitmapDrawable; Import Android.graphics.drawable.colordrawable;import Android.graphics.drawable.drawable;import Android.graphics.drawable.transitiondrawable;import android.widget.imageview;/** * Image processing Tool class * @author LanYan * */public Class Imageutils {/*** * How to get rounded pictures * @param bitmap * @param roundpx * @return */public static bitmap Getroundedcorne Rbitmap (Bitmap bitmap,float roundpx) {Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), Conf Ig.      argb_8888);      Canvas canvas = new canvas (output);      final int color = 0xff424242; Final Paint PaiNT = new Paint ();      Final rect rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());      Final RECTF RECTF = new RECTF (rect);      Paint.setantialias (TRUE);      Canvas.drawargb (0, 0, 0, 0);      Paint.setcolor (color);      Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);      Paint.setxfermode (New Porterduffxfermode (mode.src_in));      Canvas.drawbitmap (Bitmap, rect, rect, paint);      return output; }/** * Get Image method with Reflection * @param bitmap * @return */public static bitmap Createreflectionimagewithorigin (bitmap bitmap)      {Final int reflectiongap = 4;      int width = bitmap.getwidth ();      int height = bitmap.getheight ();      Matrix matrix = new Matrix ();      Matrix.prescale (1,-1);      Bitmap reflectionimage = Bitmap.createbitmap (bitmap,0, HEIGHT/2, Width, HEIGHT/2, matrix, false);      Bitmap bitmapwithreflection = Bitmap.createbitmap (width, (height + height/2), config.argb_8888);      Canvas canvas = new canvas (bitmapwithreflection); Canvas.drawbItmap (bitmap, 0, 0, NULL);      Paint Deafalutpaint = new paint ();      Canvas.drawrect (0, Height,width,height + reflectiongap, deafalutpaint);      Canvas.drawbitmap (reflectionimage, 0, height + reflectiongap, null);      Paint paint = new paint (); LinearGradient shader = new LinearGradient (0, Bitmap.getheight (), 0, Bitmapwithreflection.getheight () + Reflectiongap      , 0x70ffffff, 0X00FFFFFF, Tilemode.clamp);      Paint.setshader (shader);       Set the Transfer mode to being Porter Duff and destination in Paint.setxfermode (new Porterduffxfermode (mode.dst_in)); Draw a rectangle using the paint with our linear gradient canvas.drawrect (0, height, width, Bitmapwithreflec      Tion.getheight () + reflectiongap, paint);      return bitmapwithreflection; }/** * drawable turn Bitmap * @param drawable * @return */public static Bitmap drawabletobitmap (drawable drawable) {BITMAPD rawable BD = (bitmapdrawable) Drawable;return bd.getbitmap ();} /** * Bitmap Turn drawable * @param drawable * @return * * * @SuppressWarnings ("deprecation") public static drawable bitmaptodrawable (Bitmap Bitmap) { bitmapdrawable bd = new bitmapdrawable (bitmap); return BD;}            /*** * Image Scaling Method * * @param bgimage *: Source Picture Resource * @param newwidth *: Width after zoom * @param newheight * : scaled height * @return */public static Bitmap zipimage (Bitmap bgimage, double newwidth,double newheight) {//Get this picture width and height float width = bgimage.getwidth (); Float height = bgimage.getheight ();//Create an action picture with the Matrix object matrix matrix = new Matrix ();//Calculate the wide high zoom rate F Loat ScaleWidth = ((float) newwidth)/Width;float ScaleHeight = ((float) newheight)/height;//zoom picture Action Matrix.postscale (SC Alewidth, ScaleHeight); Bitmap Bitmap = Bitmap.createbitmap (bgimage, 0, 0, (int) width, (int) height, matrix, true); return Bitmap;} /** * @author Sunglasses * @category picture Loading effect * @param imageView * @param bitmap */public static void Fadeindisplay (I Mageview ImageView, Bitmap Bitmap) {//The current popular gradient effect final transitiondrawable transitiondrawable = New Transitiondrawable (new drawable[] {transparent_drawable, new bitmapdrawable (Imageview.getresources (), Bitmap)});  Imageview.setimagedrawable (transitiondrawable);  Transitiondrawable.starttransition (500); } private static final colordrawable transparent_drawable = new Colordrawable (Android. R.color.transparent); }

Imageutil Toolkit class has an individual method of testing the call, the actual demo is useless

Source:http://download.csdn.net/detail/anddroid_lanyan/8807533

Android Avatar Modification

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.