Android to achieve similar change QQ avatar function (image clipping)

Source: Internet
Author: User

Now almost all of the apps have user login module, need to set the user picture, and about the user's avatar part is undoubtedly a headache, most of the application of the Avatar part will have two ways: one is to use the system's cutting function to get the user's avatar, One is to get the address of a picture or photo to customize the Avatar clipping interface. And I'm here to provide a way to use the system's cutting function to get the avatar's writing, of course, I wrote this just to achieve a simple function, relatively simple. If everyone has a better writing can communicate with me.

Not a JB, first look at the effect:


The first is to open the System album, Implementing the Code:

Intent Intent = new Intent (Intent.action_pick, null); Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CON TENT _uri, "image/*"); Startactivityforresult (intent,2);

The returned intent data is obtained by the Onactivityresult method:

@Override     protected void onactivityresult (int requestcode, int  resultcode, intent data)  {        // todo  Auto-generated method stub         Super.onactivityresult (Requestcode, resultcode, data);         switch  (Requestcode)  {        case 2:             if  (layout_pop != null)  {                 layout_ Pop.dismiss ();            }             Uri uri = null;             if  (data == null)  {                 return;            }             if  (RESULTCODE == RESULT_OK)  {                 if  (! Environment.getexternalstoragestate (). Equals (                         environment.media_mounted) )  {                     toast.maketext (this,  "SD not Available", 1). Show ();                     return;                 }                 uri = data.getdata ();                 startimageaction (uri, 200, 200,3, true);             } else {                 toast.maketext (this,  "Photo acquisition Failed", 1). Show ();            }             break;             case 3:                 if  (layout_pop != null)  {             &nBsp;       layout_pop.dismiss ();                 }                 if  (data == null)  {                     return;                 } else {                      savecropavator (data);                 }                 break;        default:             break;        }    } 

  Image Clipping implementation code:

Private void startimageaction (uri uri, int outputx, int outputy,             int requestCode, boolean  Iscrop)  {        Intent intent = null;         if  (Iscrop)  {             intent = new intent ("Com.android.camera.action.CROP");         } else {             intent = new intent (intent.action_get_content, null);         }        intent.setdataandtype (URI,   "image/*");         intent.putextra ("Crop",  "true");        &Nbsp;intent.putextra ("Aspectx",  1);         intent.putextra (" Aspecty ",  1);         intent.putextra (" OutputX ",  outputX);         intent.putextra ("Outputy",  outputy);         intent.putextra ("scale",  true);         intent.putextra (Mediastore.extra_output, uri);         Intent.putextra ("Return-data",  true);         intent.putextra (" OutputFormat ",  bitmap.compressformat.jpeg.tostring ());         Intent.putextra ("Nofacedetection",  true); // no face detection         startactivityforresult (intent, requestcode);     }

After you crop the avatar, you also need to turn the picture into a rounded corner, with the following method:

/**     *  turn a picture into a fillet      *       *  @param  bitmap     *              original bitmap pictures      *  @param  pixels      *             the radian of the picture fillet (in pixels (px))       *  @return   Pictures with rounded corners (bitmap  type)      */     public static bitmap toroundcorner (Bitmap bitmap, int pixels)  {        bitmap output = bitmap.createbitmap ( Bitmap.getwidth (),                 bitmap.getheight (),  config.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);        final  Float roundpx = pixels;        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;     }

  Last saved Image:

 /**     *  Save the cropped avatar      *       *  @param  data     */    private void  savecropavator (Intent data)  {        Bundle  Extras = data.getextras ();        if  (extras !=  null)  {            bitmap bitmap  = extras.getparcelable ("Data");             LOG.I ("Life",  "avatar - bitmap = "  + bitmap);             if  (bitmap != null)  {                 bitmap = toroundcorner (bitmap, 10);//Call Round Corner processing method                  Headimageview.setimagebitmap (bitmap);                 if  (bitmap != null && bitmap.isrecycled ())  {                      bitmap.recycle ();                 }            }         }    }


Android to achieve similar change QQ avatar function (image clipping)

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.