美文网首页
Android仿IOS选择拍照相册底部弹出框

Android仿IOS选择拍照相册底部弹出框

作者: HOLLE_karry | 来源:发表于2020-12-07 19:39 被阅读0次
弹出框.jpg

1.依赖

dependencies {
    compile 'com.guoqi.widget:actionsheet:1.0'
}

2.代码

//1.实现接口
 implements ActionSheet.OnActionSheetSelected

//2.在某个点击事件中添加:
  ActionSheet.showSheet(this, this, null);

//3.然后重写点击方法:
@Override
    public void onClick(int whichButton) {
        switch (whichButton) {
            case ActionSheet.CHOOSE_PICTURE:
                //相册
                choosePic();
                break;
            case ActionSheet.TAKE_PICTURE:
                //拍照
                takePic();
                break;
            case ActionSheet.CANCEL:
                //取消
                break;
        }
    } 

//加入自己的逻辑
//去寻找是否已经有了相机的权限
        if (ContextCompat.checkSelfPermission(MineValidateActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
            //Toast.makeText(MainActivity.this,"您申请了动态权限",Toast.LENGTH_SHORT).show();
            //如果有了相机的权限有调用相机
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            // 判断存储卡是否可以用,可用进行存储
            if (hasSdcard()) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(), ".jpg")));
            }
            startActivityForResult(intent, 2);
        } else {
            //否则去请求相机权限
            ActivityCompat.requestPermissions(MineValidateActivity.this, new String[]{Manifest.permission.CAMERA}, 100);
            //如果有了相机的权限有调用相机
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            // 判断存储卡是否可以用,可用进行存储
            if (hasSdcard()) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(), ".jpg")));
            }
            startActivityForResult(intent, 2);
        }

//加入自己的逻辑
private void choosePic() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 1);
    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Uri data1 = data.getData();
            mIvImg1.setImageURI(data1);
            if (ActionSheet.){

            }
        }
        if (requestCode == 2 && resultCode == RESULT_OK) {
            if (hasSdcard()) {
                File tempFile = new File(Environment.getExternalStorageDirectory(), ".jpg");
                Uri uri = Uri.fromFile(tempFile);
                mIvImg2.setImageURI(uri);
            } else {
                Toast.makeText(MineValidateActivity.this, "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean hasSdcard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

3.源码

public class ActionSheet {

    public static final int CHOOSE_PICTURE = 100;
    public static final int TAKE_PICTURE = 200;
    public static final int CANCEL = 300;

    public interface OnActionSheetSelected {
        void onClick(int whichButton);
    }

    public ActionSheet() {
    }

    public static Dialog showSheet(Context context, final OnActionSheetSelected actionSheetSelected,
                                   OnCancelListener cancelListener) {
        final Dialog dialog = new Dialog(context, R.style.ActionSheet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.view_action_sheet, null);
        final int cFullFillWidth = 10000;
        layout.setMinimumWidth(cFullFillWidth);

        TextView picTextView = (TextView) layout.findViewById(R.id.picTextView);
        TextView camTextView = (TextView) layout.findViewById(R.id.camTextView);
        TextView cancelTextView = (TextView) layout.findViewById(R.id.cancelTextView);


        picTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                actionSheetSelected.onClick(CHOOSE_PICTURE);
                dialog.dismiss();
            }
        });

        camTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                actionSheetSelected.onClick(TAKE_PICTURE);
                dialog.dismiss();
            }
        });
        cancelTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                actionSheetSelected.onClick(CANCEL);
                dialog.dismiss();
            }
        });


        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;
        final int cMakeBottom = -1000;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.BOTTOM;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);
        if (cancelListener != null)
            dialog.setOnCancelListener(cancelListener);

        dialog.setContentView(layout);
        dialog.show();

        return dialog;
    }

}

相关文章

网友评论

      本文标题:Android仿IOS选择拍照相册底部弹出框

      本文链接:https://www.haomeiwen.com/subject/wfxuwktx.html