DragTheBall小游戏之Android拖拽技术


本例是一个拖动小球的游戏(坑爹,给Baby玩的吧。。),主要是用到Android2D绘图技术、自定义组件技术。

话不多说,先上图:

1. 窗口在拖动小球之后会变为当前的Touch坐标

2. 当手选中小球(有点难选中,球有点儿小),手机会震动(必须是真机才有震动),50ms。

3. 小球会随着手的移动而移动。

4. 不加代码控制的话,小球可以自Right、Bottom两个方向移出视图,那个时候你就看不到了。不过本例子中加入了代码,是不能移出边界的。

5.  例子比较简单,功能也很单一,需要改进。欢迎安油门提出更好的实现方案。

实现思路:

1.  自定义一个BallView类,主要有一个成员变量HashSet<Ball> basket,顾名思义,用来存放Ball的Set集合。

2. BallView实现OnTouchListener,能监听Touch事件。

3. 将实现好的BallView类加入MainActivity,运行即可见到如图效果。

DragTheBall小游戏之Android拖拽技术

代码:

  1. package ryan.entity;
  2. import android.util.Log;
  3. /**
  4. * @author Ryan Hoo
  5. * @date: 2012/3/11
  6. * */
  7. publicclass Ball {
  8. publicint id;// 唯一标示
  9. publicfloat x;// 横坐标
  10. publicfloat y;// 纵坐标
  11. publicint drawableId;
  12. public Ball(float x, float y, int drawableId) {
  13. this.x = x;
  14. this.y = y;
  15. this.drawableId = drawableId;
  16. id = this.hashCode();
  17. Log.d("ball init", "id:" + id + " x:" + x + " y:" + y + " drawableId:"
  18. + drawableId);
  19. }
  20. }

 

  1. package ryan.entity;
  2. import android.util.Log;
  3. /**
  4. * @author Ryan Hoo
  5. * @date: 2012/3/11
  6. * */
  7. publicclass Ball {
  8. publicint id;// 唯一标示
  9. publicfloat x;// 横坐标
  10. publicfloat y;// 纵坐标
  11. publicint drawableId;
  12. public Ball(float x, float y, int drawableId) {
  13. this.x = x;
  14. this.y = y;
  15. this.drawableId = drawableId;
  16. id = this.hashCode();
  17. Log.d("ball init", "id:" + id + " x:" + x + " y:" + y + " drawableId:"
  18. + drawableId);
  19. }
  20. }

 

  1. package ryan.activity;
  2. import ryan.entity.Ball;
  3. import ryan.view.BallView;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.ViewGroup.LayoutParams;
  9. import android.widget.Toast;
  10. publicclass MainActivity extends Activity {
  11. /** Called when the activity is first created. */
  12. @Override
  13. publicvoid onCreate(Bundle savedInstanceState) {
  14. /*requestWindowFeature(Window.FEATURE_NO_TITLE);
  15. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  16. WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
  17. BallView view = new BallView(this);
  18. view.setBackgroundDrawable(getResources().getDrawable(R.drawable.back));
  19. view.addComponent(new Ball(0, 10, R.drawable.ball_blue));
  20. view.addComponent(new Ball(80, 10, R.drawable.bol_geel));
  21. view.addComponent(new Ball(160, 10, R.drawable.bol_groen));
  22. view.addComponent(new Ball(240, 10, R.drawable.bol_rood));
  23. super.onCreate(savedInstanceState);
  24. setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT,
  25. LayoutParams.FILL_PARENT));
  26. int width = getWindowManager().getDefaultDisplay().getWidth();
  27. int height = getWindowManager().getDefaultDisplay().getHeight();
  28. Toast.makeText(this, "width:" + width + "\nheight:" + height,
  29. Toast.LENGTH_SHORT).show();
  30. }
  31. @Override
  32. publicboolean onCreateOptionsMenu(Menu menu) {
  33. getMenuInflater().inflate(R.menu.menu, menu);
  34. returntrue;
  35. }
  36. @Override
  37. publicboolean onMenuItemSelected(int featureId, MenuItem item) {
  38. if(item.getItemId() == R.id.exit)
  39. finish();
  40. returnsuper.onMenuItemSelected(featureId, item);
  41. }
  42. }

源码下载地址

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/11月/11日/DragTheBall小游戏之Android拖拽技术

相关内容