想要编辑图像中的裁剪形状

时间:2016-10-08 06:54:01

标签: android canvas crop custom-view

我正在开发Android项目,我在自定义视图中裁剪图像并成功获得裁剪后的图像。现在我想编辑绘制的形状或按我的意愿移动该形状。我想通过触摸和拖动完美边界来编辑绘制的形状。

这是我的Cropping

我正在接受Result这样的

现在我想在image上移动形状线或在图像上拖动线

这是我的自定义视图代码

public class SomeView extends View implements OnTouchListener {
    private Paint paint;
    public static List<Point> points;
    int DIST = 2;
    boolean flgPathDraw ;//= true;
    public Uri orignalUri;
    float origianlheight=0.0f,originalwidth=0.0f,heightratio=0.0f,widthratio=0.0f;
    public static int REQUEST_CODE=2;
    Point mfirstpoint = null;
    boolean bfirstpoint = false;

    Point mlastpoint = null;
    boolean cropflag=false;

    Bitmap bitmap = null;//BitmapFactory.decodeResource(getResources(),R.drawable.bg_crop);
    Context mContext;


    public void setBitmap(Bitmap bmp,Uri uri){
        orignalUri=uri;
        points = new ArrayList<Point>();
        setFocusable(true);
        setFocusableInTouchMode(true);

        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        origianlheight=bmp.getHeight();
        originalwidth=bmp.getWidth();
        bitmap=bmp;
        if (originalwidth > origianlheight) {

            Matrix matrix = new Matrix();
            matrix.postRotate(90);

            bmp = Bitmap.createBitmap(bmp, 0, 0,
                    bmp.getWidth(), bmp.getHeight(), matrix,
                    true);
        }
        //if(origianlheight>originalwidth){
            heightratio=height/origianlheight;
      //  }else{
            widthratio=width/originalwidth;
      //  }
        bitmap=Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth()*widthratio),(int)(bmp.getHeight()*widthratio), true);

     // bitmap=bmp;
      paint = new Paint(Paint.ANTI_ALIAS_FLAG);
      paint.setStyle(Paint.Style.STROKE);
      paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
      paint.setStrokeWidth(5);
      paint.setColor(Color.WHITE);

      flgPathDraw = true;
      this.setOnTouchListener(this);
      points = new ArrayList<Point>();
      bfirstpoint = false;
      cropflag=false;

    }
    public void clear(){
         setFocusable(true);
         setFocusableInTouchMode(true);
         paint = new Paint(Paint.ANTI_ALIAS_FLAG);
         paint.setStyle(Paint.Style.STROKE);
         paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
         paint.setStrokeWidth(5);
         paint.setColor(Color.WHITE);
         this.setOnTouchListener(this);
         points = new ArrayList<Point>();

         bfirstpoint = false;
         flgPathDraw = true;
         cropflag=false;
         invalidate();
    }

    public SomeView(Context c) {
        super(c);

        mContext = c;
        setFocusable(true);
        setFocusableInTouchMode(true);

       /* DisplayMetrics metrics = c.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        origianlheight=bitmap.getHeight();
        originalwidth=bitmap.getWidth();
        //if(origianlheight>originalwidth){
            heightratio=height/origianlheight;
      //  }else{
            widthratio=width/originalwidth;
      //  }
        bitmap=Bitmap.createScaledBitmap(bitmap, (int)(originalwidth*widthratio),(int)(origianlheight*widthratio), true);*/



        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        paint.setStyle(Paint.Style.STROKE);
        paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
        paint.setStrokeWidth(5);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(this);
        points = new ArrayList<Point>();

        bfirstpoint = false;
       // flgPathDraw = true;
        cropflag=false;
    }

    public SomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setFocusable(true);
        setFocusableInTouchMode(true);

       /* DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        origianlheight=bitmap.getHeight();
        originalwidth=bitmap.getWidth();
        //if(origianlheight>originalwidth){
            heightratio=height/origianlheight;
      //  }else{
            widthratio=width/originalwidth;
      //  }
        bitmap=Bitmap.createScaledBitmap(bitmap, (int)(originalwidth*widthratio),(int)(origianlheight*widthratio), true);*/

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
        paint.setStrokeWidth(5);
        paint.setColor(Color.WHITE);
        this.setOnTouchListener(this);
        points = new ArrayList<Point>();
        bfirstpoint = false;
       // flgPathDraw = true;
        cropflag=false;


    }

    public void onDraw(Canvas canvas) {
        if(bitmap!=null)
            canvas.drawBitmap(bitmap, 0, 0,paint);

        Path path = new Path();
        boolean first = true;

        for (int i = 0; i < points.size(); i += 2) {
            Point point = points.get(i);
            if (first) {
                first = false;
                path.moveTo(point.x, point.y);
            } else if (i < points.size() - 1) {
                Point next = points.get(i + 1);
                path.quadTo(point.x, point.y, next.x, next.y);
            } else {
                mlastpoint = points.get(i);
                path.lineTo(point.x, point.y);
            }
        }
        canvas.drawPath(path, paint);
    }
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);

        Point point = new Point();
        point.x = (int) event.getX();
        point.y = (int) event.getY();

        if (flgPathDraw) {

            if (bfirstpoint) {

                if (comparepoint(mfirstpoint, point)) {
                    // points.add(point);
                    points.add(mfirstpoint);
                        flgPathDraw = false;
                                     //  showcropdialog();
                        cropflag=true;
                } else {
                    points.add(point);
                }
            } else {
                points.add(point);
            }

            if (!(bfirstpoint)) {

                mfirstpoint = point;
                bfirstpoint = true;
            }
        }

        invalidate();
        Log.e("Hi  ==>", "Size: " + point.x + " " + point.y);

        if (event.getAction() == MotionEvent.ACTION_UP) {
            Log.d("Action up*******~~~~~~~>>>>", "called");
            mlastpoint = point;
            if (flgPathDraw) {
                if (points.size() > 12) {
                    if (!comparepoint(mfirstpoint, mlastpoint)) {
                        flgPathDraw = false;
                        points.add(mfirstpoint);
                       // showcropdialog();
                        cropflag=true;

                    }
                }
            }
        }

        return true;
    }

    private boolean comparepoint(Point first, Point current) {
        int left_range_x = (int) (current.x - 3);
        int left_range_y = (int) (current.y - 3);

        int right_range_x = (int) (current.x + 3);
        int right_range_y = (int) (current.y + 3);

        if ((left_range_x < first.x && first.x < right_range_x)
                && (left_range_y < first.y && first.y < right_range_y)) {
            if (points.size() < 10) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }

    }

    public void fillinPartofPath() {
        Point point = new Point();
        point.x = points.get(0).x;
        point.y = points.get(0).y;

        points.add(point);
        invalidate();
    }

    public void resetView() {
        points.clear();
        paint.setColor(Color.WHITE);
        paint.setStyle(Style.STROKE);
        flgPathDraw = true;
        invalidate();
    }

  /*  private void showcropdialog() {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent;
                switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    // Yes button clicked
                    // bfirstpoint = false;

                    intent = new Intent(mContext, CropActivity.class);
                    intent.putExtra("crop", true);
                    intent.putExtra("heightratio", heightratio);
                    intent.putExtra("widthratio", widthratio);
                    intent.putExtra("URI", orignalUri.toString());
                    mContext.startActivity(intent);
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    // No button clicked

                    intent = new Intent(mContext, CropActivity.class);
                    intent.putExtra("crop", false);
                    intent.putExtra("heightratio", heightratio);
                    intent.putExtra("widthratio", widthratio);
                    intent.putExtra("URI", orignalUri.toString());
                    mContext.startActivity(intent);

                    bfirstpoint = false;
                    // resetView();

                    break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("Do you Want to save Crop or Non-crop image?")
                .setPositiveButton("Crop", dialogClickListener)
                .setNegativeButton("Non-crop", dialogClickListener).show()
                .setCancelable(false);
    }*/

    public void Crop(){
        if(cropflag){
            new SwichTask().execute();
        }
    }

    ProgressDialog progressDialog;

    private void showProgressDialog() {
        if (progressDialog != null)
            progressDialog.show();
        else
            progressDialog = ProgressDialog.show(mContext, null,"Cropping");
    }

    private void dismissProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }


    private class SwichTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showProgressDialog();

        }
        @Override
        protected Void doInBackground(Void... params) {
            System.out.println("Switch Activity is call");

            Intent intent;
            intent = new Intent(mContext, CropActivity.class);
            intent.putExtra("crop", true);
            intent.putExtra("heightratio", heightratio);
            intent.putExtra("widthratio", widthratio);
            intent.putExtra("URI", orignalUri.toString());
            ((Activity)mContext).startActivityForResult(intent,REQUEST_CODE);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            dismissProgressDialog();
        }
    }


   }

谢谢

0 个答案:

没有答案