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 8747771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:24:43+00:00 2026-06-13T12:24:43+00:00

I have a surface view class which draws my bitmaps, and I am having

  • 0

I have a surface view class which draws my bitmaps, and I am having a bit of trouble getting an object to be launched from the center bottom of the screen to where the user clicks on the screen, here is a code snippet below
The onTouch method that gets the coordinates from user touching on the screen, then the run method draws the bitmaps. The rock bitmap is what I need to move from the bottom of the screen to where the touch was made.

           public boolean onTouch(View arg0, MotionEvent event) {


    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        x = event.getX();
        y = event.getY();
        sX = event.getX();
        sY = event.getY();
        fX = fY = dX = dY = sclX = sclY = aniX = aniY = 0;
        break;
    case MotionEvent.ACTION_UP:
        fX = event.getX();
        fY = event.getY();
        dX = fX - sX;
        dY = fY - sY;
        sclX = dX / 30;
        sclY = dY / 30;

        break;
    }
    return true;
}

        //the code that draws and updates the bitmaps on screen
        public void run() {
        while (running) {
            if (!sh.getSurface().isValid())
                continue;

            canvas = sh.lockCanvas();
            canvas.drawRGB(02, 02, 150);
            if (x != 0 && y != 0)
                canvas.drawBitmap(cross, x - (cross.getWidth() / 2), y- (cross.getHeight() / 2), null);

            canvas.drawBitmap(sling,(canvas.getWidth() / 2) - (sling.getHeight() / 2),canvas.getHeight() - sling.getHeight(), null);
            if (fX != 0 && fY != 0) {
                canvas.drawBitmap(rock, (canvas.getWidth()/2) - aniX,(canvas.getHeight()- sling.getHeight()) - aniY,null);

            }

            aniX = aniX + sclX;
            aniY = aniY + sclY;

            sh.unlockCanvasAndPost(canvas);
        }

    }
  • 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-13T12:24:44+00:00Added an answer on June 13, 2026 at 12:24 pm

    You are missing some vital thing in your onTouch().

    public boolean onTouch(View arg0, MotionEvent event) {
        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            x = event.getX();
            y = event.getY();
            sX = event.getX();
            sY = event.getY();
            fX = fY = dX = dY = sclX = sclY = aniX = aniY = 0;
            break;
          case MotionEvent.ACTION_UP:
            fX = event.getX();
            fY = event.getY();
            dX = fX - sX;
            dY = fY - sY;
            sclX = dX / 30;
            sclY = dY / 30;
    
            break;
        }
        return true;
    }
    

    This code has a logic issue. Consider you tap down on 100/100, sX and sY are set to 100 in the down case. In the up case, you get the up coordinates in fX and fY, which will be 100, too. This results in dX = fX - sX => dX = 100 - 100. So your sclX will be 0, too. This means no animation at all.

    You should use the middle of the screen as a base for your calculations. Source is the screen center and the destination is your touch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying an example having SurfaceView . I have inherited my class from
I have a class within my game which I copied from a tutorial: public
I have a surface (OffScreenPlain or RenderTarget with D3DFMT_A8R8G8B8) which I copy pixels (ARGB)
I have a native library which is invoked by the JNI function called from
In my application I have many classes and activities. Droid is a class which
I have two Activities, both have an inner class SurfaceView, which has an inner
I'm having trouble tying off my program (ending it up). I have a SurfaceView
I have a problem with simple drawing application on Android. Here's my class which
I am have some bitmaps which i want to display serially one after another
I have a problem with following simple code. I create GraphicView class which extends

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.