首先,大家要了解头像上传需要怎么做。第一。我们要选择图片或者从照相机照一张。第二,需要对头像图片进行处理,第三就是要把头像显示到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中找头像,转换成Bitmap
if (bt !=null) {
@SuppressWarnings("deprecation")
Drawable drawable =new BitmapDrawable(toRoundBitmap(bt));// 转换成drawable
ivHead.setImageDrawable(drawable);
}else {
/**
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在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();
// 设置显示动画
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;
// 设置显示位置
dialog.onWindowAttributesChanged(wl);
// 设置点击外围解散
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) {
/**
* 上传服务器代码
*/
setPicToView(head);// 保存在SD卡中
ivHead.setImageBitmap(toRoundBitmap(head));// 用ImageView显示出来
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
};
/**
* 调用系统的裁剪
*
* @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();// 创建文件夹
String fileName =path +"head.jpg";// 图片名字
try {
b =new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG,100, b);// 把数据写入文件
}catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
// 关闭流
b.flush();
b.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 把bitmap转成圆形
* */
public Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int r =0;
// 取最短边做边长
if (width < height) {
r = width;
}else {
r = height;
}
// 构建一个bitmap
Bitmap backgroundBm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// new一个Canvas,在backgroundBmp上画图
Canvas canvas =new Canvas(backgroundBm);
Paint p =new Paint();
// 设置边缘光滑,去掉锯齿
p.setAntiAlias(true);
RectF rect =new RectF(0,0, r, r);
// 通过制定的rect画一个圆角矩形,当圆角X轴方向的半径等于Y轴方向的半径时,
// 且都等于r/2时,画出来的圆角矩形就是圆形
canvas.drawRoundRect(rect, r /2, r /2, p);
// 设置当两个图形相交时的模式,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目录下创建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>