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

  • SEARCH
  • Home
  • 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 7128589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:09:15+00:00 2026-05-28T11:09:15+00:00

I have problem to draw line on scaled and transformed image on canvas, well

  • 0

I have problem to draw line on scaled and transformed image on canvas, well for the transformed I am able to draw line on image but if the image was scaled(zoom in or out) then line drew unexpectedly pixel using finger touch. As I am working on paint application but didn’t have knowledge more about this so I need your help, I have searched it on google but no luck, I didn’t found any thing

here is the images 1st is default working properly and in second moved image working properly drew the line properly

enter image description here enter image description here

while in third on scaled image not properly draw, I mean if start draw from the top/left corner then the line drew point was different like here

enter image description here

if you need for code then say it I’ll edit the question

  • 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-05-28T11:09:16+00:00Added an answer on May 28, 2026 at 11:09 am

    Ok after many implementation I got the solution for this.

    This is my code for drawing in onDraw()

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(mPosX, mPosY);
        canvas.scale(mScaleFactor, mScaleFactor);
    
        canvas.drawBitmap(MoveImageTestActivity.bmp, 0,0,null);
        if(mScaleFactor!=1.f)
            canvas.restore();
    
        if(isDrawing)
            canvas.drawPath(tempPath,get_paint());
    
        if(mScaleFactor==1.f)
            canvas.restore();
    }
    

    here you have to check for the scale factor value for restore the canvas. If the scale factor value was default that is 1.f and image was just move then restore after the draw line/path, if the scale factor was not equals to the 1.f (default) then restore the canvas 1st then draw line/path at using finger touch drawing time.

    here is my onTouch() code

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mScaleDetector.onTouchEvent(ev);
        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
    
            if(isDrawing){
                float dx = ev.getX();
                float dy = ev.getY();;
    
                if(mScaleFactor==1.f){
                    dx = ev.getX() - mPosX;
                    dy = ev.getY() - mPosY;
                }
                tempPath = new Path();
                tempPath.moveTo(dx,dy);
            }else{
                mLastTouchX = x;
                mLastTouchY = y;
                // Save the ID of this pointer
                mActivePointerId = ev.getPointerId(0);
            }
            break;
        }
    
        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position
            if(isDrawing){
                float dx = ev.getX();
                float dy = ev.getY();;
    
                if(mScaleFactor==1.f){
                    dx = ev.getX() - mPosX;
                    dy = ev.getY() - mPosY;
                }
                tempPath.lineTo(dx,dy);
            }else{
                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                final float x = ev.getX(pointerIndex);
                final float y = ev.getY(pointerIndex);
    
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;
    
                mPosX += dx;
                mPosY += dy;
    
                mLastTouchX = x;
                mLastTouchY = y;
            }
            break;
        }
        case MotionEvent.ACTION_UP: {
            points.clear();
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }
    
        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }
    
        case MotionEvent.ACTION_POINTER_UP: {
            if(!isDrawing){
                // Extract the index of the pointer that left the touch sensor
                final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final int pointerId = ev.getPointerId(pointerIndex);
                if (pointerId == mActivePointerId) {
                    // This was our active pointer going up. Choose a new
                    // active pointer and adjust accordingly.
                    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                    mLastTouchX = ev.getX(newPointerIndex);
                    mLastTouchY = ev.getY(newPointerIndex);
                    mActivePointerId = ev.getPointerId(newPointerIndex);
                }
            }
            break;
        }
        }
        invalidate();
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with drawing image. How I can draw image in precision
Lets say we have a 400x800 canvas and I want to draw a line
I'm trying to draw curved arrows in a html canvas. I have no problem
I have a problem with an action listener after I draw a line, basicly
I am using gRaphael JS lib to draw line chart. and have some problem
I have problem with UIWebView delay when the load image from url. In my
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem to draw a square ASCII with the size limit in
I have a problem with separator line, it is based on NSBox , I
I tried to use the canvas tag to allow users to draw shapes, but

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.