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

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

MainActivity:

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

MyHandler类:

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

附上图片效果:

转载于:https://www.cnblogs.com/android100/p/android-pengzhuang.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值