其实碰撞问题只是涉及到一点小算法而已,但在实际应用,尤其游戏中有可能会遇到,下面给出一个小示例,代码如下:

MainActivity:

  1. package com.lovo;
  2. import android.os.Bundle;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. import android.widget.RelativeLayout;
  10. import android.app.Activity;
  11. public class MainActivity extends Activity {
  12. private Handler handler;
  13. public static final int MOVE_IMAGE = 1;
  14. // 移动方向和距离
  15. private int decX = 5;
  16. private int decY = 5;
  17. // 坐标
  18. private int moveX;
  19. private int moveY;
  20. private boolean isMove;// 是否正在移动
  21. private RelativeLayout relative;
  22. private ImageView imageView;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. imageView = (ImageView) findViewById(R.id.activity_main_image);
  28. handler = new MyHandler(this);
  29. relative = (RelativeLayout) findViewById(R.id.activity_main_relativelayout);
  30. Button endBtn = (Button) findViewById(R.id.activity_main_btn_end);
  31. endBtn.setOnClickListener(new OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. isMove = false;
  35. }
  36. });
  37. Button btn = (Button) findViewById(R.id.activity_main_btn_start);
  38. btn.setOnClickListener(new OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. if (!isMove) {
  42. isMove = true;
  43. } else {
  44. return;
  45. }
  46. new Thread() {
  47. public void run() {
  48. while (isMove) {
  49. moveX += decX;
  50. moveY += decY;
  51. if ((moveX + imageView.getWidth()) >= relative
  52. .getWidth() || moveX < 0) {
  53. decX = -decX;
  54. }
  55. if ((moveY + imageView.getHeight()) >= relative
  56. .getHeight() || moveY < 0) {
  57. decY = -decY;
  58. }
  59. Message message = new Message();
  60. message.what = MOVE_IMAGE;
  61. Bundle bundle = new Bundle();
  62. bundle.putInt("moveX", moveX);
  63. bundle.putInt("moveY", moveY);
  64. message.setData(bundle);
  65. handler.sendMessage(message);
  66. try {
  67. Thread.sleep(10);
  68. } catch (InterruptedException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. };
  73. }.start();
  74. }
  75. });
  76. }
  77. }

MyHandler类:

  1. package com.lovo;
  2. import android.app.Activity;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.widget.ImageView;
  6. import android.widget.RelativeLayout;
  7. public class MyHandler extends Handler {
  8. private Activity activity;
  9. private ImageView imageView;
  10. public MyHandler(Activity activity) {
  11. this.activity = activity;
  12. }
  13. @Override
  14. public void handleMessage(Message msg) {
  15. super.handleMessage(msg);
  16. imageView = (ImageView) activity.findViewById(R.id.activity_main_image);
  17. if (msg.what == MainActivity.MOVE_IMAGE) {
  18. android.widget.RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
  19. RelativeLayout.LayoutParams.WRAP_CONTENT,
  20. RelativeLayout.LayoutParams.WRAP_CONTENT);
  21. // 利用Margin改变小球的位置
  22. lp.setMargins(msg.getData().getInt("moveX"),
  23. msg.getData().getInt("moveY"), 0, 0);
  24. imageView.setLayoutParams(lp);
  25. }
  26. }
  27. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/activity_main_relativelayout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:layout_alignParentBottom="true" >
  10. <Button
  11. android:id="@+id/activity_main_btn_start"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_weight="1"
  15. android:text="开始" />
  16. <Button
  17. android:id="@+id/activity_main_btn_end"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:text="停止" />
  22. </LinearLayout>
  23. <ImageView
  24. android:id="@+id/activity_main_image"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:src="@drawable/ball" />
  28. </RelativeLayout>

附上图片效果:

android中碰撞屏幕边界反弹问题的更多相关文章

  1. android中的屏幕单位介绍

    1.px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px 会比较多.通常不推荐使用这个. 2.dip 或dp(与密度无关的像素):一个基于density(密度)的 ...

  2. 详解Android中的屏幕方向

    屏幕方向 是对Activity而言的,所以你可以在AndroidManifest.xml 文件中,通过<activity> 标记的screenOrientation 属性进行设定,例如: ...

  3. android中获取屏幕的信息

    获取屏幕信息比较简单,可以通过android的sdk自带的工具类DisplayMetrics.话不多说,上代码: // 获取屏幕的信息 DisplayMetrics dm = new DisplayM ...

  4. Android中获取屏幕长宽的方法

    package com.kale.screen; import android.annotation.SuppressLint; import android.app.Activity; import ...

  5. [Selenium] Android 中旋转屏幕,触摸,滚动

    package com.learingselenium.android; import junit.framework.TestCase import org.openqa.selenium.Rota ...

  6. Android中直播视频技术探究之---桌面屏幕视频数据源采集功能分析

    一.前言 之前介绍了Android直播视频中一种视频源数据采集:摄像头Camera视频数据采集分析 中介绍了利用Camera的回调机制,获取摄像头的每一帧数据,然后进行二次处理进行推流.现在我们在介绍 ...

  7. 【转】如何在 Android 程序中禁止屏幕旋转和重启Activity

    原文网址:http://www.cnblogs.com/bluestorm/p/3665890.html 禁止屏幕随手机旋转变化 有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变 ...

  8. 如何在 Android 程序中禁止屏幕旋转和重启Activity

    禁止屏幕随手机旋转变化 有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变化:在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android ...

  9. android多分辨率多屏幕密度下UI适配方案

    相关概念 分辨率:整个屏幕的像素数目,为了表示方便一般用屏幕的像素宽度(水平像素数目)乘以像素高度表示,形如1280x720,反之分辨率为1280x720的屏幕,像素宽度不一定为1280 屏幕密度:表 ...

随机推荐

  1. 杂货&&心跳

    https://github.com/jsfront/month/blob/master/2016/201605.md https://github.com/abdmob/x2js https://l ...

  2. WIP 003 - Create page with a tablewalker

    Need ability to delete records by click the trash can Need ability to add new records to database an ...

  3. Ant脚本简介与基础知识

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6624003.html  一:Ant是什么 Ant相当于Linux环境下的shell脚本,只不过是用xml文档来 ...

  4. linux下神奇的script

    script 是一个神奇命令,script 能够将终端的会话过程录制下来,然后使用 scriptreplay 就可以将其录制的结果播放给他人观看.script 的好处就在于你在终端中的所有操作.敲过的 ...

  5. jump-game i&&ii 能否跳出区间 贪心

    I: Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  6. C++11中的mutex, lock,condition variable实现分析

    本文分析的是llvm libc++的实现:http://libcxx.llvm.org/ C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装.不 ...

  7. Xamarin.Android之SQLite.NET ORM

    一.前言 通过<Xamarin.Android之SQLiteOpenHelper>和<Xamarin.Android之ContentProvider>的学习,我们已经掌握了如何 ...

  8. 怎么查看mysql的数据库编码格式

    一.查看MySQL数据库服务器和数据库MySQL字符集. show variables like "%char%" 二.查看MySQL数据表(table)的MySQL字符集. sh ...

  9. Lotusscript统计在线用户数

    使用notessession的SendConsoleCommand方法向服务器控制台发送“show inetusers”命令,该命令返回一个结果(字符串),字符串类似如下: admin   192.1 ...

  10. 完完全全彻底删除VMware_Workstation

    vmware-workstation,卸载不干净.bat脚本自动处理dll.注册的表垃圾 Download https://pan.baidu.com/s/1dmrOs3rkR8cX5b0vTUOxH ...