Android图片裁剪,裁剪外的区域添加蒙层

对图片的裁剪主要就是使用canvas进行处理,使用canvas.drawBitmap

drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint);

Rect src: 是对图片进行裁截,若是空null则显示整个图片

RectF dst:是图片在Canvas画布中显示的区域,
           大于src则把src的裁截区放大,
           小于src则把src的裁截区缩小。

加蒙层操作分两步,1、给整个图片会上蒙层;2、将裁剪区域贴到图片上

上代码

/**
     * 按区域裁剪图片,对框外区域添加蒙层
     * @param mBitmap 被裁剪图片
     * @param r 裁剪区域
     */
    private void savePic(Bitmap mBitmap,Rect r) {
        try {
            if (mSaving)
                return;
            mSaving = true;

            final Bitmap croppedImage;
            // If we are circle cropping, we want alpha channel, which is the
            // third param here.
            int w = mBitmap.getWidth();
            int h = mBitmap.getHeight();
            croppedImage = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
            Rect allrect = new Rect(0, 0, w, w);
            Paint paint = new Paint();
            paint.setColor(Color.GRAY);
            paint.setAlpha(90);
            Canvas canvas = new Canvas(croppedImage);
            canvas.drawBitmap(mBitmap, allrect, allrect, paint);
            canvas.drawBitmap(mBitmap, r, r, null);
            Drawable qTag = getResources().getDrawable(R.drawable.frame_q_nor);
            //可以在此绘制框的标签
            int x1  = r.left;
            int y1  = r.top;
            qTag.setBounds(x1+5, y1+5,x1 + (qTag.getIntrinsicWidth())/2,y1 + (qTag.getIntrinsicHeight())/2);
            qTag.draw(canvas);
            //保存位置
            Calendar calendar = Calendar.getInstance();//获取当前日历对象
            Date d = calendar.getTime();
            SimpleDateFormat time = new SimpleDateFormat("yyMMddHHmmssSSS");
            String timestr = time.format(d);
            final String cropPath = CameraConfig.OUTPUT_PIC_PATH + timestr + ".jpg";
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    saveDrawableToCache(croppedImage, cropPath);
                }
            });
            mSaving = false;
            imagePathsThrow += cropPath+"&";
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    /**
     * 将Bitmap放入缓存,
     *
     * @param bitmap
     * @param filePath
     * @return void
     * @Title: saveDrawableToCache
     */
    private void saveDrawableToCache(Bitmap bitmap, String filePath) {
        try {
            File f = new File(CameraConfig.OUTPUT_PIC_PATH);
            if (!f.exists() || !f.isDirectory()) {
                f.mkdirs();
            }
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            OutputStream outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


你可能感兴趣的:(Android图片裁剪,裁剪外的区域添加蒙层)