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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:26:31+00:00 2026-05-30T03:26:31+00:00

I’m trying to run a very simple 2D animation when I fling an image

  • 0

I’m trying to run a very simple 2D animation when I fling an image view. I have 2 activities involved in this.

The GameCanvas

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY)
    {           
        if(GameWindow.getContext() == null)
            return false;

        if((e1.getY() >= GameWindow.getHeight()) && (e1.getY() <= GameWindow.getBottom()))
        {               
            try
            {                
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
                    return false;               
                // right to left slap               
                if((e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) && (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))
                {                                   
                    slappingLeft = true;

                    //Animate Slap                  
                    GameWindow.update();                

                    if(!running)
                        running = true;


                }  
                else if ((e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) && (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))              
                {       
                    slappingRight = true;

                    //Animate Slap
                    GameWindow.update();

                    if(!running )
                        running = true;
                }
            }                
            catch (Exception e)                 
            {                                   
                //nothing                                   
            }               
        }

        slappingLeft = slappingRight = running = false;
        return true;            
    }       

And the GameWindow

private RefreshHandler mRedrawHandler = new RefreshHandler();

class RefreshHandler extends Handler
{
    @Override
    public void handleMessage(Message msg) {
        GameWindow.this.update(); 
        GameWindow.this.invalidate();
    }

    public void sleep(long delayMillis) {
        this.removeMessages(0);
        sendMessageDelayed(obtainMessage(0), delayMillis);
    }
};

public void update() 
{
    //animate slap                  
    if(GameCanvas.slappingLeft)
    {           
        for(int i = 0; i < 500; i+=100)
        {
            GameCanvas.SlapLeft();              
            mRedrawHandler.sleep(100);              
        }
        GameCanvas.SetImage();
        //this.invalidate();
    }
    else if(GameCanvas.slappingRight)                           
    {           
        for(int i = 0; i < 500; i+=100)
        {
            GameCanvas.SlapImage();
            mRedrawHandler.sleep(100);
        }       
        GameCanvas.SetImage();
        //this.invalidate();
    }

}

I would greatly appreciate if anyone helps me figure this problem out. I have tried many different approaches to this problem.

I just want to show the SlapImage for half a second, then revert back to the normal image till onFling is called again.

If you would like to see SetImage(), SlapLeft(), and SetImage(), let me know!

Thanks very much in advance!

EDIT

GameCanvas is not a Canvas obj. Its an activity that setsContentView(R.layout.game_canvas)

GameWindow is a custom ImageView

  • 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-30T03:26:33+00:00Added an answer on May 30, 2026 at 3:26 am

    After some consideration, i decided to implement this solution. I think it works fairly well.

    I’m still not sure why my previous way didn’t work. If anyone would still like to elaborate I would appreciate.

    GameCanvas. I created this thread.

    resetSlap = new Thread()
    {
            public void run()
            {           
                while(true)
                {
                    try 
                    {
                        Thread.sleep(500);
                        GameWindow.post(new Runnable()
                        {
                            public void run()
                            {                           
                                GameWindow.setImageBitmap(images[0]);
                                GameWindow.postInvalidate();
                            }
                    });
                    } catch (InterruptedException e)
                    {                               
                        e.printStackTrace();
                    }                                   
                }
            }
        };          
    

    Baiscally, every half-second I want to reset the image back to normal

    Then in the OnFling I wrote

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY)
        {           
            if(GameWindow.getContext() == null)
                return false;
    
            if((e1.getY() >= GameWindow.getHeight()) && (e1.getY() <= GameWindow.getBottom()))
            {       
    
                try
                {                
                    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
                        return false;               
    
                    // right to left slap               
                    if((e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) && 
                            (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))
                    {                                   
                        slappingLeft = true;
    
                        //Animate Slap  
                        SlapLeft();
                        SlapSound();
                        if(voice.nextInt(10) < 3)
                        {
                            Voice.start();
                        }
    
                    }  
                    // left to right slap
                    else if ((e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) && 
                            (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY))               
                    {       
                        slappingRight = true;
    
                        //Animate Slap
                        SlapImage();
                        SlapSound();
                        if(voice.nextInt(10) < 3)
                        {
                            Voice.start();
                        }
                    }
                }                
                catch (Exception e)                 
                {                                   
                    //nothing                                   
                }               
            }
    
            slappingLeft = slappingRight  = false;
            return true;            
        }
    

    Basically, if I was slapping left I would call Slap left and wait for the thread to reset and if i was slapping right I would call Slap Right and wait for the thread to reset.

    This answer works well for my situation.. I hope this helps 😀

    Thank you all for your help.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is

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.