当前位置:   article > 正文

android canvas 绘制网格 并添加格子点击事件_canvas 网格点击

canvas 网格点击

实现: 

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Path;
  7. import android.graphics.RectF;
  8. import android.support.constraint.ConstraintLayout;
  9. import android.util.AttributeSet;
  10. import android.util.DisplayMetrics;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import android.widget.LinearLayout;
  14. public class BGNet extends View {
  15. private Context context;
  16. private Paint paint;
  17. private Path path;
  18. private int dmWidth;
  19. private int dmHeight;
  20. private String TAG="BGNet";
  21. public BGNet(Context context) {
  22. super(context);
  23. this.context=context;
  24. init();
  25. }
  26. public BGNet(Context context,AttributeSet attrs) {
  27. super(context, attrs);
  28. this.context=context;
  29. init();
  30. }
  31. public BGNet(Context context,AttributeSet attrs, int defStyleAttr) {
  32. super(context, attrs, defStyleAttr);
  33. this.context=context;
  34. init();
  35. }
  36. private void init(){
  37. DisplayMetrics displayMetrics = new DisplayMetrics();
  38. ((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
  39. dmWidth=displayMetrics.widthPixels;
  40. dmHeight=displayMetrics.heightPixels;
  41. paint=new Paint();
  42. paint.setAntiAlias(true);
  43. paint.setColor(Color.RED);
  44. paint.setStrokeWidth(3f);
  45. paint.setStyle(Paint.Style.STROKE);
  46. path=new Path();
  47. invalidate();
  48. }
  49. float allSpell=0;
  50. float top=200;
  51. int partCount=9;
  52. @Override
  53. protected void onDraw(Canvas canvas) {
  54. super.onDraw(canvas);
  55. LogTools.i(TAG, "onDraw==");
  56. //绘制网格
  57. top=(dmHeight-dmWidth)/2;
  58. allSpell=0;
  59. float spell=(float) dmWidth/(float) partCount;
  60. for(int i=0;i<=partCount;i++){
  61. canvas.drawLine(1.5f, allSpell+top, dmWidth, allSpell+top, paint);//横向
  62. float vX= i==0 ? 1.5f : (i==partCount ? allSpell-1.5f : allSpell);
  63. canvas.drawLine(vX, top, vX, dmWidth+top, paint);//纵向
  64. allSpell+=spell;
  65. }
  66. if(downRectF!=null){
  67. Paint downRectFPaint=new Paint();
  68. downRectFPaint.setColor(Color.RED);
  69. downRectFPaint.setAntiAlias(true);
  70. downRectFPaint.setStyle(Paint.Style.FILL);
  71. canvas.drawRect(downRectF, downRectFPaint);
  72. }
  73. }
  74. RectF downRectF;
  75. @Override
  76. public boolean onTouchEvent(MotionEvent event) {
  77. switch (event.getAction()){
  78. case MotionEvent.ACTION_DOWN:
  79. int c=0;
  80. float downX=event.getRawX();
  81. float downY=event.getRawY();
  82. float spell=((float) dmWidth/(float) partCount);
  83. float initX=spell;
  84. float initY=top+spell;
  85. while (initX<=dmWidth && initY<=dmHeight-top && downY<=dmHeight-top && downY>=top){
  86. c++;
  87. if(initX<downX){
  88. initX+=spell;
  89. }
  90. if(initY<downY){
  91. initY+=spell;
  92. }
  93. LogTools.i(TAG, "c=="+c+",initX="+initX+",initY="+initY+",downX="+downX+",downY="+downY+",spell="+spell+",top="+top);
  94. if(downX>= initX-spell && downX<=initX && downY>=initY-spell && downY<=initY){
  95. downRectF=new RectF(initX-spell,initY-spell,initX,initY);
  96. invalidate();
  97. LogTools.i(TAG, "down==row=="+initX/spell+",columns="+(initY-top)/spell);
  98. break;
  99. }
  100. }
  101. break;
  102. case MotionEvent.ACTION_MOVE:
  103. break;
  104. case MotionEvent.ACTION_UP:
  105. break;
  106. }
  107. return super.onTouchEvent(event);
  108. }
  109. // @Override
  110. // protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  111. // super.onLayout(changed, left, top, right, bottom);
  112. //
  113. // LogTools.i(TAG,"onLayout="+changed);
  114. // }
  115. //
  116. @Override
  117. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  118. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  119. LogTools.i(TAG,"onMeasure=w="+getMeasuredWidth()+",h="+getMeasuredHeight());
  120. }
  121. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/254530
推荐阅读
相关标签
  

闽ICP备14008679号