Android Imitation micro-letter QQ Set graphics head cutting function _android

Source: Internet
Author: User
Tags getcolor int size static class stub throwable

Recently in the graduation design, want to have a function and QQ can be cropped head and set round head, forehead, this is a trend of design lions.

And throughout the current mainstream app, as long as there is user system This function, this demand is generally in (BU) Rob (de) difficult (bu) escape (xue)!

There are two ways to realize picture clipping, one is to use the system's own cutting tools, one is to use open Source Tool cropper. This section will bring you how to use the system with the cutting tool to cut the picture ~

Let's start with a simple run diagram.

Well, simply put, I will be the code written as a small demo to share to everyone, at the end of the article will be attached GitHub link, the need to download their own ~

Here is a simple analysis of the implementation of ideas, we first photos can certainly be photographed and selected from the album, this can be sent to the system specific intent, response to the corresponding system program, and then in the Onactivityresult inside, access to our data can be. In Onactivityresult, we can get two forms of data, Bitmap and URI. Under normal circumstances we will not choose Bitmap, because we all know that our mobile phone inside the photos are too big ~ forced to use bitmap, I can only say you, dick cock cock, sorry, I said is not 666, is silly cock meaning!

hahaha, let me explode a foul language, I originally was refused ~ Just hope to watch the article you, then use URI it ~

So what then? Of course, it is trimmed, finished after this trimmed bitmap object set to ImageView, save up, upload to the server.

A general understanding of the process, then we look directly at the code ~ ~

First look at our circular image bar, I am a bit messy, because I have considered a lot of logic, so I made some corrections, this circular image believes that there will be a lot of online.

Package com.example.nanchen.cropimagetest;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.Config;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.ColorMatrix;
Import Android.graphics.ColorMatrixColorFilter;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff.Mode;
Import Android.graphics.PorterDuffXfermode;
Import Android.graphics.Rect;
Import Android.graphics.RectF;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.widget.ImageView;
/** * @author Nanchen * @fileName cropimagetest * @packageName com.example.nanchen.cropimagetest * @date 2016/10/13 15:09
* * public class Roundimageview extends ImageView {/** * circular imageview, you can set up to two different widths and different colors of a round border. * @author Alan */private static class Imageview_level {public final static int level0 = 0; public final static int Lev
EL1 = 1; Public final Static int level2 = 2;
Public final static int level3 = 3;
Public final static int level4 = 4;
Private context Mcontext;
private int circlecolor = Color.White;
private int circlewidth = 0;
private int mlevel = Imageview_level.level1; public void Setlevel (int level) {mlevel = level;} public Roundimageview (context context) {super (context); Mcontext = Co
ntext Public Roundimageview (context, AttributeSet attrs) {Super (context, attrs); mcontext = context; Setcustomattribu
TES (ATTRS); Public Roundimageview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); mcontext = C
Ontext;
Setcustomattributes (ATTRS); } private void Setcustomattributes (AttributeSet attrs) {TypedArray a = Mcontext.obtainstyledattributes (Attrs,
R.styleable.roundedimageview);
int width = a.getdimensionpixelsize (r.styleable.roundedimageview_border_thickness, 0);
Setpadding (width, width, width, width); Mlevel = A.getint (R.styleable.roundedimageview_image_mode, Imageview_level.leveL1);
Circlecolor = A.getcolor (R.styleable.roundedimageview_border_color, Circlecolor); @Override public void Setimagebitmap (Bitmap BM) {switch (this.mlevel) {Case IMAGEVIEW_LEVEL.LEVEL1:BM = Roundbitmap (
BM); Case Imageview_level.level2:if (getpaddingleft () = = Getpaddingright ()) && (getpaddingleft () = = Getpaddingbottom ()) && (getpaddingleft () = = Getpaddingtop ()) {this.circlewidth = Getpaddingleft (); BM = ROUNDBI
TMap (BM);
} break;
Case IMAGEVIEW_LEVEL.LEVEL3:BM = Chamferbitmap (BM);
Break Case Imageview_level.level4:if (getpaddingleft () = = Getpaddingright ()) && (getpaddingleft () = = Getpaddingbottom ()) && (getpaddingleft () = = Getpaddingtop ()) {this.circlewidth = Getpaddingleft (); BM = ROUNDBI
TMap (BM);
} break;
Default:break;
Super.setimagebitmap (BM); } @Override protected void OnDraw (Canvas Canvas) {switch (this.mlevel) {case Imageview_level.level2:if (circlewidth) ; 0) {Drawcircleborder (canvas, getwidth ()-this.circlewidth*2 +Circlewidth)/2, This.circlecolor, GetWidth (), GetHeight (), this.circlewidth);
} break; Case Imageview_level.level4:if (Circlewidth > 0) {int paddingwidth = circlewidth; Drawcircleborder (canvas, GetWidth (
-paddingwidth*2 +CIRCLEWIDTH/2)/2, This.circlecolor, GetWidth (), GetHeight (), This.circlewidth/2,color.dkgray);
int tempwidth = CIRCLEWIDTH/2; Drawcircleborder (Canvas, (getwidth ()-paddingwidth*2 +tempwidth)/2, This.circlecolor, GetWidth (), GetHeight (),
Tempwidth,color.dkgray);
} break;
Default:break;
} super.ondraw (canvas); /** * Bitmap cut into round * * @param bitmap incoming Bitmap object * @return/Private bitmap Roundbitmap (bitmap bitmap) {bitmap resultbitm
AP = null;
Canvas Canvas = null;
int width = bitmap.getwidth ();
int height = bitmap.getheight ();
float ROUNDPX;
Float left, top, right, bottom, Dst_left, Dst_top, Dst_right, Dst_bottom; if (width <= height) {roundpx = WIDTH/2 top = 0; bottom = width, left = 0, right = width; height = width; dst_left
= 0;
dst_top = 0; Dst_rigHT = width;
Dst_bottom = width; else {roundpx = HEIGHT/2; float clip = (width-height)/2; left = clip, right = width-clip, top = 0; bottom = Hei
Ght
width = height;
Dst_left = 0;
dst_top = 0;
Dst_right = height;
Dst_bottom = height; } if (width <= 0) {width = 1;} if (height <= 0) {height = 1;} try {resultbitmap = Bitmap.createbitmap (width, he
ight, config.argb_4444); catch (Throwable e) {e.printstacktrace ();} try {canvas = new canvas (RESULTBITMAP);} catch (Throwable e) {E.printsta
Cktrace ();
final int color = color.red;
Final Paint Paint = new Paint ();
Final Rect src = new Rect (int) left, (int) top, (int) right, (int) bottom);
Final Rect DST = new Rect (int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
Final RECTF RECTF = new RECTF (DST);
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, SRC, DST, paint);
return resultbitmap; /** * Bitmap chamfer * * @param bitmap incoming Bitmap object * @return/Private bitmap Chamferbitmap (bitmap bitmap) {bitmap resultbitm
AP = null;
Canvas Canvas = null;
int width = bitmap.getwidth ();
int height = bitmap.getheight ();
float ROUNDPX;
Float left, top, right, bottom, Dst_left, Dst_top, Dst_right, Dst_bottom; if (width <= height) {roundpx = dip2px (This.mcontext, 4);//8 Pixel Chamfer 4 is dp value top = 0; bottom = width; left = 0; right = WI
Dth
Height = width;
Dst_left = 0;
dst_top = 0;
Dst_right = width;
Dst_bottom = width; else {roundpx = dip2px (This.mcontext, 4);//8 Pixel Chamfer 4 is DP value float clip = (width-height)/2; left = clip; right = width
-Clip;
top = 0;
bottom = height;
width = height;
Dst_left = 0;
dst_top = 0;
Dst_right = height;
Dst_bottom = height; } if (width <= 0) {width = 1;} if (height <= 0) {height = 1;} try {resultbitmap = Bitmap.createbitmap (width, he
ight, config.argb_4444);
catch (Throwable e) {e.printstacktrace ();}try {canvas = new canvas (RESULTBITMAP);} catch (Throwable e) {e.printstacktrace ();} final int color = color.red;
Final Paint Paint = new Paint ();
Final Rect src = new Rect (int) left, (int) top, (int) right, (int) bottom);
Final Rect DST = new Rect (int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
Final RECTF RECTF = new RECTF (DST);
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, SRC, DST, paint);
return resultbitmap; /** * Canvas drawing circle/private void Drawcircleborder (Canvas Canvas, int radius, int color, int width, int height, int circlewidth {Paint Paint = new Paint ();/* antialiasing/Paint.setantialias (TRUE); Paint.setfilterbitmap (true); Paint.setdither (true); Pai
Nt.setcolor (color);
/* Set paint style for STROKE: Hollow */Paint.setstyle (Paint.Style.STROKE);
/* Set the frame width of paint/paint.setstrokewidth (circlewidth); CaNvas.drawcircle (WIDTH/2, HEIGHT/2, radius, paint); private void Drawcircleborder (Canvas Canvas, int radius, int color, int width, int height, int circlewidth,int shadowcol Olor (0x09ffffff & Shadowcolor); Sets the alpha value for (int i=0;i<circlewidth*2;i++)//down angle offset projection how many times the shadow layer {canvas.drawcircle (width/2+i, HEIGHT/2, radius+2,
Paint);
} canvas.restore ();
Paint = new paint ();
/* antialiasing/Paint.setantialias (TRUE);
Paint.setfilterbitmap (TRUE);
Paint.setdither (TRUE);
Paint.setcolor (color);
/* Set paint style for STROKE: Hollow */Paint.setstyle (Paint.Style.STROKE); /* Set the frame width of paint/paint.setstrokewidth (circlewidth);
One-second entity canvas.drawcircle (WIDTH/2, HEIGHT/2, radius, paint); public void setcirclewidth (int padding) {setpadding (padding, padding, padding, padding);} public int Getcirclecolor ()
{return circlecolor;} public void Setcirclecolor (int circlecolor) {This.ciRclecolor = Circlecolor;
///Setimagebitmap After completion of the execution;
public int getcirclewidth () {return this.circlewidth.} public Ontouchlistener Ontouchlistener = new Ontouchlistener () {
@Override public boolean Ontouch (view view, Motionevent event) {switch (event.getaction ()) {case MOTIONEVENT.ACTION_UP:
Changelight ((imageview) view, 0);
OnClick break;
Case MotionEvent.ACTION_DOWN:changeLight ((imageview) view,-60);
Break
Case Motionevent.action_move://Changelight ((imageview) view, 0);
Break
Case MotionEvent.ACTION_CANCEL:changeLight ((imageview) view, 0);
Break
Default:break;
return false;
}
}; public void Setcolorfilter (Boolean value) {if (value) {Setontouchlistener (ontouchlistener);}
else{setontouchlistener (null);} private void Changelight (ImageView imageview, int brightness) {ColorMatrix matrix = new ColorMatrix (); Matrix.set (New Flo
At[] {1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0}); Imageview.setcolorfilter (New ColormatrixcolorfIlter (matrix)); /** * According to the resolution of the mobile phone from the DP unit to become PX (pixel) * * * Private int dip2px (context, float Dpvalue) {final float scale = context.ge
Tapplicationcontext (). Getresources (). Getdisplaymetrics (). density;
return (int) (Dpvalue * scale + 0.5f); }
}

Customize an iOS-like pop-up box

Package com.example.nanchen.cropimagetest;
Import android.app.Activity;
Import Android.app.Dialog;
Import Android.os.Bundle;
Import Android.text.TextUtils;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.view.ViewGroup.LayoutParams;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import java.util.List; /** * Dialog * * */public Class Selectdialog extends dialog implements Onclicklistener,onitemclicklistener {private Selec
Tdialoglistener Mlistener;
Private activity mactivity;
Private Button Mmbtn_cancel;
Private TextView Mtv_title;
Private list<string> Mname;
Private String Mtitle;
Private Boolean musecustomcolor = false; private int Mfirstitemcolor;
private int motheritemcolor;  Public interface Selectdialoglistener {public void Onitemclick (adapterview<?> parent, view view, int position, long
ID);
Private Selectdialogcancellistener Mcancellistener;  Public interface Selectdialogcancellistener {public void Oncancelclick (View v); int theme, Selectdialoglistener listener,list<string> names) {Super (activity, theme); mactivity = activity; mliste
NER = listener;
This.mname=names;
Set whether to click the periphery to dissolve setcanceledontouchoutside (true); /** * @param activity invokes the activity of the pop-up menu * @param theme Theme * @param listener menu Item Click event * @param cancellistener Cancel Event * @param Names menu Item Name * * */public selectdialog Theme,selectdialoglistener Listener,
Selectdialogcancellistener Cancellistener,list<string> names) {Super (activity, theme); mactivity = activity;
Mlistener = listener;
Mcancellistener = Cancellistener;
This.mname=names;
Set whether to click the periphery does not dissolve setcanceledontouchoutside (false); }
/** * @param activity invoke pop-up menu activity * @param theme Theme * @param listener menu Item Click event * @param names menu item name * @param title menu title text * */Public Selectdialog (activity activity, int theme,selectdialoglistener listener,list<string> names,string Tit 
Le) {super (activity, theme); mactivity = activity; mlistener = listener; this.mname=names; mtitle = title;//Set whether click the periphery to dissolve
Setcanceledontouchoutside (TRUE); "Public selectdialog" (activity activity, int Theme,selectdialoglistener Listener,selectdialogcancellistener Cancellistener,list<string> names,string title) {Super (activity, theme); mactivity = activity; Mlistener = Listene
R
Mcancellistener = Cancellistener;
This.mname=names;
Mtitle = title;
Set whether click on the periphery can dissolve setcanceledontouchoutside (true);
} @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
View view = Getlayoutinflater (). Inflate (r.layout.view_dialog_select, NULL); Setcontentview (view, New Layoutparams (Layoutparams.fill_parent, Layoutparams.
Wrap_content));
window window = GetWindow ();
Set Display animation window.setwindowanimations (R.style.main_menu_animstyle);
Windowmanager.layoutparams wl = Window.getattributes ();
wl.x = 0;
Wl.y = Mactivity.getwindowmanager (). Getdefaultdisplay (). GetHeight ();
The following two sentences are to ensure that the button can be horizontally full screen wl.width = layoutparams.match_parent;
Wl.height = layoutparams.wrap_content;
Set the display position onwindowattributeschanged (WL);
Setcanceledontouchoutside (FALSE);
Initviews ();
private void Initviews () {dialogadapter dialogadapter=new dialogadapter (mname);
ListView dialoglist= (ListView) Findviewbyid (r.id.dialog_list);
Dialoglist.setonitemclicklistener (this);
Dialoglist.setadapter (Dialogadapter);
Mmbtn_cancel = (Button) Findviewbyid (r.id.mbtn_cancel);
Mtv_title = (TextView) Findviewbyid (r.id.mtv_title);
Mmbtn_cancel.setonclicklistener (this); Mmbtn_cancel.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {//TODO Auto-generat Ed method stub if (Mcancellistener!= null) {Mcancellistener.oncanceLclick (v);
} dismiss ();
}
}); if (! Textutils.isempty (mtitle) && mtv_title!= null) {mtv_title.setvisibility (view.visible); Mtv_title.settext (
Mtitle);
}else{mtv_title.setvisibility (view.gone);} @Override public void OnClick (view v) {dismiss ();} @Override public void Onitemclick (adapterview<?> parent, view V iew, int position, long id) {Mlistener.onitemclick (parent, view, position, id); dismiss ();} Private Class Dialogadapter Extends Baseadapter {private list<string> mstrings; private Viewholder viewholder; private Layoutinflater Layoutin
Flater; Public Dialogadapter (list<string> strings) {this.mstrings = strings; this.layoutinflater=
Mactivity.getlayoutinflater (); @Override public int GetCount () {//TODO auto-generated a stub return Mstrings.size ();} @Override public Object g Etitem (int position) {//TODO auto-generated Method stub return Mstrings.get (position);} @Override public Long Getitemid (int position) {//TODO auto-generated method stub
return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {if (null = = Convertview) {viewholder=
New Viewholder ();
Convertview=layoutinflater.inflate (R.layout.view_dialog_item, NULL);
viewholder.dialogitembutton= (TextView) Convertview.findviewbyid (R.ID.DIALOG_ITEM_BT);
Convertview.settag (Viewholder); }else{viewholder= (Viewholder) Convertview.gettag (); Viewholder.dialogItemButton.setText (Mstrings.get (position))
; if (!musecustomcolor) {mfirstitemcolor = Mactivity.getresources (). GetColor (r.color.dialog_blue); MOtherItemColor =
Mactivity.getresources (). GetColor (R.color.dialog_blue);
} if (1 = = Mstrings.size ()) {Viewholder.dialogItemButton.setTextColor (mfirstitemcolor);
Viewholder.dialogItemButton.setBackgroundResource (r.drawable.dialog_item_bg_only);
else if (position = 0) {viewholder.dialogItemButton.setTextColor (mfirstitemcolor);
Viewholder.dialogItemButton.setBackgroundResource (R.drawable.dialog_item_bg_top); else if (position = MStRings.size ()-1) {Viewholder.dialogItemButton.setTextColor (motheritemcolor);
Viewholder.dialogItemButton.setBackgroundResource (R.drawable.dialog_item_bg_buttom);
else {viewholder.dialogItemButton.setTextColor (motheritemcolor);
Viewholder.dialogItemButton.setBackgroundResource (R.drawable.dialog_item_bg_center);
return convertview; The public static class Viewholder {public TextView Dialogitembutton}/** * Sets the text color of the list item/public void Setitemcolor (int
Firstitemcolor, int otheritemcolor) {mfirstitemcolor = Firstitemcolor; motheritemcolor = Otheritemcolor;
Musecustomcolor = true; }
}

Because the picture file must be in the album, you may also be in the file system other places to choose, here I use the universal adapter written before!

Because the landlord time is really tight, so the code is directly copy up, some places did not do complete optimization also hope everyone forgive me!

And look at the code and layout of the activity.

Package com.example.nanchen.cropimagetest;
Import Android.content.ComponentName;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnCancelListener;
Import android.content.Intent;
Import Android.content.pm.ResolveInfo;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.CompressFormat;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import Android.provider.MediaStore.Images.Media;
Import Android.support.v7.app.AlertDialog;
Import Android.support.v7.app.AlertDialog.Builder;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Com.example.nanchen.cropimagetest.SelectDialog.SelectDialogListener;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List; public class Mainactivity extends Appcompatactivity {private Roundimageview mheadimage; @OveRride protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Mheadimage = (Roundimageview) Findviewbyid (r.id.main_roundimage); Private final int photo_picked_from_camera = 1; Used to identify avatar from system photography private final int photo_picked_from_file = 2;
Used to identify from albums get avatar private final int crop_from_camera = 3;
private void Geticonfromphoto () {Intent Intent = new Intent (intent.action_get_content, Media.external_content_uri);
Intent.settype ("image/*");
Startactivityforresult (Intent, photo_picked_from_file); private void Selectphoto () {list<string> List = new arraylist<> (); List.add ("photo"); List.add ("album"); showdial OG (new Selectdialoglistener () {@Override public void Onitemclick (adapterview<?> parent, view view, int position, lo ng ID) {switch (position) {case 0:geticonfromcamera (), break, Case 1:geticonfromphoto ();//Get Break from system album; Default:bre
Ak
}}},list); Private Uri Imguri; Because the Android phone's picture is basically very big, so suggestUsing URIs instead of bitmap/** * Call system camera Photo/private void Geticonfromcamera () {Intent Intent = new Intent (mediastore.action_image_cap
ture); Imguri = Uri.fromfile (New File (Environment.getexternalstoragedirectory (), "Avatar_" +string.valueof (
System.currenttimemillis ()) + ". png");
Intent.putextra (Mediastore.extra_output,imguri);
Startactivityforresult (Intent,photo_picked_from_camera); Private Selectdialog ShowDialog (Selectdialoglistener Listener, list<string> List) {selectdialog dialog = new Sele
Ctdialog (this, r.style.transparentframewindowstyle,listener,list);
Dialog.show ();
return dialog; /** * Attempt to crop the picture/private void Docrop () {final arraylist<cropoption> cropoptions = new arraylist<> (); final in
Tent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.settype ("image/*");
list<resolveinfo> list = Getpackagemanager (). Queryintentactivities (intent,0);
int size = List.size ();
if (size = = 0) {Uiutil.showtoast (this, "do not currently support cropping picture!");
Return
} intent.setdata (Imguri); Intent. Putextra ("Outputx", 300);
Intent.putextra ("Outputy", 300);
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Intent.putextra ("scale", true);
Intent.putextra ("Return-data", true);
Only one if (size = = 1) {Intent intent1 = new Intent (Intent);
Resolveinfo res = list.get (0);
Intent1.setcomponent (New ComponentName (Res.activityinfo.packagename,res.activityinfo.name));
Startactivityforresult (Intent1,crop_from_camera); }else {//Many app for Resolveinfo res:list {cropoption co = new Cropoption (), Co.title = Getpackagemanager (). Get
Applicationlabel (Res.activityInfo.applicationInfo);
Co.icon = Getpackagemanager (). Getapplicationicon (Res.activityInfo.applicationInfo);
Co.appintent = new Intent (Intent);
Co.appIntent.setComponent (New ComponentName (Res.activityinfo.packagename,res.activityinfo.name));
Cropoptions.add (CO); } commonadapter<cropoption> adapter = new Commonadapter<cropoption> (this,cropoptions,r.layout.layout_ Crop_selector) {@Override public void convert (VieWholder holder, cropoption item) {holder.setimagedrawable (R.id.iv_icon,item.icon); Holder.settext (R.id.tv_name,
Item.title);
}
};
Alertdialog.builder Builder = new Builder (this);
Builder.settitle ("Choose a App"); Builder.setadapter (Adapter, new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface
dialog, int which) {Startactivityforresult (Cropoptions.get (which). Appintent,crop_from_camera);}}); Builder.setoncancellistener (New Oncancellistener () {@Override public void OnCancel (Dialoginterface dialog) {if (Imguri
!= null) {getcontentresolver (). Delete (imguri,null,null); Imguri = null;}}
});
Alertdialog dialog = Builder.create ();
Dialog.show (); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (reque
Stcode, ResultCode, data); if (ResultCode!= result_ok) {return;} switch (Requestcode) {case Photo_picked_from_camera:docrop ();
Picked_from_file:imguri = Data.getdata (); TodoCrop ();
Break
Case CROP_FROM_CAMERA:IF (data!= null) {setcropimg (data);
Default:break; } private void Setcropimg (Intent picdata) {Bundle Bundle = Picdata.getextras (); if (Bundle!= null) {Bitmap Mbitmap = bu
Ndle.getparcelable ("Data");
Mheadimage.setimagebitmap (MBITMAP);
Savebitmap (Environment.getexternalstoragedirectory () + "/crop_" +system.currenttimemillis () + ". png", mBitmap);
} private void Savebitmap (String filename,bitmap Bitmap) {File File = new file (fileName);
FileOutputStream fout = null;
try {file.createnewfile (); fout = new FileOutputStream (file); bitmap.compress (compressformat.png,100,fout);
Fout.flush ();
catch (IOException e) {e.printstacktrace ();} finally {try {if (fout!=null) {fout.close ()} Uiutil.showtoast (Mainactivity.this, "Save success!") ");
}
catch (IOException e) {e.printstacktrace ();}} public void Btnclick (view view) {Selectphoto ();}}

Layout:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://" Schemas.android.com/apk/res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:id=" @+id/ Activity_main "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
tools: context= "Com.example.nanchen.cropimagetest.MainActivity" >
< Com.example.nanchen.cropimagetest.RoundImageView
android:id= "@+id/main_roundimage"
android:layout_ Width= "100DP"
android:layout_height= "100DP"
android:layout_alignparenttop= "true"
android:layout _centerinparent= "true"
android:src= "@drawable/default_avatar"/>
<button android:id=
"@+id/ Main_btn "
android:layout_width=" match_parent "
android:layout_height=" Wrap_content "
android: layout_below= "@+id/main_roundimage"
android:onclick= "Btnclick"
android:text= "set Avatar"/>
Relativelayout>

The above is a small series to introduce the Android imitation micro-letter QQ Set graphics head cutting function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.