Results 1 to 11 of 11
  1. #1
    abacabb is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Question Getting my Android Animation to move along the edge of the screen.

    Hi all, I'm trying to get my animation for my java game to move along the edges of my device. Once it hits the edge I would like it to turn and keep moving along the edges. I'm sure this is easier than I'm making it but I can't find much on this. Right now my animation moves right and goes right off the screen until the app is closed and restarted. Thanks for any help!

    This is my code so far:

    Java Code:
    package com.pjf.animation;
    import java.io.IOException;
    import java.io.InputStream;
    
    import android.content.Context;
    import android.content.res.AssetManager;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class DrawView extends SurfaceView implements Runnable
    {
    	
    	Thread gameloop = null;
    	SurfaceHolder surface;
    	volatile boolean running = false;
    	AssetManager assets = null;
    	BitmapFactory.Options options = null;
    	Bitmap dragon[];
    	int frame = 0;
    	int x = 0;
    	
    	public DrawView(Context context) 
    	{
    		super(context);
    		surface = getHolder();
    		assets = context.getAssets();
    		options = new BitmapFactory.Options();
    		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    		dragon = new Bitmap[8];
    		try 
    		{
    			for (int n=0; n<8; n++) 
    			{
    	               String filename = "airdragon" + Integer.toString(n) + ".png";
    	               InputStream istream = assets.open(filename);
    	               dragon[n] = BitmapFactory.decodeStream(istream,null,options);
    	               istream.close();
    			}
    		} 
    			catch (IOException e) 
    		{
    			
    			e.printStackTrace();
    		}
    		
    	}
    	public void resume()
    	{
    		running = true;
    		gameloop = new Thread(this);
    		gameloop.start();
    		
    	}
    	public void pause()
    	{
    		while(true)
    		{
    			try 
    			{
    				gameloop.join();
    	        } 
    	        catch (InterruptedException e) 
    	        { 
    	        	
    	        }
    		}
        }
    	@Override
    	public void run()
    	{
    		while(running)
    		{
    			if (!surface.getSurface().isValid())
    			    continue;
    		
    		Canvas canvas = surface.lockCanvas();
    		canvas.drawColor(Color.rgb(0,0,0));
    		canvas.drawBitmap(dragon[frame], x, 0, null);
    		surface.unlockCanvasAndPost(canvas);
    		frame += 1;
    		if (frame > 7)
    			frame = 0;
    		x += 5;
    		try 
    		{
    			Thread.sleep(50);
            } 
    		catch(InterruptedException e) 
    		{
                e.printStackTrace();
            }
    		}
    	}
    }
    Last edited by abacabb; 02-18-2014 at 09:44 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Getting my Android Animation to move along the edge of the screen.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Getting my Android Animation to move along the edge of the screen.

    I think that before you call drawBitmap() you need to determine if the position where you are about to draw the bitmap is is on or off the canvas.
    If it's off the canvas, then you know you've reached the edge of the screen, and so you need to adjust your next drawing position accordingly.
    Once you figure out your coordinates, you can probably wrap those changes up into a convenience method like moveUp(), moveDown(), etc.

  4. #4
    abacabb is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Getting my Android Animation to move along the edge of the screen.

    Quote Originally Posted by sehudson View Post
    I think that before you call drawBitmap() you need to determine if the position where you are about to draw the bitmap is is on or off the canvas.
    If it's off the canvas, then you know you've reached the edge of the screen, and so you need to adjust your next drawing position accordingly.
    Once you figure out your coordinates, you can probably wrap those changes up into a convenience method like moveUp(), moveDown(), etc.
    So within my run method? Or before it?

    Basically my animation starts in the middle of the screen and runs directly off the screen to the right. I want him to stop at the screen and go down or up, then hit the next end and go left, etc.

    |<----------i
    ||
    ||
    ||
    ||
    |v
    |-------->
    Last edited by abacabb; 02-18-2014 at 08:30 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Getting my Android Animation to move along the edge of the screen.

    When the moving object gets to the edge of the screen, stop changing the variable that was causing the object to move in that direction.
    If moving to the right, stop increasing the x value.
    If moving down, stop increasing the y value.
    If moving to the left, stop decreasing the x value.
    etc
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    abacabb is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Getting my Android Animation to move along the edge of the screen.

    Quote Originally Posted by Norm View Post
    When the moving object gets to the edge of the screen, stop changing the variable that was causing the object to move in that direction.
    If moving to the right, stop increasing the x value.
    If moving down, stop increasing the y value.
    If moving to the left, stop decreasing the x value.
    etc
    My issue is I can't quite find a way to detect when it's reached the edge. Should I use variables for marking the location, if so how would I specify what constitutes the edge? I basically want to make the device screen it's boundaries and to continue moving within the boundary.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Getting my Android Animation to move along the edge of the screen.

    Look at the Display and DisplayMetrics classes for some methods that might be useful.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    abacabb is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Getting my Android Animation to move along the edge of the screen.

    Quote Originally Posted by Norm View Post
    Look at the Display and DisplayMetrics classes for some methods that might be useful.
    How and where would I use those in my code? I've been following a book and it doesn't mention anything about setting boundaries for the screen. I'm really not even sure how to initiate something like this. I'm pretty really new to Android coding.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Getting my Android Animation to move along the edge of the screen.

    take a look at the API doc for those classes to see what methods could be useful.
    Ask a question about the problem with an internet Search engine.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    abacabb is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Getting my Android Animation to move along the edge of the screen.

    Quote Originally Posted by Norm View Post
    take a look at the API doc for those classes to see what methods could be useful.
    Ask a question about the problem with an internet Search engine.
    Thanks for the help. I was actually on the Android developer site reading about what you suggested. I'm surprised even google didn't have a solution for this, it seems like it shouldn't be this hard. Maybe I'm not cut out for this after all :/

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Getting my Android Animation to move along the edge of the screen.

    One thing you absolutely must be able to do is read through API docs for classes to find what methods are useful and how to use them.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. 2D Array on a Android screen?
    By Ikarus Fox in forum Android
    Replies: 1
    Last Post: 12-23-2013, 09:43 AM
  2. Replies: 9
    Last Post: 09-06-2012, 09:37 AM
  3. Replies: 8
    Last Post: 08-16-2012, 09:41 PM
  4. Move from one screen fto other screen
    By jprgmr75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-02-2011, 06:32 PM
  5. Edge detector
    By ranwolf in forum SWT / JFace
    Replies: 9
    Last Post: 09-11-2008, 01:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •