12.android 简单的(6.0、7.0、8.0)相机拍照、相册选图、剪裁。

12.android 简单的(6.0、7.0、8.0)相机拍照、相册选图、剪裁。_第1张图片

//第一步 权限:

 



//第二步 依赖,我使用了一个圆形图片库: 

//圆形图片
implementation 'de.hdodenhof:circleimageview:2.1.0'

//第三步 我的Activity布局,就是一个圆形图片:

 




    


//第四步 Activity代码:

 

public class MainPopActivity extends AppCompatActivity implements View.OnClickListener {
    //相册请求码
    private static final int ALBUM_REQUEST_CODE = 1;
    //相机请求码
    private static final int CAMERA_REQUEST_CODE = 2;
    //剪裁请求码
    private static final int CROP_SMALL_PICTURE = 3;
    //调用照相机返回图片文件
    private File tempFile;
    //最后显示的图片文件
    private String mFile;

    private Button mPhotograph;
    private Button mAlbum;
    private Button mCancel;
    private PopupWindow window;
    private CircleImageView mCircleImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_pop);
        popUp();
        initView();
        readPictures(); //读取图片,读取上次保存的图片
    }

    //初始化控件,图片点击事件
    private void initView() {
        mCircleImageView = (CircleImageView) findViewById(R.id.mCircleImageView);
        mCircleImageView.setOnClickListener(this);
        mPhotograph.setOnClickListener(this);
        mAlbum.setOnClickListener(this);
        mCancel.setOnClickListener(this);
    }

    //读取图片,设置了图片之后,下次再进来还会有,读取上次保存的图片
    private void readPictures() {
        Bitmap bitmap = BitmapFactory.decodeFile(getPath());
        if (null != bitmap) {
            mCircleImageView.setImageBitmap(bitmap);
        } else {
            mCircleImageView.setImageResource(R.mipmap.ic_launcher);
        }
    }

    //初始化popwindow
    private void popUp() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
        window = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPhotograph = inflate.findViewById(R.id.mPhotograph);
        window.setAnimationStyle(R.style.popwin_anim_style);//动画
        mAlbum = inflate.findViewById(R.id.mAlbum);
        mCancel = inflate.findViewById(R.id.mCancel);
//        window.setBackgroundDrawable(new BitmapDrawable());//设置PopupWindow的背景
//        window.setOutsideTouchable(false);
        window.setFocusable(true);

        //设置背景颜色变正常
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });
    }

    @Override
    public void onClick(View v) {
        // 设置背景颜色变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.7f;
        getWindow().setAttributes(lp);

        switch (v.getId()) {
            case R.id.mCircleImageView: //图片点击事件,打开popwindow
                //第一个参数是要将PopupWindow放到的View,第二个参数是位置,第三第四是偏移值
                window.showAtLocation(mCircleImageView, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.mPhotograph://拍照
                getPicFromCamera();
                window.dismiss();
                break;
            case R.id.mAlbum://相册
                getPicFromAlbm();
                window.dismiss();
                break;
            case R.id.mCancel://取消
                window.dismiss();
                break;
        }
    }

    //拍照和选择相册的回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CAMERA_REQUEST_CODE:   //调用相机后返回
                if (resultCode == RESULT_OK) {
                    //用相机返回的照片去调用剪裁也需要对Uri进行处理
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        Uri contentUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", tempFile);
                        startPhotoZoom(contentUri);//开始对图片进行裁剪处理
                    } else {
                        startPhotoZoom(Uri.fromFile(tempFile));//开始对图片进行裁剪处理
                    }
                }
                break;
            case ALBUM_REQUEST_CODE:    //调用相册后返回
                if (resultCode == RESULT_OK) {
                    Uri uri = data.getData();
                    startPhotoZoom(uri); // 开始对图片进行裁剪处理
                }
                break;
            case CROP_SMALL_PICTURE:  //调用剪裁后返回
                Bitmap bitmap = BitmapFactory.decodeFile(getPath());
                if (null != bitmap) { // 让刚才选择裁剪得到的图片显示在界面上,清晰
                    mCircleImageView.setImageBitmap(bitmap);
                } else {
                    if (data != null) {// 让刚才选择裁剪得到的图片显示在界面上,模糊
                        Bitmap photo = BitmapFactory.decodeFile(mFile);
                        mCircleImageView.setImageBitmap(photo);
                    } else {
                        Log.e("data", "data=null");
                    }
                }
                break;
        }
    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    protected void startPhotoZoom(Uri uri) {
        if (uri == null) {
            Log.i("tag", "The uri is not exist.");
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 720);
        intent.putExtra("outputY", 720);
        //设置了true的话直接返回bitmap,可能会很占内存
        intent.putExtra("return-data", false);
        //不启用人脸识别
        intent.putExtra("noFaceDetection", true);
        File out = new File(getPath());
        if (!out.getParentFile().exists()) {
            out.getParentFile().mkdirs();
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
        startActivityForResult(intent, CROP_SMALL_PICTURE);
    }

    //裁剪后的地址,保存到本地
    public String getPath() {
        if (mFile == null) {
            mFile = Environment.getExternalStorageDirectory() + "/" + "MyPicture/" + "temp.png";
        }
        return mFile;
    }

    /**
     * 从相机获取图片
     */
    private void getPicFromCamera() {
        //用于保存调用相机拍照后所生成的文件
//        tempFile = new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".png");
        tempFile = new File(Environment.getExternalStorageDirectory().getPath(), "/temp.png");
        //跳转到调用系统相机
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //判断版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {   //如果在Android7.0以上,使用FileProvider获取Uri
            intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", tempFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
            Log.e("getPicFromCamera", contentUri.toString());
        } else {    //否则使用Uri.fromFile(file)方法获取Uri
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        }
        startActivityForResult(intent, CAMERA_REQUEST_CODE);
    }

    /**
     * 从相册获取图片
     */
    private void getPicFromAlbm() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
    }

}

 

//第五步 我的popwindow布局 pop_layout.xml: 

 





    

//第六步 在res下新建anim,在anim下建了两个动画xml文件:

bottom_in.xml:



    
    

bottom_out.xml:



    

    

//第七步 在res下,values下,style里 添加popwindow动画style:

//第八步 在drawable下新建了三个选择圆角背景xml布局文件:

 bottom_pop_button_bg.xml:



       
        
            
            
        
    
      
        
            
            
        
    

cancel_button_bg.xml:



       
        
        
        
        
    
      
        
            
            
        
    

top_pop_button_bg.xml:



       
        
            
            
        
    
      
        
            
            
        
    

 

//第九步 Android7.0及以上的需要设置FileProvider,在res下新建xml文件夹,在xml下新建 file_paths.xml:



    
    

//第十步 在AndroidManifest.xml里注册清单文件,在application下注册,再替换你的包名: 



    

//-------------------------------------------------------------------------------完-----------------------------------------------------------------------------

你可能感兴趣的:(android效果,第三方效果,android)