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

 更新时间:2015年06月16日 17:16:03   作者:红薯  
这篇文章主要介绍了Android实现在屏幕上移动图片的方法,实例分析了Android操作图片的相关技巧,需要的朋友可以参考下

本文实例讲述了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系统Binder机制

    理解Android系统Binder机制

    这篇文章主要为大家介绍了Android系统Binder机制,帮助大家理解Binder机制,感兴趣的朋友可以参考一下
    2016-05-05
  • Android实现拍照或者选取本地图片

    Android实现拍照或者选取本地图片

    这篇文章主要为大家详细介绍了Android实现拍照或者选取本地图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Android入门之PopupWindow用法实例解析

    Android入门之PopupWindow用法实例解析

    这篇文章主要介绍了Android入门之PopupWindow用法,对于Android初学者来说有一定的学习借鉴价值,需要的朋友可以参考下
    2014-08-08
  • Android 个人理财工具四:添加账单页面 下

    Android 个人理财工具四:添加账单页面 下

    本文主要介绍Android 个人理财工具添加账单页面,这里是添加账单的详情页面及如何使用Android Spinner控件的简单示例,有需要的小伙伴可以参考下
    2016-08-08
  • Android实现信号强度监听的方法

    Android实现信号强度监听的方法

    这篇文章主要介绍了Android实现信号强度监听的方法,是Android手机中很常见的一个实用功能,需要的朋友可以参考下
    2014-08-08
  • Android自定义view实现圆环效果实例代码

    Android自定义view实现圆环效果实例代码

    本文通过实例代码给大家介绍了Android自定义view实现圆环效果,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • android SDk中常用的java包介绍

    android SDk中常用的java包介绍

    在android的应用程序开发中,通常使用的是java语言,除了需要熟悉java语言的基础知识之外,还需要了解android提供的扩展的java功能。android SDK中API提供一些扩展的java 类库,类库分为若干个包,每个包中包含若干个类
    2014-05-05
  • Android AsyncTask源码分析

    Android AsyncTask源码分析

    这篇文章主要针对Android AsyncTask源码为大家进行分析,非常方便的AsyncTask类内部封装了Handler和线程池,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • Android HttpURLConnection断点下载(单线程)

    Android HttpURLConnection断点下载(单线程)

    这篇文章主要为大家详细介绍了Android HttpURLConnection断点下载的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Flutter倒计时/计时器的实现代码

    Flutter倒计时/计时器的实现代码

    这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论