Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:08:04+00:00 2026-06-14T04:08:04+00:00

I have the following on Draw method: @Override protected void onDraw(Canvas canvas) { //

  • 0

I have the following on Draw method:

 @Override
protected void onDraw(Canvas canvas) {
    //        super.onDraw(canvas);
    canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint); //Deseneaza Bitmapul mutabil
    if (PaintActivity.isPic == 1) {
        rect = new Rect((int)mX, (int)mY, ((int)mX+bitmaps.get(lastBitmap).bitmap.getWidth()), ((int)mY+bitmaps.get(lastBitmap).bitmap.getHeight()));
        rectscale = new Rect((int)mX-20, (int)mY-20, (int)mX+20, (int)mY+20);
        rectrotate = new Rect((int)((mX+bitmaps.get(lastBitmap).bitmap.getWidth())-20), (int)mY-20, (int)(mX+bitmaps.get(lastBitmap).bitmap.getHeight()+20), (int)mY+20);
        for(int i=0;i<bitmaps.size();i++){
            if((lastBitmap != i)&& (bitmaps.get(i)!= null) ){
                if(bitmaps.get(i).isRotate){
                    matrixRotate.setRotate(bitmaps.get(i).rectrotateVal, bitmaps.get(i).pX+(bitmaps.get(i).bitmap.getWidth() / 2), bitmaps.get(i).pY+(bitmaps.get(i).bitmap.getHeight() / 2)); //rotate it
                }else{
                    matrixRotate.setRotate(0, bitmaps.get(i).pX+(bitmaps.get(i).bitmap.getWidth() / 2), bitmaps.get(i).pY+(bitmaps.get(i).bitmap.getHeight() / 2)); //rotate it
                }
                canvas.setMatrix(matrixRotate);
                canvas.drawBitmap(bitmaps.get(i).bitmap, bitmaps.get(i).pX,  bitmaps.get(i).pY, mBitmapPaint);
                canvas.setMatrix(null);
                canvas.drawCircle(bitmaps.get(i).pX, bitmaps.get(i).pY, 10, cPaint);
                canvas.drawCircle(bitmaps.get(i).pX+bitmaps.get(i).bitmap.getWidth(), bitmaps.get(i).pY, 10, cPaint);
            }
        }
        if (bitmaps.get(lastBitmap).bitmap != null) {
            bitmaps.get(lastBitmap).rectscale = rectscale;
            bitmaps.get(lastBitmap).rectrotate = rectrotate;
            if(isScalling){
                rect = new Rect((int)mX,(int) mY, (int)tempx, (int)tempy);
                int w = Math.abs(rect.width());
                int h = Math.abs(rect.height());
                canvas.drawRect(rect, rPaint);
                if((w!=0)&&(h!=0)){
                    bitmaps.get(lastBitmap).bitmap = getResizedBitmap(bitmaps.get(lastBitmap).bitmap2, h, w, bitmaps.get(lastBitmap).bitmap.getConfig());
                }
            }
            if(isRotating){
                rotateVal = (float) 45;
                bitmaps.get(lastBitmap).rectrotateVal = rotateVal;
                bitmaps.get(lastBitmap).isRotate = isRotating;
                matrixRotate.setRotate(rotateVal, mX+(bitmaps.get(lastBitmap).bitmap.getWidth() / 2), mY+(bitmaps.get(lastBitmap).bitmap.getHeight() / 2)); //rotate it
                canvas.setMatrix(matrixRotate);
                canvas.drawBitmap(bitmaps.get(lastBitmap).bitmap,  mX, mY, mBitmapPaint);
                canvas.setMatrix(null);
                canvas.drawCircle(bitmaps.get(lastBitmap).pX+bitmaps.get(lastBitmap).bitmap.getWidth(), bitmaps.get(lastBitmap).pY, 10, cPaint);
                canvas.drawRect(bitmaps.get(lastBitmap).rect, rPaint);
            }else{
                canvas.drawBitmap(bitmaps.get(lastBitmap).bitmap, mX, mY, mBitmapPaint);
                canvas.drawCircle(mX, mY, 10, cPaint);
                canvas.drawCircle(mX+bitmaps.get(lastBitmap).bitmap.getWidth(), mY, 10, cPaint);
            }
            bitmaps.get(lastBitmap).pX = mX;
            bitmaps.get(lastBitmap).pY = mY;
            bitmaps.get(lastBitmap).rect = rect;
            bitmaps.get(lastBitmap).rectscale = rectscale;
            bitmaps.get(lastBitmap).rectrotate = rectrotate;
        }
        for(int j=0;j<textArray.size();j++){
            if (textArray.get(j) != null) {
                canvas.drawText(textArray.get(j), tXArray.get(j), tYArray.get(j), textPaintArray.get(j));
            }
        }
    } 

I have a number of bitmaps, the “rect” rectangle is the same width and height of the picture, so that if i press a picture, it sets me on it, to move it.
rectscale = is a rectangle situated in the left-top corner of the picture, where i draw a circle, if i press the circle, then i can scale my picture.
rectrotate = same thing but in the right-top corner of the picture. i press it, it rotates my picture with 45degrees.
I have the following touch events:

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    canvas.drawPoint(x, y, paint);
    mX = x;
    mY = y;
    if(PaintActivity.isPic == 1){
        if(bitmaps.size() > 0){
            for(int i = 0;i<(bitmaps.size()-1);i++){
                if(bitmaps.get(i).rect.contains((int)mX,(int) mY)){
                    lastBitmap = i;
                }
            }
        }
        if(bitmaps.size() > 0){
            for(int i = 0;i<(bitmaps.size());i++){
                if(bitmaps.get(i).rectscale.contains((int)mX,(int) mY)){
                    lastBitmap = i;
                    isScalling  = true;
                    tempx = mX+bitmaps.get(lastBitmap).bitmap.getWidth();
                    tempy = mY+bitmaps.get(lastBitmap).bitmap.getHeight();
                }
            }
        }
        if(bitmaps.size() > 0){
            for(int i = 0;i<(bitmaps.size());i++){
                if(bitmaps.get(i).rectrotate.contains((int)mX,(int) mY)){
                    lastBitmap = i;
                    isRotating = true;
                }
            }
        }
    }
}

private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
        Log.d("move", "mx, my=" + mX + " " + mY);
        // mPath.moveTo(mX, mY);
    }
    // invalidate();
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    mPath.moveTo(mX, mY);
    // commit the path to our offscreen
    canvas.drawPath(mPath, paint);
    //        canvas.save();
    // kill this so we don't double draw
    mPath.reset();
    if(PaintActivity.isPic == 1){
        if(isScalling){
            isScalling = false;
        }
        if(isRotating){
            isRotating = false;
        }
        lastBitmap = bitmaps.size()-1;
        mX= bitmaps.get(lastBitmap).pX;
        mY= bitmaps.get(lastBitmap).pY;
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);

        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }
    return true;
}

If i comment the following lines:

canvas.setMatrix(matrixRotate);   

AND:

canvas.setMatrix(null);

It all works, except for the rotation. But if i don’t comment them, the rotation works, the first picture is ok, but for the rest of the pictures, the rectangles are a bit messed up. They do not start in the top-left of the bitmap (where my scale circle is), instead it starts, somewhere lower, almost at the half of the bitmap. So i can use my scale and rotate functions, but i have to guess where to put my finger, because, the point where the circle is drawn is not contained in the Rects. Any ideeas how i could fix this?

EDIT:
I tried: before setting the matrix to matrix rotate:

origMatrix = canvas.getMatrix();

and instead of:

canvas.setMatrix(null);

I tried:

canvas.setMatrix(origMatrix);

It still messed up my rectangles, but at least now it draws the circles for rotating and scalling where the rectangles are (usually somewhere in the middle of the bitmap)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:08:05+00:00Added an answer on June 14, 2026 at 4:08 am

    I used this instead:

    canvas.save();
    matrixRotate.setRotate(bitmaps.get(i).rectrotateVal, bitmaps.get(i).pX+(bitmaps.get(i).bitmap.getWidth() / 2), bitmaps.get(i).pY+(bitmaps.get(i).bitmap.getHeight() / 2));
    canvas.drawBitmap(bitmaps.get(i).bitmap, matrixRotate , mBitmapPaint);
    canvas.restore();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have OnPaint method overrided to draw an Ellipse on the screen. protected override
I have the following code for writing draw calls to a back buffer canvas,
I have the following code to draw text centered vertically (and horizontally) in a
I have the following code in my Rails application's routes file: MyApp::Application.routes.draw do constraints
I have the following destroy method in my Tracks controller: def destroy if params[:product_id].present?
How can i generate multiple texts using the imagecreatetruecolor() method? I have the following
I have the following method that loads in a blank template image, draws the
I have an abstract class with an abstract method draw(Graphics2D g2), and the methods
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have following source. In insertMessage(..), it calls selectMessage to check whether duplicate record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.