歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用實例之跟隨手指的小球——自定義View應用

Android應用實例之跟隨手指的小球——自定義View應用

日期:2017/3/1 11:16:19   编辑:Linux編程

實現的功能:手指在屏幕上滑動,變幻顏色的小球始終跟隨手指移動。

實現的思路:1)自定義View,在onDraw中畫圓作為小球;2)重寫自定義View的onTouchEvent方法,記錄觸屏坐標,用新的坐標重新繪制小球。

關鍵技術點:自定義View應用、觸摸事件處理、canvas繪圖、Paint應用

第一步:新建一個工程,命名為BallViewDemo,Activity命名為BallActivity。

第二步:編寫自定義View類BallView,本例中將BallView作為BallActivity的內部類,BallActivity代碼如下:

  1. package com.zyg.customview.ball;
  2. import java.util.Random;
  3. import Android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.os.Bundle;
  9. import android.view.Display;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. public class BallActivity extends Activity {
  15. private int screenW; //屏幕寬度
  16. private int screenH; //屏幕高度
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. Display dis = this.getWindowManager().getDefaultDisplay();
  21. // 設置全屏
  22. requestWindowFeature(Window.FEATURE_NO_TITLE);
  23. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  24. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  25. // 獲取屏幕寬度
  26. screenW = dis.getWidth();
  27. // 獲取屏幕高度
  28. screenH = dis.getHeight();
  29. setContentView(new BallView(this));
  30. }
  31. //自定義繪圖類
  32. class BallView extends View{
  33. private Paint paint; //定義畫筆
  34. private float cx = 50; //圓點默認X坐標
  35. private float cy = 50; //圓點默認Y坐標
  36. private int radius = 20;
  37. //定義顏色數組
  38. private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED};
  39. private int paintColor = colorArray[0]; //定義畫筆默認顏色
  40. public BallView(Context context) {
  41. super(context);
  42. //初始化畫筆
  43. initPaint();
  44. }
  45. private void initPaint(){
  46. paint = new Paint();
  47. //設置消除鋸齒
  48. paint.setAntiAlias(true);
  49. //設置畫筆顏色
  50. paint.setColor(paintColor);
  51. }
  52. //重寫onDraw方法實現繪圖操作
  53. @Override
  54. protected void onDraw(Canvas canvas) {
  55. super.onDraw(canvas);
  56. //將屏幕設置為白色
  57. canvas.drawColor(Color.WHITE);
  58. //修正圓點坐標
  59. revise();
  60. //隨機設置畫筆顏色
  61. setPaintRandomColor();
  62. //繪制小圓作為小球
  63. canvas.drawCircle(cx, cy, radius, paint);
  64. }
  65. //為畫筆設置隨機顏色
  66. private void setPaintRandomColor(){
  67. Random rand = new Random();
  68. int randomIndex = rand.nextInt(colorArray.length);
  69. paint.setColor(colorArray[randomIndex]);
  70. }
  71. //修正圓點坐標
  72. private void revise(){
  73. if(cx <= radius){
  74. cx = radius;
  75. }else if(cx >= (screenW-radius)){
  76. cx = screenW-radius;
  77. }
  78. if(cy <= radius){
  79. cy = radius;
  80. }else if(cy >= (screenH-radius)){
  81. cy = screenH-radius;
  82. }
  83. }
  84. @Override
  85. public boolean onTouchEvent(MotionEvent event) {
  86. switch (event.getAction()) {
  87. case MotionEvent.ACTION_DOWN:
  88. // 按下
  89. cx = (int) event.getX();
  90. cy = (int) event.getY();
  91. // 通知重繪
  92. postInvalidate(); //該方法會調用onDraw方法,重新繪圖
  93. break;
  94. case MotionEvent.ACTION_MOVE:
  95. // 移動
  96. cx = (int) event.getX();
  97. cy = (int) event.getY();
  98. // 通知重繪
  99. postInvalidate();
  100. break;
  101. case MotionEvent.ACTION_UP:
  102. // 抬起
  103. cx = (int) event.getX();
  104. cy = (int) event.getY();
  105. // 通知重繪
  106. postInvalidate();
  107. break;
  108. }
  109. /*
  110. * 備注1:此處一定要將return super.onTouchEvent(event)修改為return true,原因是:
  111. * 1)父類的onTouchEvent(event)方法可能沒有做任何處理,但是返回了false。
  112. * 2)一旦返回false,在該方法中再也不會收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。
  113. */
  114. //return super.onTouchEvent(event);
  115. return true;
  116. }
  117. }
  118. }

main.xml與AndroidManifest.xml未作修改,不再貼出~

備注:代碼中的備注1介紹���onTouchEvent方法在實際開發中的一個Bug的解決方法,詳見代碼。

第三步:運行程序,效果如下:

下一篇將用自定義SurfaceView代替自定義View實現該實例功能(http://www.linuxidc.com/Linux/2011-10/44495.htm),並總結一下自定義View與自定義SurfaceView區別與應用場景。

Copyright © Linux教程網 All Rights Reserved