OnClick () va onTouch () ikkalasini ham android-da ishlating

OnClick() da oddiy image galereyasi ustida onClick() va onTouch() ustida ishlashni boshladim, lekin onclick() ishlamayapti, onTouch() da to'g'ri ishlashi bilan bog'liq. Haqiqiy muammo nimada ekanini kimdir aniqlay olishi mumkin. Mana mening kodim ..,

boolean isMoved=true;
    Matrix matrix=new Matrix();
    Matrix savedMatrix=new Matrix();
    PointF startPoint=new PointF();
    PointF midPoint=new PointF();
    float oldDist = 1f;
    int NONE = 0;
    int DRAG = 1;
    int ZOOM = 2;
    int mode = NONE;
public boolean onTouch(View v, MotionEvent event) {

        ImageView imageView = (ImageView) v;
        imageView.setScaleType(ScaleType.MATRIX);

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:
            matrix.set(imageView.getImageMatrix());
            isMoved = false;
            startPoint.set(event.getX(), event.getY());
            savedMatrix.set(matrix);
            mode = DRAG;
            break;

        case MotionEvent.ACTION_UP:
            if (!isMoved) {
                v.performClick();
            }
            break;

        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            savedMatrix.set(matrix);
            break;

        case MotionEvent.ACTION_MOVE:

            if (mode == DRAG) {
                matrix.set(savedMatrix);
                matrix.postTranslate(event.getX() - startPoint.x, event.getY()
                        - startPoint.y);
            } else if (mode == ZOOM) {

                float newDist = spacing(event);
                if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = newDist/oldDist;
                    matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                }
            }
            isMoved = true;
            break;

        case MotionEvent.ACTION_POINTER_DOWN:

            oldDist = spacing(event);

            if (oldDist > 10f) {
                savedMatrix.set(matrix);
                midPoint(midPoint, event);
                mode = ZOOM;
                isMoved = false;
            }
            break;
        }

        imageView.setImageMatrix(matrix);
        return true;
    }
@SuppressLint("FloatMath")
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x/2, y/2);
    }
0
Sizningcha, ImageView-ni tebranish mumkin va harakatlanadigan qilishni xohlaysiz. Agar shunday bo'lsa, men SimpleGestureDetector-ga murojaat qilmoqchiman. developer.android.com/reference/android/view/…
qo'shib qo'ydi muallif CzarMatt, manba
Sizningcha, ImageView-ni tebranish mumkin va harakatlanadigan qilishni xohlaysiz. Agar shunday bo'lsa, men SimpleGestureDetector-ga murojaat qilmoqchiman. developer.android.com/reference/android/view/…
qo'shib qo'ydi muallif CzarMatt, manba

10 javoblar

onClick() va onTouch() ham GesturesDetectors hisoblanadi va iste'mol tizimi mavjud. Qurilmaning taktikasi ustiga har qanday harakatni amalga oshirganingizda, barcha GesturesDetectors ( onClick() , onTouch() ), onDoubleTap() . ..) bu maxsus ko'rinishga biriktirilgan birma-bir deb nomlanadi. Agar siz GestureDetector-ning so'nggida siz haqiqatdan qaytarsangiz, unda voqea bajariladi va boshqalar GestureDetector chaqirilmaydi. Agar yolg'onga qaytsangiz, voqea bajarilmaydi va keyingi GestureDetector chaqiriladi.

Buning uchun onClick() ni amalga oshirish uchun onTouch() dan so'ng uni chaqirishni so'raymiz.

2
qo'shib qo'ydi

onClick() va onTouch() ham GesturesDetectors hisoblanadi va iste'mol tizimi mavjud. Qurilmaning taktikasi ustiga har qanday harakatni amalga oshirganingizda, barcha GesturesDetectors ( onClick() , onTouch() ), onDoubleTap() . ..) bu maxsus ko'rinishga biriktirilgan birma-bir deb nomlanadi. Agar siz GestureDetector-ning so'nggida siz haqiqatdan qaytarsangiz, unda voqea bajariladi va boshqalar GestureDetector chaqirilmaydi. Agar yolg'onga qaytsangiz, voqea bajarilmaydi va keyingi GestureDetector chaqiriladi.

Buning uchun onClick() ni amalga oshirish uchun onTouch() dan so'ng uni chaqirishni so'raymiz.

2
qo'shib qo'ydi

you can use the following logic to distinguish between onTouch() and onClick()

Tushuntiring:

private static final int DurationOfClick = 2000;  //duration of tap on screen
private long clickTime;

Endi onTouch (V, MotionEvent hodisasini ko'rish) holatini yozing.

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            clickTime = Calendar.getInstance().getTimeInMillis();
            break;
        }
        case MotionEvent.ACTION_UP: {
            long duration = Calendar.getInstance().getTimeInMillis() - clickTime;
            if(duration < DurationOfClick) {
                //click event has occurred write ur code
            }else{
                //touch event has occurred write ur code
            }
        }
    }
    return true;
}
0
qo'shib qo'ydi

you can use the following logic to distinguish between onTouch() and onClick()

Tushuntiring:

private static final int DurationOfClick = 2000;  //duration of tap on screen
private long clickTime;

Endi onTouch (V, MotionEvent hodisasini ko'rish) holatini yozing.

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            clickTime = Calendar.getInstance().getTimeInMillis();
            break;
        }
        case MotionEvent.ACTION_UP: {
            long duration = Calendar.getInstance().getTimeInMillis() - clickTime;
            if(duration < DurationOfClick) {
                //click event has occurred write ur code
            }else{
                //touch event has occurred write ur code
            }
        }
    }
    return true;
}
0
qo'shib qo'ydi

Quyidagi thnik kodi sizga yordam berishi mumkin, buni sinab ko'ring

  ImageView image_name = (ImageView) findViewById(R.id.image); //Find your image here
    image_name.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.d("test", "ontouch");
            return false;
        }
    });
    image_name.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Log.d("test", "onclick");
        }

    });

or you can use GestureDetector property of android go through this

0
qo'shib qo'ydi

Quyidagi thnik kodi sizga yordam berishi mumkin, buni sinab ko'ring

  ImageView image_name = (ImageView) findViewById(R.id.image); //Find your image here
    image_name.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.d("test", "ontouch");
            return false;
        }
    });
    image_name.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Log.d("test", "onclick");
        }

    });

or you can use GestureDetector property of android go through this

0
qo'shib qo'ydi

Siz onclick kodingizni berasiz, shuning uchun hammasi yaxshi bo'ladi deb o'ylayman. haqiqiy ga qaytayotganingizdan noto'g'ri Agar siz rostgo'y bo'lsangiz, onclick-ni ishga tushirishni xohlasangiz, onClick usuli bajarilmaydi.

0
qo'shib qo'ydi

Siz onclick kodingizni berasiz, shuning uchun hammasi yaxshi bo'ladi deb o'ylayman. haqiqiy ga qaytayotganingizdan noto'g'ri Agar siz rostgo'y bo'lsangiz, onclick-ni ishga tushirishni xohlasangiz, onClick usuli bajarilmaydi.

0
qo'shib qo'ydi

Men GestureDetector dan foydalanmoqdaman va muammo hal qilmoqdaman. Takliflaringiz uchun tashakkur bu mening javobim ..,

private GestureDetector gestureDetector;
gestureDetector = new GestureDetector(this, new SingleTapConfirm());

public boolean onTouch(View v, MotionEvent event) {

        ImageView imageView = (ImageView) v;
        imageView.setScaleType(ScaleType.MATRIX);
        if (gestureDetector.onTouchEvent(event)) {
            v.performClick();
        } else {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:


                matrix.set(imageView.getImageMatrix());
               //isMoved = false;
                startPoint.set(event.getX(), event.getY());
                savedMatrix.set(matrix);
                mode = DRAG;
                break;

            case MotionEvent.ACTION_UP:
                /*
                 * if (!isMoved) { v.performClick(); }
                 */
                break;

            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                savedMatrix.set(matrix);
                break;

            case MotionEvent.ACTION_MOVE:

                if (mode == DRAG) {
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - startPoint.x,
                            event.getY() - startPoint.y);
                } else if (mode == ZOOM) {

                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist/oldDist;
                        matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                    }
                }
               //isMoved = true;
                break;

            case MotionEvent.ACTION_POINTER_DOWN:

                oldDist = spacing(event);

                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(midPoint, event);
                    mode = ZOOM;
                   //isMoved = false;
                }
                break;
            }

            imageView.setImageMatrix(matrix);
        }
        return true;
    }

    @SuppressLint("FloatMath")
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x/2, y/2);
    }

    private class SingleTapConfirm extends SimpleOnGestureListener {

        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            return true;
        }
    }
0
qo'shib qo'ydi

Men GestureDetector dan foydalanmoqdaman va muammo hal qilmoqdaman. Takliflaringiz uchun tashakkur bu mening javobim ..,

private GestureDetector gestureDetector;
gestureDetector = new GestureDetector(this, new SingleTapConfirm());

public boolean onTouch(View v, MotionEvent event) {

        ImageView imageView = (ImageView) v;
        imageView.setScaleType(ScaleType.MATRIX);
        if (gestureDetector.onTouchEvent(event)) {
            v.performClick();
        } else {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:


                matrix.set(imageView.getImageMatrix());
               //isMoved = false;
                startPoint.set(event.getX(), event.getY());
                savedMatrix.set(matrix);
                mode = DRAG;
                break;

            case MotionEvent.ACTION_UP:
                /*
                 * if (!isMoved) { v.performClick(); }
                 */
                break;

            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                savedMatrix.set(matrix);
                break;

            case MotionEvent.ACTION_MOVE:

                if (mode == DRAG) {
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - startPoint.x,
                            event.getY() - startPoint.y);
                } else if (mode == ZOOM) {

                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist/oldDist;
                        matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                    }
                }
               //isMoved = true;
                break;

            case MotionEvent.ACTION_POINTER_DOWN:

                oldDist = spacing(event);

                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(midPoint, event);
                    mode = ZOOM;
                   //isMoved = false;
                }
                break;
            }

            imageView.setImageMatrix(matrix);
        }
        return true;
    }

    @SuppressLint("FloatMath")
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x/2, y/2);
    }

    private class SingleTapConfirm extends SimpleOnGestureListener {

        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            return true;
        }
    }
0
qo'shib qo'ydi