如果要把图片设置成圆形,用到的依赖:implementation'com.github.bumptech.glide:glide:4.8.0'
1.定义一个ImageView作为显示头像的组件
2.找到控件id并设置一个基础图片并设置点击事件,用Glide将图片设置成圆形:
RequestOptions requestOptions = RequestOptions.bitmapTransform(new CircleCrop());
Glide.with(this).load(R.drawable.ic_launcher_background).apply(requestOptions).into(img);
img.setOnClickListener(this);
3.图片点击弹出popupWindow设置:
弹窗后外部变暗设置
4.拍照截取图片:
public void openCamera(Activity activity) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},1);
}
//获取系統版本
currentapiVersion = Build.VERSION.SDK_INT;
// Intent调用相机
Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 判断存储卡是否可以用,可用进行存储
if (hasSdcard()) {
SimpleDateFormat timeStampFormat =new SimpleDateFormat(
"yyyy_MM_dd_HH_mm_ss");
String filename = timeStampFormat.format(new Date());
tempFile =new File(Environment.getExternalStorageDirectory(),
filename +".jpg");
if (currentapiVersion <24) {
// 从文件中创建uri
imageUri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
}else {
//兼容android7.0 使用共享文件的形式
ContentValues contentValues =new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA,tempFile.getAbsolutePath());
//检查是否有存储权限,如果没有就申请权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
return;
}
imageUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
}
}
// 开启一个带有返回值的Activity,记住这里的请求码,待会得判断请求码
activity.startActivityForResult(intent,0x001);
}
/*
* 判断有没有使用sd卡
*/
public static boolean hasSdcard() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
5.从本地文件获取图片
private void openPhones() {
// 图库选择
// 激活系统图库,选择一张图片
Intent intent_gallery =new Intent(Intent.ACTION_PICK);
intent_gallery.setType("image/*");
//记住这里的请求码,待会得判断请求码
startActivityForResult(intent_gallery,0x003);
}
6.重写activity回调方法
@Override
protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data) {
switch (requestCode) {
case 0x001:
if (resultCode ==RESULT_OK) {
//开启剪裁并返回图片
Intent intent =new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri,"image/*");
intent.putExtra("scale",true);
intent.putExtra("crop",true);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,0x002);
}
break;
case 0x002:
if (resultCode ==RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
RequestOptions requestOptions = RequestOptions.bitmapTransform(new CircleCrop());
Glide.with(this).load(bitmap).apply(requestOptions).into(img);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
case 0x003:
if (data ==null) {
return;
}else {
Uri galleryUri = data.getData();
try {
// 将地址中的图片读取为galleryBitmap
InputStream is = getContentResolver().openInputStream(
galleryUri);
Bitmap galleryBitmap = BitmapFactory.decodeStream(is);
is.close();
RequestOptions requestOptions = RequestOptions.bitmapTransform(new CircleCrop());
Glide.with(this).load(galleryBitmap).apply(requestOptions).into(img);
}catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
根据请求码判断是从哪请求过来的,要做什么操作:
注意:Android 6.0 一下会烂!!!