public class SelectPicPopupWindow extends PopupWindow { PRivate Button takePhotoBtn, pickPhotoBtn, cancelBtn; private View mMenuView; @SuppressLint("InflateParams") public SelectPicPopupWindow(Context context, OnClickListener itemsOnClick) { super(context); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.layout_dialog_pic, null); takePhotoBtn = (Button) mMenuView.findViewById(R.id.takePhotoBtn); pickPhotoBtn = (Button) mMenuView.findViewById(R.id.pickPhotoBtn); cancelBtn = (Button) mMenuView.findViewById(R.id.cancelBtn); // 设置点击监听 cancelBtn.setOnClickListener(itemsOnClick); pickPhotoBtn.setOnClickListener(itemsOnClick); takePhotoBtn.setOnClickListener(itemsOnClick); this.setContentView(mMenuView); this.setWidth(LayoutParams.MATCH_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setFocusable(true); this.setAnimationStyle(R.style.PopupAnimation); ColorDrawable dw = new ColorDrawable(0x80000000); this.setBackgroundDrawable(dw); mMenuView.setOnTouchListener(new OnTouchListener() { @Override @SuppressLint("ClickableViewaccessibility") public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss(); } } return true; } }); }}二、自定义布局layout_dialog_pic
<LinearLayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#ffffff" android:gravity="center_horizontal" android:orientation="vertical" > <Button android:id="@+id/takePhotoBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="10dp" android:padding="10dp" android:text="相机" /> <Button android:id="@+id/pickPhotoBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="5dp" android:padding="10dp" android:text="图库" /> <Button android:id="@+id/cancelBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="20dp" android:padding="10dp" android:text="取消" /> </LinearLayout></RelativeLayout>三、具体使用(Button点击调用)@OnClick (R.id.iv_makeupinfo_image1)public void onClick(View view) { postflage = "1"; menuWindow = new SelectPicPopupWindow(getapplicationContext(), itemsOnClick); menuWindow.showAtLocation((rlAll), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);}//为弹出窗口实现监听类private View.OnClickListener itemsOnClick = new View.OnClickListener() { @Override public void onClick(View v) { // 隐藏弹出窗口 menuWindow.dismiss(); switch (v.getId()) { case R.id.takePhotoBtn:// 拍照 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); // 判断存储卡是否可以用,可用进行存储 if (hasSdcard()) { intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory(), PHOTO_FILE_NAME))); } startActivityForResult(intent, PHOTO_REQUEST_CAMERA); break; case R.id.pickPhotoBtn:// 相册选择图片 // 激活系统图库,选择一张图片 Intent intent1 = new Intent(Intent.ACTION_PICK); intent1.setType("image/*"); startActivityForResult(intent1, PHOTO_REQUEST_GALLERY); break; case R.id.cancelBtn:// 取消 break; default: break; } }};//是否有内存卡private boolean hasSdcard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; }}//返回的结果@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PHOTO_REQUEST_GALLERY) { if (data != null) { // 得到图片的全路径 Uri uri = data.getData(); crop(uri); } } else if (requestCode == PHOTO_REQUEST_CAMERA) { if (hasSdcard()) { tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME); crop(Uri.fromFile(tempFile)); } else { Toast.makeText(getApplicationContext(), "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT).show(); } } else if (requestCode == PHOTO_REQUEST_CUT) { try { //说明从这里获得了bitmap bitmap = data.getParcelableExtra("data"); //设置上了,说明bitmap获取到了 ivMakeupinfoImage1.setImageBitmap(bitmap); ivMakeupinfoImage1.setScaleType(ImageView.ScaleType.FIT_XY); } catch (Exception e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data);}//剪切图片private void crop(Uri uri) { // 裁剪图片意图 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); // 裁剪后输出图片的尺寸大小 intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); // 图片格式 intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true);// 取消人脸识别 intent.putExtra("return-data", true);// true:不返回uri,false:返回uri startActivityForResult(intent, PHOTO_REQUEST_CUT);}
新闻热点
疑难解答