@OnClick(R.id.information_head)
public void onClick(View v) {
switch (v.getId()) {
case R.id.information_head:
popShowFaces();
break;
}
}
private void popShowFaces() {
View mView = LayoutInflater.from(this).inflate(R.layout.pop_head_portrait, null, false);
PopupWindow mPopupWindow = new PopupWindow(mView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button icon_btn_camera = (Button) mView.findViewById(R.id.icon_btn_camera);
Button icon_btn_select = (Button) mView.findViewById(R.id.icon_btn_select);
Button icon_btn_cancel = (Button) mView.findViewById(R.id.icon_btn_cancel);
icon_btn_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//拍照处理
mPopupWindow.dismiss();
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return;
}
avatarName = Configs.HEADPORTRAIT.IMAGE_FILE_NAME + System.currentTimeMillis() + ".jpg";
avatarUrl = new File(rootFile, Configs.HEADPORTRAIT.IMAGE_FILE_NAME +
System.currentTimeMillis() + ".jpg");
//是否有文件,没有就创建
if (!rootFile.exists()) rootFile.mkdirs();
//跳转到调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//判断版本 如果在Android7.0以上,使用FileProvider获取Uri
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile
(PersonalCenterActivity.this, getPackageName(), avatarUrl);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
} else {
//否则使用Uri.fromFile(file)方法获取Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(avatarUrl));
}
//授予临时权限
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
ActivityCompat.requestPermissions(PersonalCenterActivity.this, new String[]
{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
startActivityForResult(intent, Configs.HEADPORTRAIT.REQUEST_IMAGE_CAPTURE);
}
});
icon_btn_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//相册处理
mPopupWindow.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
// 判断系统中是否有处理该 Intent 的 Activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, Configs.HEADPORTRAIT.REQUEST_IMAGE_GET);
} else {
Toast.makeText(PersonalCenterActivity.this, "未找到图片查看器", Toast.LENGTH_SHORT).show();
}
}
});
icon_btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//取消
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), Configs.HEADPORTRAIT.IMAGE_FILE_NAME)));
startActivityForResult(intent, Configs.HEADPORTRAIT.REQUEST_IMAGE_CAPTURE);
mPopupWindow.dismiss();
}
});
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.showAtLocation(mView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//设置PopWindow背景颜色。
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;
getWindow().setAttributes(lp);
//给Pop设置监听,取消Popt弹出时的背景颜色。
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int height = mView.findViewById(R.id.ll_pop).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
mPopupWindow.dismiss();
}
}
return true;
}
});
}
//处理点击回调的监听事件
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// 回调成功
if (resultCode == RESULT_OK) {
switch (requestCode) {
// 小图切割
case Configs.HEADPORTRAIT.REQUEST_SMALL_IMAGE_CUTTING:
if (data != null) {
saveImage(avatarUrl.getAbsolutePath());
ivInformationHead.setImageBitmap(BitmapFactory.decodeFile(avatarUrl.getAbsolutePath()));
}
break;
// 相册选取
case Configs.HEADPORTRAIT.REQUEST_IMAGE_GET:
try {
cropPhoto(data.getData());
} catch (NullPointerException e) {
e.printStackTrace();
}
break;
// 拍照
case Configs.HEADPORTRAIT.REQUEST_IMAGE_CAPTURE:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(
PersonalCenterActivity.this, getPackageName(), avatarUrl);
cropPhoto(contentUri);
} else {
cropPhoto(Uri.fromFile(avatarUrl));
}
break;
}
}
}
//裁剪
private void cropPhoto(Uri uri) {
File cropFile = new File(rootFile, avatarName);
if (!rootFile.exists()) rootFile.mkdirs();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropFile));
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
intent.putExtra("noFaceDetection", true);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, Configs.HEADPORTRAIT.REQUEST_SMALL_IMAGE_CUTTING);
}
// 保存图片
private void saveImage(String path) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return;
}
Bitmap bitmap = BitmapFactory.decodeFile(path);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(avatarUrl);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
//在视图中显示图片(原图显示)
Glide.with(this).load(bitmap).apply(RequestOptions.bitmapTransform(
new CircleCrop())).into(ivInformationHead);
RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), avatarUrl);
MultipartBody.Part body = MultipartBody.Part.createFormData(
"head_portrait", avatarUrl.getName(), requestFile);
Capricornus
.remoteApi()
//上传图片到服务器的接口
.avatar_upload(body)
.enqueue(new TCallback() {
@Override
public void done(Void response) {
showTip("头像更新成功!");
}
});
}