Android选择和设置图片

一、自定义SelectPicPopupWindow类

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;
    }
}
//返回的结果
@Override
public 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");
    // 裁剪框的比例,11
    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:不返回urifalse:返回uri
    startActivityForResult(intent, PHOTO_REQUEST_CUT);
}







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值