android中碰撞屏幕边界反弹问题

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、QQ技术交流群(183198395)。

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

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>

附上图片效果:



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值