Сенсорный слушатель не обрабатывает все события в приложении - PullRequest
0 голосов
/ 09 июля 2019

Я создаю приложение для цветных книг, в котором слушатель касаний не принимает все события касания, иногда он пропускает некоторые касания.Я не могу понять, почему это происходит так.Я также добавляю ссылку на проект github, которая содержит все файлы кода.https://github.com/tanishqsatsangi/ColorBook

Вот код, в котором он прослушивает сенсорные события

 class tapListner extends GestureDetector.SimpleOnGestureListener {
    tapListner() {
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        int x = (int) e.getX();
        int y = (int) e.getY();
        final int sourcecolor = mBitmap.getPixel(x, y);
        int targetColor = s;
        int action = e.getAction();
        ArrayList<Integer> arrX = new ArrayList<>();
        ArrayList<Integer> arrY = new ArrayList<>();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                //ScaleImageView img = (ScaleImageView) findViewById(R.id.iv_coloring_figure);
                BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
                mBitmap = drawable.getBitmap();
                Rect imageBounds = new Rect();
                img.getDrawingRect(imageBounds);
                // original height and width of the bitmap
                int intrinsicHeight = drawable.getIntrinsicHeight();
                int intrinsicWidth = drawable.getIntrinsicWidth();
                // height and width of the visible (scaled) image
                int scaledHeight = imageBounds.height();
                int scaledWidth = imageBounds.width();
                // Find the ratio of the original image to the scaled image Should normally be equal unless a disproportionate scaling (e.g. fitXY) is used.
                float heightRatio = (float) intrinsicHeight / scaledHeight;
                float widthRatio = (float) intrinsicWidth / scaledWidth;
                // do whatever magic to get your touch point
                // MotionEvent event;
                // get the distance from the left and top of the image bounds
                float scaledImageOffsetX = e.getX() - imageBounds.left;
                float scaledImageOffsetY = e.getY() - imageBounds.top;
                // scale these distances according to the ratio of your scaling
                // For example, if the original image is 1.5x the size of the scaled image, and your offset is (10, 20), your original image offset values should be (15, 30).
                int originalImageOffsetX = (int)(scaledImageOffsetX * widthRatio);
                int originalImageOffsetY = (int)(scaledImageOffsetY * heightRatio);
                int oldColor = mBitmap.getPixel(originalImageOffsetX, originalImageOffsetY);
                arrX.add(originalImageOffsetX);
                arrY.add(originalImageOffsetY);
                int index = arrX.size();
                int newColor = s;
                newColor = targetColor;
                if (oldColor!=newColor ) {
                    Point p = new Point();
                    p.x = originalImageOffsetX;
                    p.y = originalImageOffsetY;
                    ActionSaver act = new ActionSaver(p, sourcecolor, targetColor);
                    lastaction.add(act);

                    sc = sourcecolor;
                    tc = targetColor;
                    pt = new Point();
                    pt.x = p.x;
                    pt.y = p.y;
                    new TheTask().execute();
                }
                break;
            default:
                break;
        }
        return super.onSingleTapConfirmed(e);
    }
}

Вот асинхронная задача, в которой я вызываю заливку для заливки цветом

    class TheTask extends AsyncTask<Void, Void, Void> {
    QueueLinearFloodFiller f;
    @Override
    protected Void doInBackground(Void... voids) {

        try{

             f = new QueueLinearFloodFiller(mBitmap, sc, tc);
            f.setTolerance(0);
            f.floodFill(pt.x, pt.y);
            return null;

        }catch (Exception e){
            e.printStackTrace();
        }

        return  null;
    }


    @Override
    protected void onPostExecute(Void aVoid) {
        img.setImageBitmap(f.getImage());
        img.invalidate();
        super.onPostExecute(aVoid);


    }
}

В коде "img" - это пользовательское изображение. Пожалуйста, помогите мне выяснить, в чем проблема, из-за которой пропускаются некоторые сенсорные события.

...