首先缕棵,大家要了解頭像上傳需要怎么做性锭。第一赠潦。我們要選擇圖片或者從照相機照一張。第二草冈,需要對頭像圖片進行處理她奥,第三就是要把頭像顯示到ImageButton上了瓮增。
1.布局文件
<RelativeLayout
? ? xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context=".MainActivity" >
? ? ? ? android:id="@+id/iv_head"
? ? ? ? android:layout_width="120dp"
? ? ? ? android:layout_height="120dp"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_marginTop="10dp"
? ? ? ? android:background="@null"
? ? ? ? android:scaleType="fitXY"
? ? ? ? android:src="@drawable/ic_launcher_background" />
</RelativeLayout>
2.MainActivity
public class MainActivityextends AppCompatActivityimplements View.OnClickListener {
private ImageButtonivHead;
private Buttonbtn_picture,btn_photo,btn_cancle;
private Bitmaphead;// 頭像Bitmap
? ? @SuppressLint("SdCardPath")
private static Stringpath ="/sdcard/myHead/";// sd路徑
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivHead = (ImageButton) findViewById(R.id.iv_head);
ivHead.setOnClickListener(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Bitmap bt = BitmapFactory.decodeFile(path +"head.jpg");// 從Sd中找頭像,轉(zhuǎn)換成Bitmap
? ? ? ? if (bt !=null) {
@SuppressWarnings("deprecation")
Drawable drawable =new BitmapDrawable(toRoundBitmap(bt));// 轉(zhuǎn)換成drawable
? ? ? ? ? ? ivHead.setImageDrawable(drawable);
}else {
/**
* 如果SD里面沒有則需要從服務(wù)器取頭像哩俭,取回來的頭像再保存在SD中
*
*/
? ? ? ? }
}
public void onClick(View v) {
showDialog();
}
private void showDialog() {
View view = getLayoutInflater().inflate(R.layout.photo_choose_dialog,null);
final Dialog dialog =new Dialog(this, R.style.transparentFrameWindowStyle);
dialog.setContentView(view,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Window window = dialog.getWindow();
// 設(shè)置顯示動畫
? ? ? ? window.setWindowAnimations(R.style.main_menu_animstyle);
WindowManager.LayoutParams wl = window.getAttributes();
wl.x =0;
wl.y = getWindowManager().getDefaultDisplay().getHeight();
// 以下這兩句是為了保證按鈕可以水平滿屏
? ? ? ? wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
// 設(shè)置顯示位置
? ? ? ? dialog.onWindowAttributesChanged(wl);
// 設(shè)置點擊外圍解散
? ? ? ? dialog.setCanceledOnTouchOutside(true);
dialog.show();
btn_picture = (Button) window.findViewById(R.id.btn_picture);
btn_photo = (Button) window.findViewById(R.id.btn_photo);
btn_cancle = (Button) window.findViewById(R.id.btn_cancle);
btn_picture.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View v) {
Intent intent1 =new Intent(Intent.ACTION_PICK,null);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
startActivityForResult(intent1,1);
dialog.dismiss();
}
});
btn_photo.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View v) {
Intent intent2 =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"head.jpg")));
startActivityForResult(intent2,2);// 采用ForResult打開
? ? ? ? ? ? ? ? dialog.dismiss();
}
});
btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View v) {
dialog.dismiss();
}
});
}
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode ==RESULT_OK) {
cropPhoto(data.getData());// 裁剪圖片
? ? ? ? ? ? ? ? }
break;
case 2:
if (resultCode ==RESULT_OK) {
File temp =new File(Environment.getExternalStorageDirectory() +"/head.jpg");
cropPhoto(Uri.fromFile(temp));// 裁剪圖片
? ? ? ? ? ? ? ? }
break;
case 3:
if (data !=null) {
Bundle extras = data.getExtras();
head = extras.getParcelable("data");
if (head !=null) {
/**
* 上傳服務(wù)器代碼
*/
? ? ? ? ? ? ? ? ? ? ? ? setPicToView(head);// 保存在SD卡中
? ? ? ? ? ? ? ? ? ? ? ? ivHead.setImageBitmap(toRoundBitmap(head));// 用ImageView顯示出來
? ? ? ? ? ? ? ? ? ? }
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
};
/**
* 調(diào)用系統(tǒng)的裁剪
*
? ? * @param uri
? ? */
? ? public void cropPhoto(Uri uri) {
Intent intent =new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri,"image/*");
intent.putExtra("crop","true");
// aspectX aspectY 是寬高的比例
? ? ? ? intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
// outputX outputY 是裁剪圖片寬高
? ? ? ? intent.putExtra("outputX",150);
intent.putExtra("outputY",150);
intent.putExtra("return-data",true);
startActivityForResult(intent,3);
}
private void setPicToView(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {// 檢測sd是否可用
? ? ? ? ? ? return;
}
FileOutputStream b =null;
File file =new File(path);
file.mkdirs();// 創(chuàng)建文件夾
? ? ? ? String fileName =path +"head.jpg";// 圖片名字
? ? ? ? try {
b =new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG,100, b);// 把數(shù)據(jù)寫入文件
? ? ? ? }catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
// 關(guān)閉流
? ? ? ? ? ? ? ? b.flush();
b.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 把bitmap轉(zhuǎn)成圓形
* */
? ? public Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int r =0;
// 取最短邊做邊長
? ? ? ? if (width < height) {
r = width;
}else {
r = height;
}
// 構(gòu)建一個bitmap
? ? ? ? Bitmap backgroundBm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// new一個Canvas绷跑,在backgroundBmp上畫圖
? ? ? ? Canvas canvas =new Canvas(backgroundBm);
Paint p =new Paint();
// 設(shè)置邊緣光滑,去掉鋸齒
? ? ? ? p.setAntiAlias(true);
RectF rect =new RectF(0,0, r, r);
// 通過制定的rect畫一個圓角矩形凡资,當(dāng)圓角X軸方向的半徑等于Y軸方向的半徑時砸捏,
// 且都等于r/2時,畫出來的圓角矩形就是圓形
? ? ? ? canvas.drawRoundRect(rect, r /2, r /2, p);
// 設(shè)置當(dāng)兩個圖形相交時的模式隙赁,SRC_IN為取SRC圖形相交的部分垦藏,多余的將被去掉
? ? ? ? p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// canvas將bitmap畫在backgroundBmp上
? ? ? ? canvas.drawBitmap(bitmap,null, rect, p);
return backgroundBm;
}
}
3.photo_choose_dialog.xml
? <LinearLayout?
????android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:background="#00000000"
? ? android:gravity="bottom"
? ? android:orientation="vertical"
? ? android:padding="5dip" >
? ? ? ? android:id="@+id/btn_picture"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:background="@drawable/photo_gallery_selector"
? ? ? ? android:paddingBottom="10dip"
? ? ? ? android:paddingTop="10dip"
? ? ? ? android:text="圖庫"
? ? ? ? android:textSize="16sp" />
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0.5dip"
? ? ? ? android:background="#DAD9DB" />
? ? ? ? android:id="@+id/btn_photo"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:background="@drawable/photo_camera_selector"
? ? ? ? android:paddingBottom="10dip"
? ? ? ? android:paddingTop="10dip"
? ? ? ? android:text="拍照"
? ? ? ? android:textSize="16sp" />
? ? ? ? android:id="@+id/btn_cancle"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_marginTop="5dip"
? ? ? ? android:background="@drawable/photo_cancel_selector"
? ? ? ? android:paddingBottom="10dip"
? ? ? ? android:paddingTop="10dip"
? ? ? ? android:text="取消"
? ? ? ? android:textSize="16sp" />
</LinearLayout>
4.在res目錄下創(chuàng)建anim文件夾,在anim下寫photo_dialog_in_anim.xml伞访,photo_dialog_out_anim.xml
photo_dialog_in_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
? ? <translate
? ? ? ? android:duration="500"
? ? ? ? android:fromXDelta="0"
? ? ? ? android:fromYDelta="1000"
? ? ? ? android:toXDelta="0"
? ? ? ? android:toYDelta="0" />
</set>
photo_dialog_out_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
????<translate
? ? ? ? android:duration="500"
? ? ? ? android:fromXDelta="0"
? ? ? ? android:fromYDelta="0"
? ? ? ? android:toXDelta="0"
? ? ? ? android:toYDelta="1000" />
</set>
5.在values的styles中加入
<style name="transparentFrameWindowStyle" parent="android:style/Theme.Dialog">
? ? <item name="android:windowBackground">@drawable/photo_choose_bg></item>
</style>
<style name="main_menu_animstyle">
<item name="android:windowEnterAnimation">@anim/photo_dialog_in_anim</item>
<item name="android:windowExitAnimation">@anim/photo_dialog_out_anim</item>
</style>