欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现在屏幕上移动图片的方法

程序员文章站 2023-10-28 20:14:16
本文实例讲述了android实现在屏幕上移动图片的方法。分享给大家供大家参考。具体实现方法如下: 1. speed.java文件: package net.ob...

本文实例讲述了android实现在屏幕上移动图片的方法。分享给大家供大家参考。具体实现方法如下:

1. speed.java文件:

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;
  }
  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文件:

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.lockcanvas();
      synchronized (surfaceholder) {
        // update game state
        this.gamepanel.update();
        // render state to the screen
        // draws the canvas on the panel
        this.gamepanel.render(canvas);
      }
    } finally {
      // in case of an exception the surface is not left in
      // an inconsistent state
      if (canvas != null) {
        surfaceholder.unlockcanvasandpost(canvas);
      }
    }  // end finally
  }
}
public void update() {
  // check collision with right wall if heading right
  if (droid.getspeed().getxdirection() == speed.direction_right
      && droid.getx() + droid.getbitmap().getwidth() / 2 >= getwidth()) {
    droid.getspeed().togglexdirection();
  }
  // check collision with left wall 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();
}

希望本文所述对大家的android程序设计有所帮助。