File path= new File(Environment.getExternalStorageDirectory().getPath() ,"/xiaofei.png");
在一个点击事件中创建PopupWindow:
//PopupWindow View contentView = View.inflate(getActivity(), R.layout.mine_pop_layout,null); PopupWindow window = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //思考:点击外部不消失 /* * new BitmapDrawable():就是一个透明的背景 */ //如果想要设置点击外部窗口消失,,必须2个方法同时使用 window.setBackgroundDrawable(new BitmapDrawable());//给窗口设置一个完全透明的背景图片 window.setOutsideTouchable(true);//设置窗口外部可以触摸 //思考:窗口里面的控件没有响应 //使用一个setFocusable 就可以实现,,,但是一般这2个方法也会同时使用 window.setFocusable(true);//设置窗口的焦点事件 window.setTouchable(true);//设置窗口本身可以触摸 //如果想要使用窗口内部的控件,,,必须通过他的视图对象去找 Button pop_btn_camera = contentView.findViewById(R.id.pop_btn_camera); Button pop_btn_pick = contentView.findViewById(R.id.pop_btn_pick); //点击相机 调取相机 pop_btn_camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //打开相机 MediaStore.ACTION_IMAGE_CAPTURE 打开相机的Action Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //在Sdcard 中创建文件 存入图片 it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(path)); //1.意图 2.requestCode 请求码 startActivityForResult(it, 1000); } }); //点击相册 pop_btn_pick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调取系统的相册 Intent.ACTION_PICK相册 Intent it = new Intent(Intent.ACTION_PICK); //设置格式 it.setType("image/*"); startActivityForResult(it, 10000); } }); //window位置 window.showAtLocation(contentView, Gravity.BOTTOM, 0, 0); //进行显示 //window.showAsDropDown(btns);//显示在某控件的正左下方 //window.showAsDropDown(btns, -150, 200);//显示在某控件的左下方(带偏移量)
mine_pop_layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/pop_btn_camera" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="调取相机" /> <Button android:id="@+id/pop_btn_pick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="调取相册" /> </LinearLayout>
//回调 重新回到Activity时 会被调用 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //当拍完照以后点击完成 会执行 onActivityResult 方法 调取裁剪功能 if(requestCode == 1000 && resultCode == getActivity().RESULT_OK){ //调取裁剪功能 om.android.camera.action.CROP 裁剪的Action Intent it = new Intent("com.android.camera.action.CROP"); //得到图片设置类型 it.setDataAndType(Uri.fromFile(path)), "image/*"); //是否支持裁剪 设置 true 支持 false 不支持 it.putExtra("CROP", true); //设置比例大小 1:1 it.putExtra("aspectX", 1); it.putExtra("aspectY", 1); //输出的大小 it.putExtra("outputX", 250); it.putExtra("outputY", 250); //将裁剪好的图片进行返回到Intent中 it.putExtra("return-data", true); startActivityForResult(it, 2000); } //点击完裁剪的完成以后会执行的方法 if(requestCode == 2000 && resultCode == getActivity().RESULT_OK){ Bitmap bitmap = data.getParcelableExtra("data"); head_img.setImageBitmap(bitmap); } //得到相册里的图片进行裁剪 if(requestCode == 10000 && resultCode == getActivity().RESULT_OK){ //得到相册图片 Uri uri = data.getData(); //裁剪 Intent it = new Intent("com.android.camera.action.CROP"); //设置图片 以及格式 it.setDataAndType(uri, "image/*"); //是否支持裁剪 it.putExtra("crop", true); //设置比例 it.putExtra("aspectX", 1); it.putExtra("aspectY", 1); //设置输出的大小 it.putExtra("outputX", 250); it.putExtra("outputY", 250); //是否支持人脸识别 // it.putExtra("onFaceDetection", true); //返回 it.putExtra("return-data", true); startActivityForResult(it, 20000); } //2.点击裁剪完成 if(requestCode == 20000 && resultCode == getActivity().RESULT_OK){ Bitmap bitmap = data.getParcelableExtra("data"); head_img.setImageBitmap(bitmap); } }