291_自定义可点击表格

来源:互联网 发布:软件系统验收规范 编辑:程序博客网 时间:2024/05/25 23:26






自定义可点击表格




    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (widthMeasureSpec < heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        } else {
            super.onMeasure(heightMeasureSpec, heightMeasureSpec);
        }
        width = getWidth();
        height = getHeight();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);


        //给一个可变的数量
        num = 5;
        all = (float) width;
        single = all / num;


        //创建集合配合坐标数据
        list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                Entity entity = new Entity();
                entity.key = i;
                entity.value = j;
                list.add(entity);
                entity.position = list.size() - 1;
            }
        }
    }






    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getY();


                xCount = 0;
                yCount = 0;


                for (int i = 0; i < num; i++) {
                    x = x - single;
                    if (x < 0) {
                        break;
                    } else {
                        xCount++;
                    }
                }


                for (int i = 0; i < num; i++) {
                    y = y - single;
                    if (y < 0) {
                        break;
                    } else {
                        yCount++;
                    }
                }


                break;
            case MotionEvent.ACTION_UP:


                for (Entity entity : list) {
                    if (entity.key == xCount && entity.value == yCount) {
                        int position = entity.position;


                    }
                }


                invalidate();


                break;
        }
        return true;
    }












    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //先画出横排的线
        for (int i = 0; i < num; i++) {
            float startX = width / num * (i + 1);
            float startY = 0;
            float stopX = width / num * (i + 1);
            float stopY = height;
            canvas.drawLine(startX, startY, stopX, stopY, paint);
        }


        //再画出竖排的线
        for (int i = 0; i < num; i++) {
            float startX2 = 0;
            float startY2 = height / num * (i + 1);
            float stopX2 = width;
            float stopY2 = height / num * (i + 1);
            canvas.drawLine(startX2, startY2, stopX2, stopY2, paint);
        }


        paint.setColor(Color.RED);
        float x = (xCount + 1) * (single) - (single / 2.0f);
        float y = (yCount + 1) * (single) - (single / 2.0f);
        canvas.drawCircle(x, y, 20, paint);
        paint.setColor(Color.BLACK);

    }









0 0
原创粉丝点击