Identify triple tap on custom view

前端 未结 1 712
暖寄归人
暖寄归人 2021-02-06 15:49

I want to draw circle, whenever user taps on a custom view, and based on tap count circle color changes.

Single Tap : YELLOW CIRCLE
Double Tap : GREEN CIRCLE
Tri         


        
相关标签:
1条回答
  • 2021-02-06 16:30

    This code will do what you need. I simplified your class.

    import java.util.ArrayList;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.PointF;
    import android.graphics.RectF;
    import android.os.CountDownTimer;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class TripleTapView extends View {
    
        // Set the tap delay in milliseconds
        protected static final long TAP_MAX_DELAY = 500L;
        // Radius to capture tap within bound
        private final static int RADIUS = 30;
        // Store all points with tap count
        public ArrayList<CustomPoint> _points = new ArrayList<CustomPoint>();
    
        Context _context;
        Paint _paint;
    
        TapCounter _tapCounter = new TapCounter(TAP_MAX_DELAY, TAP_MAX_DELAY);
    
        public TripleTapView(Context context) {
            super(context);
            _context = context;
    
            _paint = new Paint();
            _paint.setAntiAlias(true);
            _paint.setDither(true);
            _paint.setStyle(Paint.Style.FILL);
            _paint.setStrokeJoin(Paint.Join.ROUND);
            _paint.setStrokeCap(Paint.Cap.ROUND);
            _paint.setStrokeWidth(2);
        }
    
        @Override
        protected void onDraw(final Canvas canvas) {
            super.onDraw(canvas);
            for (CustomPoint point_temp : _points) {
                // For changing tap circle color based on tap count
                switch (point_temp.count) {
                case 1:
                    _paint.setColor(Color.YELLOW);
                    break;
                case 2:
                    _paint.setColor(Color.GREEN);
                    break;
                case 3:
                    _paint.setColor(Color.RED);
                    break;
                }
                canvas.drawCircle(point_temp.point.x, point_temp.point.y, 10,
                        _paint);
            }
        }
    
        private RectF _lastTapArea;
        private int _lastTapCount = 0;
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                _tapCounter.resetCounter();
                float x = event.getX();
                float y = event.getY();
    
                if (_lastTapArea != null) {
                    if (_lastTapArea.contains(x, y)) {
                        if (_lastTapCount < 3) {
                            _lastTapCount++;
                        } else {
                            addPoint(_lastTapArea.centerX(),
                                    _lastTapArea.centerY(), _lastTapCount);
                            _lastTapCount = 1;
                        }
                    } else {
                        addPoint(_lastTapArea.centerX(), _lastTapArea.centerY(),
                                _lastTapCount);
                        _lastTapCount = 1;
                        _lastTapArea = new RectF(x - RADIUS, y - RADIUS,
                                x + RADIUS, y + RADIUS);
                    }
                } else {
                    _lastTapCount = 1;
                    _lastTapArea = new RectF(x - RADIUS, y - RADIUS, x + RADIUS, y
                            + RADIUS);
                }
    
                return true;
            }
    
            return false;
        }
    
        void addPoint(float x, float y, int tapCount) {
            _points.add(new CustomPoint(new PointF(x, y), tapCount));
            invalidate();
        }
    
        class TapCounter extends CountDownTimer {
    
            public TapCounter(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }
    
            @Override
            public void onFinish() {
                if (_lastTapArea != null) {
                    if (_lastTapCount > 0)
                        addPoint(_lastTapArea.centerX(), _lastTapArea.centerY(),
                                _lastTapCount);
    
                    _lastTapCount = 0;
                    _lastTapArea = null;
                }
            }
    
            @Override
            public void onTick(long millisUntilFinished) {
            }
    
            public void resetCounter() {
                start();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题