国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現(xiàn)在屏幕上移動圖片的方法

2020-04-11 11:29:46
字體:
來源:轉載
供稿:網友

本文實例講述了Android實現(xiàn)在屏幕上移動圖片的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

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程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 贡觉县| 清远市| 安图县| 敦煌市| 苍梧县| 丹阳市| 石屏县| 辉县市| 花莲县| 晋江市| 法库县| 锡林浩特市| 微博| 溆浦县| 德令哈市| 旌德县| 九江市| 乳山市| 保康县| 闽侯县| 白沙| 保山市| 昭苏县| 固原市| 罗江县| 鄱阳县| 正定县| 什邡市| 沁源县| 正安县| 阿城市| 庐江县| 奉新县| 南澳县| 浦北县| 长沙市| 保康县| 西青区| 罗定市| 东山县| 连南|