Android realizes how to move pictures on the screen _android

Source: Internet
Author: User
Tags gety

The example in this article describes how Android implements moving pictures on the screen. Share to everyone for your reference. The implementation methods are as follows:

1. Speed.java Documents:

Package net.obviam.droidz.model.components;
  public class Speed {public static final int direction_right = 1;
  public static final int direction_left =-1;
  public static final int direction_up =-1;
  public static final int direction_down = 1;  Private float XV = 1;  Velocity value on the X axis private float YV = 1;
  Velocity value on the Y axis private int xdirection = Direction_right;
  private int ydirection = Direction_down;
    Public Speed () {THIS.XV = 1;
  THIS.YV = 1;
    Public Speed (float XV, float yv) {this.xv = XV;
  This.yv = YV;
  public float GETXV () {return XV;
  public void Setxv (float xv) {this.xv = XV;
  public float Getyv () {return yv;
  public void Setyv (float yv) {this.yv = YV;
  public int getxdirection () {return xdirection;
  The public void setxdirection (int xdirection) {this.xdirection = xdirection;
  public int getydirection () {return ydirection; } public void Setydirection (int ydirection) {this.ydirection = ydirection;
  }//Changes the direction on the X axis public void togglexdirection () {xdirection = xdirection *-1;
  }//Changes the direction on the Y axis public void toggleydirection () {ydirection = ydirection *-1; }
}

2. Main.java File:

public void Run () {Canvas Canvas;
  LOG.D (TAG, "Starting game loop");
    while (running) {canvas = null; Try locking the canvas for exclusive pixel editing//In the surface try {canvas = This.surfaceHolder.lo
      Ckcanvas ();
        Synchronized (Surfaceholder) {//Update game State this.gamePanel.update ();
      Render state to the "screen//Draws" canvas on the panel this.gamePanel.render (canvas); finally {//In case of ' an exception the ' surface is ' not ' in//a inconsistent state if (can
      VAS!= null) {surfaceholder.unlockcanvasandpost (canvas); }//End finally} is public void Update () {//Check collision and right wall if heading right if (droid.get Speed (). getxdirection () = = Speed.direction_right && droid.getx () + Droid.getbitmap (). GetWidth ()/2 >= ge
  Twidth ()) {droid.getspeed (). Togglexdirection (); }//Check collision with leftWall if heading left if (Droid.getspeed (). getxdirection () = = Speed.direction_left && droid.getx ()-Droid.
  Getbitmap (). GetWidth ()/2 <= 0) {droid.getspeed (). Togglexdirection ();
      }//Check collision with bottom wall if heading down if (Droid.getspeed (). getydirection () = = Speed.direction_down && droid.gety () + Droid.getbitmap (). GetHeight ()/2 >= getheight ()) {droid.getspeed (). Toggleydirection (
  ); }//Check collision with top wall if heading up if (Droid.getspeed (). getydirection () = = Speed.direction_up
  ;& droid.gety ()-Droid.getbitmap (). GetHeight ()/2 <= 0) {droid.getspeed (). Toggleydirection ();
}//Update the Lone Droid droid.update (); }

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.