返回顶部
首页 > 资讯 > 移动开发 >Android实现在屏幕上移动图片的方法
  • 532
分享到

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

方法图片Android 2022-06-06 09:06:14 532人浏览 八月长安
摘要

本文实例讲述了Android实现在屏幕上移动图片的方法。分享给大家供大家参考。具体实现方法如下: 1. Speed.java文件: package net.obviam.dr

本文实例讲述了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程序设计有所帮助。

您可能感兴趣的文章:Android中利用matrix 控制图片的旋转、缩放、移动android 图片操作(缩放移动) 实例代码Android手势控制实现缩放、移动图片Android仿淘宝商品浏览界面图片滚动效果


--结束END--

本文标题: Android实现在屏幕上移动图片的方法

本文链接: https://lsjlt.com/news/26670.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作