布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="选择头像" />
</LinearLayout>
PopupWindow的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/biankuang"
android:text="相机" />
<Button
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/camera"
android:layout_marginTop="15dp"
android:background="@drawable/biankuang"
android:text="从相册选取" />
<Button
android:id="@+id/photoJanqie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/photo"
android:layout_marginTop="15dp"
android:background="@drawable/biankuang"
android:text="从相册选取并剪切" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/photoJanqie"
android:layout_marginTop="15dp"
android:background="@drawable/biankuang"
android:text="取消" />
</RelativeLayout>
清单文件加入权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
代码:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Button button;
private View view;
private Button camera, cancel, photo,photoJanqie;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
imageView = (ImageView) findViewById(R.id.image);
button = (Button) findViewById(R.id.button);
}
private void initData() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_layout, null);
popupWindow = new PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setClippingEnabled(true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
popupWindow.dismiss();
}
});
camera = (Button) view.findViewById(R.id.camera);
cancel = (Button) view.findViewById(R.id.cancel);
photo = (Button) view.findViewById(R.id.photo);
photoJanqie = (Button) view.findViewById(R.id.photoJanqie);
popupWindow.showAsDropDown(v, 0, 0);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 【1】相机的 隐式回传意图
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 【2】添加意图
intent.addCategory("android.intent.category.DEFAULT");
// 【3】回传(注意:请求码要和判断的一样)
startActivityForResult(intent, 0);
}
});
//点击从相册获取照片
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [1]设置相册的意图(权限)
Intent intent1 = new Intent(Intent.ACTION_PICK);
// [2]设置显式MIME数据类型
intent1.setType("image/*");
// [3]跳转回传
startActivityForResult(intent1, 1);
}
});
photoJanqie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// (1)设置相册的意图(权限)
Intent intent2 = new Intent(Intent.ACTION_PICK);
// (2)设置显式MIME数据类型
intent2.setType("image/*");
//(3)跳转回传
startActivityForResult(intent2, 2);
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
//得到拍摄的照片
Bitmap bitmap = data.getParcelableExtra("data");
//设置给imageView(这个时候就完成了 获取拍照图片)
imageView.setImageBitmap(bitmap);
popupWindow.dismiss();
break;
case 1:
//得到图片路径
Uri uri = data.getData();
//设置图片(相册获取图片完毕)
imageView.setImageURI(uri);
popupWindow.dismiss();
break;
case 2://获取相测 图片裁剪
// (4)从返回值中直接获取路径
Uri uri1 = data.getData();
//(5)调用裁剪的方法
Intent crp= crop(uri1);
//(6)再次回传
startActivityForResult(crp, 3);
break;
case 3:
//(7)获取bitmap
Bitmap bmp = (Bitmap) data.getExtras().get("data");
//8(设置图片)
imageView.setImageBitmap(bmp);
break;
}
}
private Intent crop(Uri uri) {
/*
* 裁剪需要的东西
* 1.图片
* 2.裁剪框的大小 裁剪完后图片大小(我要裁成什么样子的?其实就是 裁剪完的大小)
* 3.图片格式
* 4.得到裁剪完的图片
* */
// 裁剪图片意图
Intent intent = new Intent("com.android.camera.action.CROP");
// 设置裁剪的数据源和数据类型
intent.setDataAndType(uri, "image/*");
// 可裁剪
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小(图片展示到imageView的大小,不要太大了,不然会出错)
//参数(1.输出的大小,大小)
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
// 图片格式(参数:输出格式,格式)
intent.putExtra("outputFormat", "JPEG");
// 必须加,否则返回值中找不到返回的值
intent.putExtra("return-data", true);// 若为true则表示返回数据(图片)
return intent;
}
}