其实碰撞问题只是涉及到一点小算法而已,但在实际应用,尤其游戏中有可能会遇到,下面给出一个小示例,代码如下:
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>
附上图片效果: