1

I have set bitmap in imageview and doing operation rotating and dragging bitmap on touch mutiple times. I want bitmap set in imageview should not go outside from particular region in imageview.

Is there any way to achieve this.

Please help me for this..

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        scaleCount=scaleCount+scale;
        angleCount = addAngle(angleCount, Math.toDegrees(angle));       

        if (!isInitialized) {
            int w = getWidth();
            int h = getHeight();
            position.set(w / 2, h / 2);
            isInitialized = true;
        }

        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        //Log.v("Height and Width", "Height: "+ getHeight() + "Width: "+ getWidth());

        transform.reset();
        transform.postTranslate(-width / 2.0f, -height / 2.0f);
        transform.postRotate((float) Math.toDegrees(angle));
        transform.postScale(scale, scale);
        transform.postTranslate(position.getX(), position.getY());

        rectangleF = new RectF();
        transform.mapRect(rectangleF);
        //Log.v("VM", "Rect " + rectangleF.left + " " + rectangleF.top + " " + rectangleF.right + " " + rectangleF.bottom);

        canvas.drawBitmap(bitmap, transform, paint);
        canvas.restore();
        BitmapWidth=BitmapWidth+bitmap.getScaledWidth(canvas);
        BitmapHeight=BitmapHeight+bitmap.getScaledHeight(canvas);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        vca = null;
        vcb = null;
        vpa = null;
        vpb = null;

        x = event.getX();
        y = event.getY();

        try {
            touchManager.update(event);

            if (touchManager.getPressCount() == 1) {
                vca = touchManager.getPoint(0);
                vpa = touchManager.getPreviousPoint(0);

                position.add(touchManager.moveDelta(0));
            }
            else {
                if (touchManager.getPressCount() == 2) {
                    vca = touchManager.getPoint(0);
                    vpa = touchManager.getPreviousPoint(0);
                    vcb = touchManager.getPoint(1);
                    vpb = touchManager.getPreviousPoint(1);

                    VMVector2D current = touchManager.getVector(0, 1);
                    VMVector2D previous = touchManager.getPreviousVector(0, 1);
                    float currentDistance = current.getLength();
                    float previousDistance = previous.getLength();

                    if (currentDistance-previousDistance != 0) {
                        scale *= currentDistance / previousDistance;
                    }
                    angle -= VMVector2D.getSignedAngleBetween(current, previous);
                    /*angleCount=angleCount+angle;*/
                }
            }
            invalidate();
        }
        catch(Exception exception) {
        //  Log.d("VM", exception.getMessage());
        }
        return true;
    }
Ajay S
  • 48,003
  • 27
  • 91
  • 111

1 Answers1

0

Set canvas clipRect. Next draw calls will modify canvas only inside specified area. See: Using clipRect - explanation

Community
  • 1
  • 1
Leonidos
  • 10,482
  • 2
  • 28
  • 37