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

作者:红薯 时间:2022-06-03 01:40:26 

本文实例讲述了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,屏幕,图片
0
投稿

猜你喜欢

  • Android本地数据存储Room实践和优化技巧

    2023-11-04 09:01:30
  • SpringBoot持久化层操作支持技巧

    2023-11-24 06:40:32
  • 深入了解java内存分配和回收策略

    2023-02-27 15:16:58
  • C#中的数组用法详解

    2021-08-19 14:50:17
  • spring AOP的After增强实现方法实例分析

    2023-06-10 13:52:06
  • 关于SpringBoot静态资源路径管理问题

    2022-12-04 03:51:29
  • C#使用虚拟方法实现多态

    2023-01-01 05:21:08
  • WinForm实现最小化到系统托盘方法实例详解

    2023-10-05 05:53:30
  • zookeeper watch机制的理解

    2021-11-08 11:36:36
  • C语言压缩文件和用MD5算法校验文件完整性的实例教程

    2023-04-01 05:21:49
  • IDEA插件EasyCode及MyBatis最优配置步骤详解

    2023-11-09 03:19:19
  • JAVA中的Token 基于Token的身份验证实例

    2023-11-09 18:05:09
  • 在java中ArrayList集合底层的扩容原理

    2023-12-19 11:12:15
  • c#如何使用 XML 文档功能

    2023-12-25 03:44:33
  • 解决IDEA无法下载maven依赖的问题

    2023-12-05 17:06:59
  • JVM垃圾收集器详解

    2022-10-06 10:46:00
  • SpringBoot 整合 Shiro 密码登录与邮件验证码登录功能(多 Realm 认证)

    2023-06-23 02:45:28
  • Spring Cloud Alibaba使用Nacos作为注册中心和配置中心

    2021-07-15 18:18:42
  • Java利用Easyexcel导出excel表格的示例代码

    2023-05-30 23:36:04
  • java日期格式化SimpleDateFormat的使用详解

    2023-08-25 03:22:15
  • asp之家 软件编程 m.aspxhome.com