赞
踩
实现:
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.RectF;
- import android.support.constraint.ConstraintLayout;
- import android.util.AttributeSet;
- import android.util.DisplayMetrics;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.LinearLayout;
-
-
-
- public class BGNet extends View {
- private Context context;
- private Paint paint;
- private Path path;
- private int dmWidth;
- private int dmHeight;
- private String TAG="BGNet";
-
- public BGNet(Context context) {
- super(context);
- this.context=context;
- init();
- }
-
- public BGNet(Context context,AttributeSet attrs) {
- super(context, attrs);
- this.context=context;
- init();
- }
-
- public BGNet(Context context,AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- this.context=context;
- init();
- }
-
- private void init(){
- DisplayMetrics displayMetrics = new DisplayMetrics();
- ((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
- dmWidth=displayMetrics.widthPixels;
- dmHeight=displayMetrics.heightPixels;
-
- paint=new Paint();
- paint.setAntiAlias(true);
- paint.setColor(Color.RED);
- paint.setStrokeWidth(3f);
- paint.setStyle(Paint.Style.STROKE);
-
- path=new Path();
-
- invalidate();
- }
-
- float allSpell=0;
- float top=200;
- int partCount=9;
-
-
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- LogTools.i(TAG, "onDraw==");
-
- //绘制网格
- top=(dmHeight-dmWidth)/2;
- allSpell=0;
- float spell=(float) dmWidth/(float) partCount;
- for(int i=0;i<=partCount;i++){
- canvas.drawLine(1.5f, allSpell+top, dmWidth, allSpell+top, paint);//横向
- float vX= i==0 ? 1.5f : (i==partCount ? allSpell-1.5f : allSpell);
- canvas.drawLine(vX, top, vX, dmWidth+top, paint);//纵向
- allSpell+=spell;
- }
-
- if(downRectF!=null){
- Paint downRectFPaint=new Paint();
- downRectFPaint.setColor(Color.RED);
- downRectFPaint.setAntiAlias(true);
- downRectFPaint.setStyle(Paint.Style.FILL);
- canvas.drawRect(downRectF, downRectFPaint);
- }
-
-
- }
-
- RectF downRectF;
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()){
- case MotionEvent.ACTION_DOWN:
- int c=0;
- float downX=event.getRawX();
- float downY=event.getRawY();
-
-
- float spell=((float) dmWidth/(float) partCount);
- float initX=spell;
- float initY=top+spell;
-
- while (initX<=dmWidth && initY<=dmHeight-top && downY<=dmHeight-top && downY>=top){
- c++;
-
- if(initX<downX){
- initX+=spell;
- }
-
- if(initY<downY){
- initY+=spell;
- }
- LogTools.i(TAG, "c=="+c+",initX="+initX+",initY="+initY+",downX="+downX+",downY="+downY+",spell="+spell+",top="+top);
- if(downX>= initX-spell && downX<=initX && downY>=initY-spell && downY<=initY){
-
- downRectF=new RectF(initX-spell,initY-spell,initX,initY);
- invalidate();
-
- LogTools.i(TAG, "down==row=="+initX/spell+",columns="+(initY-top)/spell);
- break;
- }
-
- }
-
- break;
- case MotionEvent.ACTION_MOVE:
- break;
- case MotionEvent.ACTION_UP:
- break;
- }
-
-
- return super.onTouchEvent(event);
- }
-
-
-
-
- // @Override
- // protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
- // super.onLayout(changed, left, top, right, bottom);
- //
- // LogTools.i(TAG,"onLayout="+changed);
- // }
- //
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- LogTools.i(TAG,"onMeasure=w="+getMeasuredWidth()+",h="+getMeasuredHeight());
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。