歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> DragTheBall小游戲之Android拖拽技術

DragTheBall小游戲之Android拖拽技術

日期:2017/3/1 10:07:21   编辑:Linux編程

本例是一個拖動小球的游戲(坑爹,給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,運行即可見到如圖效果。

代碼:

  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.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/11月/11日/DragTheBall小游戲之Android拖拽技術

Copyright © Linux教程網 All Rights Reserved