android中碰撞屏幕边界反弹问题
其实碰撞问题只是涉及到一点小算法而已,但在实际应用,尤其游戏中有可能会遇到,下面给出一个小示例,代码如下:
MainActivity:
- package com.lovo;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- import android.app.Activity;
- public class MainActivity extends Activity {
- private Handler handler;
- public static final int MOVE_IMAGE = 1;
- // 移动方向和距离
- private int decX = 5;
- private int decY = 5;
- // 坐标
- private int moveX;
- private int moveY;
- private boolean isMove;// 是否正在移动
- private RelativeLayout relative;
- private ImageView imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.activity_main_image);
- handler = new MyHandler(this);
- relative = (RelativeLayout) findViewById(R.id.activity_main_relativelayout);
- Button endBtn = (Button) findViewById(R.id.activity_main_btn_end);
- endBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- isMove = false;
- }
- });
- Button btn = (Button) findViewById(R.id.activity_main_btn_start);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (!isMove) {
- isMove = true;
- } else {
- return;
- }
- new Thread() {
- public void run() {
- while (isMove) {
- moveX += decX;
- moveY += decY;
- if ((moveX + imageView.getWidth()) >= relative
- .getWidth() || moveX < 0) {
- decX = -decX;
- }
- if ((moveY + imageView.getHeight()) >= relative
- .getHeight() || moveY < 0) {
- decY = -decY;
- }
- Message message = new Message();
- message.what = MOVE_IMAGE;
- Bundle bundle = new Bundle();
- bundle.putInt("moveX", moveX);
- bundle.putInt("moveY", moveY);
- message.setData(bundle);
- handler.sendMessage(message);
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- };
- }.start();
- }
- });
- }
- }
MyHandler类:
- package com.lovo;
- import android.app.Activity;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- public class MyHandler extends Handler {
- private Activity activity;
- private ImageView imageView;
- public MyHandler(Activity activity) {
- this.activity = activity;
- }
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- imageView = (ImageView) activity.findViewById(R.id.activity_main_image);
- if (msg.what == MainActivity.MOVE_IMAGE) {
- android.widget.RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,
- RelativeLayout.LayoutParams.WRAP_CONTENT);
- // 利用Margin改变小球的位置
- lp.setMargins(msg.getData().getInt("moveX"),
- msg.getData().getInt("moveY"), 0, 0);
- imageView.setLayoutParams(lp);
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/activity_main_relativelayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true" >
- <Button
- android:id="@+id/activity_main_btn_start"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="开始" />
- <Button
- android:id="@+id/activity_main_btn_end"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="停止" />
- </LinearLayout>
- <ImageView
- android:id="@+id/activity_main_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/ball" />
- </RelativeLayout>
附上图片效果:
android中碰撞屏幕边界反弹问题的更多相关文章
- android中的屏幕单位介绍
1.px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px 会比较多.通常不推荐使用这个. 2.dip 或dp(与密度无关的像素):一个基于density(密度)的 ...
- 详解Android中的屏幕方向
屏幕方向 是对Activity而言的,所以你可以在AndroidManifest.xml 文件中,通过<activity> 标记的screenOrientation 属性进行设定,例如: ...
- android中获取屏幕的信息
获取屏幕信息比较简单,可以通过android的sdk自带的工具类DisplayMetrics.话不多说,上代码: // 获取屏幕的信息 DisplayMetrics dm = new DisplayM ...
- Android中获取屏幕长宽的方法
package com.kale.screen; import android.annotation.SuppressLint; import android.app.Activity; import ...
- [Selenium] Android 中旋转屏幕,触摸,滚动
package com.learingselenium.android; import junit.framework.TestCase import org.openqa.selenium.Rota ...
- Android中直播视频技术探究之---桌面屏幕视频数据源采集功能分析
一.前言 之前介绍了Android直播视频中一种视频源数据采集:摄像头Camera视频数据采集分析 中介绍了利用Camera的回调机制,获取摄像头的每一帧数据,然后进行二次处理进行推流.现在我们在介绍 ...
- 【转】如何在 Android 程序中禁止屏幕旋转和重启Activity
原文网址:http://www.cnblogs.com/bluestorm/p/3665890.html 禁止屏幕随手机旋转变化 有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变 ...
- 如何在 Android 程序中禁止屏幕旋转和重启Activity
禁止屏幕随手机旋转变化 有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变化:在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android ...
- android多分辨率多屏幕密度下UI适配方案
相关概念 分辨率:整个屏幕的像素数目,为了表示方便一般用屏幕的像素宽度(水平像素数目)乘以像素高度表示,形如1280x720,反之分辨率为1280x720的屏幕,像素宽度不一定为1280 屏幕密度:表 ...
随机推荐
- 杂货&&心跳
https://github.com/jsfront/month/blob/master/2016/201605.md https://github.com/abdmob/x2js https://l ...
- 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 ...
- Ant脚本简介与基础知识
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6624003.html 一:Ant是什么 Ant相当于Linux环境下的shell脚本,只不过是用xml文档来 ...
- linux下神奇的script
script 是一个神奇命令,script 能够将终端的会话过程录制下来,然后使用 scriptreplay 就可以将其录制的结果播放给他人观看.script 的好处就在于你在终端中的所有操作.敲过的 ...
- jump-game i&&ii 能否跳出区间 贪心
I: Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- C++11中的mutex, lock,condition variable实现分析
本文分析的是llvm libc++的实现:http://libcxx.llvm.org/ C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装.不 ...
- Xamarin.Android之SQLite.NET ORM
一.前言 通过<Xamarin.Android之SQLiteOpenHelper>和<Xamarin.Android之ContentProvider>的学习,我们已经掌握了如何 ...
- 怎么查看mysql的数据库编码格式
一.查看MySQL数据库服务器和数据库MySQL字符集. show variables like "%char%" 二.查看MySQL数据表(table)的MySQL字符集. sh ...
- Lotusscript统计在线用户数
使用notessession的SendConsoleCommand方法向服务器控制台发送“show inetusers”命令,该命令返回一个结果(字符串),字符串类似如下: admin 192.1 ...
- 完完全全彻底删除VMware_Workstation
vmware-workstation,卸载不干净.bat脚本自动处理dll.注册的表垃圾 Download https://pan.baidu.com/s/1dmrOs3rkR8cX5b0vTUOxH ...