Example usage for android.view MotionEvent getActionMasked

List of usage examples for android.view MotionEvent getActionMasked

Introduction

In this page you can find the example usage for android.view MotionEvent getActionMasked.

Prototype

public final int getActionMasked() 

Source Link

Document

Return the masked action being performed, without pointer index information.

Usage

From source file:kr.wdream.ui.PhotoViewer.java

private boolean onTouchEvent(MotionEvent ev) {
    if (animationInProgress != 0 || animationStartTime != 0) {
        return false;
    }//from w ww  . j a  v a2 s.c om

    if (currentEditMode == 2) {
        photoFilterView.onTouch(ev);
        return true;
    }

    if (currentEditMode == 1) {
        if (ev.getPointerCount() == 1) {
            if (photoCropView.onTouch(ev)) {
                updateMinMax(scale);
                return true;
            }
        } else {
            photoCropView.onTouch(null);
        }
    }

    if (captionEditText.isPopupShowing() || captionEditText.isKeyboardVisible()) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            closeCaptionEnter(true);
        }
        return true;
    }

    if (currentEditMode == 0 && ev.getPointerCount() == 1 && gestureDetector.onTouchEvent(ev)) {
        if (doubleTap) {
            doubleTap = false;
            moving = false;
            zooming = false;
            checkMinMax(false);
            return true;
        }
    }

    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
        if (currentEditMode == 1) {
            photoCropView.cancelAnimationRunnable();
        }
        discardTap = false;
        if (!scroller.isFinished()) {
            scroller.abortAnimation();
        }
        if (!draggingDown && !changingPage) {
            if (canZoom && ev.getPointerCount() == 2) {
                pinchStartDistance = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0));
                pinchStartScale = scale;
                pinchCenterX = (ev.getX(0) + ev.getX(1)) / 2.0f;
                pinchCenterY = (ev.getY(0) + ev.getY(1)) / 2.0f;
                pinchStartX = translationX;
                pinchStartY = translationY;
                zooming = true;
                moving = false;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            } else if (ev.getPointerCount() == 1) {
                moveStartX = ev.getX();
                dragY = moveStartY = ev.getY();
                draggingDown = false;
                canDragDown = true;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
        if (currentEditMode == 1) {
            photoCropView.cancelAnimationRunnable();
        }
        if (canZoom && ev.getPointerCount() == 2 && !draggingDown && zooming && !changingPage) {
            discardTap = true;
            scale = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0)) / pinchStartDistance
                    * pinchStartScale;
            translationX = (pinchCenterX - getContainerViewWidth() / 2)
                    - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX) * (scale / pinchStartScale);
            translationY = (pinchCenterY - getContainerViewHeight() / 2)
                    - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY) * (scale / pinchStartScale);
            updateMinMax(scale);
            containerView.invalidate();
        } else if (ev.getPointerCount() == 1) {
            if (velocityTracker != null) {
                velocityTracker.addMovement(ev);
            }
            float dx = Math.abs(ev.getX() - moveStartX);
            float dy = Math.abs(ev.getY() - dragY);
            if (dx > AndroidUtilities.dp(3) || dy > AndroidUtilities.dp(3)) {
                discardTap = true;
            }
            if (!(placeProvider instanceof EmptyPhotoViewerProvider) && currentEditMode == 0 && canDragDown
                    && !draggingDown && scale == 1 && dy >= AndroidUtilities.dp(30) && dy / 2 > dx) {
                draggingDown = true;
                moving = false;
                dragY = ev.getY();
                if (isActionBarVisible && canShowBottom) {
                    toggleActionBar(false, true);
                } else if (pickerView.getVisibility() == View.VISIBLE) {
                    toggleActionBar(false, true);
                    toggleCheckImageView(false);
                }
                return true;
            } else if (draggingDown) {
                translationY = ev.getY() - dragY;
                containerView.invalidate();
            } else if (!invalidCoords && animationStartTime == 0) {
                float moveDx = moveStartX - ev.getX();
                float moveDy = moveStartY - ev.getY();
                if (moving || currentEditMode != 0
                        || scale == 1 && Math.abs(moveDy) + AndroidUtilities.dp(12) < Math.abs(moveDx)
                        || scale != 1) {
                    if (!moving) {
                        moveDx = 0;
                        moveDy = 0;
                        moving = true;
                        canDragDown = false;
                    }

                    moveStartX = ev.getX();
                    moveStartY = ev.getY();
                    updateMinMax(scale);
                    if (translationX < minX && (currentEditMode != 0 || !rightImage.hasImage())
                            || translationX > maxX && (currentEditMode != 0 || !leftImage.hasImage())) {
                        moveDx /= 3.0f;
                    }
                    if (maxY == 0 && minY == 0 && currentEditMode == 0) {
                        if (translationY - moveDy < minY) {
                            translationY = minY;
                            moveDy = 0;
                        } else if (translationY - moveDy > maxY) {
                            translationY = maxY;
                            moveDy = 0;
                        }
                    } else {
                        if (translationY < minY || translationY > maxY) {
                            moveDy /= 3.0f;
                        }
                    }

                    translationX -= moveDx;
                    if (scale != 1 || currentEditMode != 0) {
                        translationY -= moveDy;
                    }

                    containerView.invalidate();
                }
            } else {
                invalidCoords = false;
                moveStartX = ev.getX();
                moveStartY = ev.getY();
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_CANCEL
            || ev.getActionMasked() == MotionEvent.ACTION_UP
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
        if (currentEditMode == 1) {
            photoCropView.startAnimationRunnable();
        }
        if (zooming) {
            invalidCoords = true;
            if (scale < 1.0f) {
                updateMinMax(1.0f);
                animateTo(1.0f, 0, 0, true);
            } else if (scale > 3.0f) {
                float atx = (pinchCenterX - getContainerViewWidth() / 2)
                        - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX)
                                * (3.0f / pinchStartScale);
                float aty = (pinchCenterY - getContainerViewHeight() / 2)
                        - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY)
                                * (3.0f / pinchStartScale);
                updateMinMax(3.0f);
                if (atx < minX) {
                    atx = minX;
                } else if (atx > maxX) {
                    atx = maxX;
                }
                if (aty < minY) {
                    aty = minY;
                } else if (aty > maxY) {
                    aty = maxY;
                }
                animateTo(3.0f, atx, aty, true);
            } else {
                checkMinMax(true);
            }
            zooming = false;
        } else if (draggingDown) {
            if (Math.abs(dragY - ev.getY()) > getContainerViewHeight() / 6.0f) {
                closePhoto(true, false);
            } else {
                if (pickerView.getVisibility() == View.VISIBLE) {
                    toggleActionBar(true, true);
                    toggleCheckImageView(true);
                }
                animateTo(1, 0, 0, false);
            }
            draggingDown = false;
        } else if (moving) {
            float moveToX = translationX;
            float moveToY = translationY;
            updateMinMax(scale);
            moving = false;
            canDragDown = true;
            float velocity = 0;
            if (velocityTracker != null && scale == 1) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = velocityTracker.getXVelocity();
            }

            if (currentEditMode == 0) {
                if ((translationX < minX - getContainerViewWidth() / 3 || velocity < -AndroidUtilities.dp(650))
                        && rightImage.hasImage()) {
                    goToNext();
                    return true;
                }
                if ((translationX > maxX + getContainerViewWidth() / 3 || velocity > AndroidUtilities.dp(650))
                        && leftImage.hasImage()) {
                    goToPrev();
                    return true;
                }
            }

            if (translationX < minX) {
                moveToX = minX;
            } else if (translationX > maxX) {
                moveToX = maxX;
            }
            if (translationY < minY) {
                moveToY = minY;
            } else if (translationY > maxY) {
                moveToY = maxY;
            }
            animateTo(scale, moveToX, moveToY, false);
        }
    }
    return false;
}

From source file:com.glview.widget.AbsListView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }/*w ww  .  j a v a2  s. c  o  m*/

    if (mPositionScroller != null) {
        mPositionScroller.stop();
    }

    if (mIsDetaching || !isAttachedToWindow()) {
        // Something isn't right.
        // Since we rely on being attached to get data set change notifications,
        // don't risk doing anything where we might try to resync and find things
        // in a bogus state.
        return false;
    }

    startNestedScroll(SCROLL_AXIS_VERTICAL);

    if (mFastScroll != null) {
        boolean intercepted = mFastScroll.onTouchEvent(ev);
        if (intercepted) {
            return true;
        }
    }

    initVelocityTrackerIfNotExists();
    final MotionEvent vtev = MotionEvent.obtain(ev);

    final int actionMasked = ev.getActionMasked();
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        onTouchDown(ev);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        onTouchMove(ev, vtev);
        break;
    }

    case MotionEvent.ACTION_UP: {
        onTouchUp(ev);
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        onTouchCancel();
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        final int x = mMotionX;
        final int y = mMotionY;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            final View child = getChildAt(motionPosition - mFirstPosition);
            mMotionViewOriginalTop = child.getTop();
            mMotionPosition = motionPosition;
        }
        mLastY = y;
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        // New pointers take over dragging duties
        final int index = ev.getActionIndex();
        final int id = ev.getPointerId(index);
        final int x = (int) ev.getX(index);
        final int y = (int) ev.getY(index);
        mMotionCorrection = 0;
        mActivePointerId = id;
        mMotionX = x;
        mMotionY = y;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            final View child = getChildAt(motionPosition - mFirstPosition);
            mMotionViewOriginalTop = child.getTop();
            mMotionPosition = motionPosition;
        }
        mLastY = y;
        break;
    }
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return true;
}

From source file:me.ububble.speakall.fragment.ConversationGroupFragment.java

@Override
public boolean onTouch(View v, MotionEvent event) {

    int action = event.getActionMasked();
    switch (v.getId()) {
    case R.id.record_audio:
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            textRecordingPress.setVisibility(View.INVISIBLE);
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            fechaAudioMillis = Calendar.getInstance().getTimeInMillis();
            File directory = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent");
            directory.mkdirs();//from  w ww. j a  va2 s.  c  o m
            ficheroAudio = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent/"
                    + fechaAudioMillis + ".mp4";
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setOutputFile(ficheroAudio);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
            }
            mediaRecorder.start();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    finalTime += 1000;
                    int seconds = (int) (finalTime / 1000) % 60;
                    int minutes = (int) ((finalTime / (1000 * 60)) % 60);
                    int hours = (int) ((finalTime / (1000 * 60 * 60)) % 24);
                    timeFinal = String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
                    timerAudio.setText(timeFinal);
                    if (!saveAudio) {
                        textRecording.setText(timeFinal);
                    }
                    handler.postDelayed(this, 1000);
                }
            });
            scaleAnimX = ObjectAnimator.ofFloat(recordAudioButton, "scaleX", 1, 1.2f);
            scaleAnimX.setDuration(800);
            scaleAnimX.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimX.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY = ObjectAnimator.ofFloat(recordAudioButton, "scaleY", 1, 1.2f);
            scaleAnimY.setDuration(800);
            scaleAnimY.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimY.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY.start();
            scaleAnimX.start();

            scaleRedBackgroundX = ObjectAnimator.ofFloat(audioRecordBackground, "scaleX", 1, 100f);
            scaleRedBackgroundX.setDuration(200);
            scaleRedBackgroundY = ObjectAnimator.ofFloat(audioRecordBackground, "scaleY", 1, 100f);
            scaleRedBackgroundY.setDuration(200);
            saveAudio = true;
            break;

        case MotionEvent.ACTION_MOVE:
            if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_white);
                timerAudio.setVisibility(View.GONE);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_red));
                if (!animatorBackground) {
                    textRecording.setText(timeFinal);
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    scaleRedBackgroundY.start();
                    scaleRedBackgroundX.start();
                    animatorBackground = true;
                }
                saveAudio = false;
            } else {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
                textRecording.setText(getString(R.string.audio_record));
                timerAudio.setVisibility(View.VISIBLE);
                if (animatorBackground) {
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    ViewHelper.setScaleX(audioRecordBackground, 1);
                    ViewHelper.setScaleY(audioRecordBackground, 1);
                    animatorBackground = false;
                }
                saveAudio = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            textRecordingPress.setVisibility(View.VISIBLE);
            animatorBackground = false;
            if (scaleAnimY.isRunning())
                scaleAnimY.end();
            if (scaleAnimX.isRunning())
                scaleAnimX.end();
            if (scaleRedBackgroundX.isRunning())
                scaleRedBackgroundX.end();
            if (scaleRedBackgroundY.isRunning())
                scaleRedBackgroundY.end();
            ViewHelper.setScaleX(audioRecordBackground, 1);
            ViewHelper.setScaleY(audioRecordBackground, 1);
            ViewHelper.setScaleX(recordAudioButton, 1);
            ViewHelper.setScaleY(recordAudioButton, 1);
            handler.removeCallbacksAndMessages(null);
            timerAudio.setText("00:00");
            timerAudio.setVisibility(View.VISIBLE);
            textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
            textRecording.setText(getString(R.string.audio_record));
            recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
            if (finalTime > 1000) {
                mediaRecorder.stop();
                mediaRecorder.release();
            } else {
                saveAudio = false;
                mediaRecorder.release();
            }
            finalTime = 0;
            timeFinal = "";
            if (saveAudio) {
                try {
                    hideKeyBoard();
                    final MsgGroups msjAudio = new MsgGroups();
                    JSONArray targets = new JSONArray();
                    JSONArray contactos = new JSONArray(grupo.targets);
                    String contactosId = null;
                    if (SpeakSocket.mSocket != null) {
                        if (SpeakSocket.mSocket.connected()) {
                            for (int i = 0; i < contactos.length(); i++) {
                                JSONObject contacto = contactos.getJSONObject(i);
                                JSONObject newContact = new JSONObject();
                                Contact contact = new Select().from(Contact.class)
                                        .where("id_contact = ?", contacto.getString("name")).executeSingle();
                                if (contact != null) {
                                    if (!contact.idContacto.equals(u.id)) {
                                        if (translate)
                                            newContact.put("lang", contact.lang);
                                        else
                                            newContact.put("lang", u.lang);
                                        newContact.put("name", contact.idContacto);
                                        newContact.put("screen_name", contact.screenName);
                                        newContact.put("status", 1);
                                        contactosId += contact.idContacto;
                                        targets.put(targets.length(), newContact);
                                    }
                                }
                            }
                        } else {
                            for (int i = 0; i < contactos.length(); i++) {
                                JSONObject contacto = contactos.getJSONObject(i);
                                JSONObject newContact = new JSONObject();
                                Contact contact = new Select().from(Contact.class)
                                        .where("id_contact = ?", contacto.getString("name")).executeSingle();
                                if (contact != null) {
                                    if (!contact.idContacto.equals(u.id)) {
                                        if (translate)
                                            newContact.put("lang", contact.lang);
                                        else
                                            newContact.put("lang", u.lang);
                                        newContact.put("name", contact.idContacto);
                                        newContact.put("screen_name", contact.screenName);
                                        newContact.put("status", -1);
                                        contactosId += contact.idContacto;
                                        targets.put(targets.length(), newContact);
                                    }
                                }
                            }
                        }
                    }
                    msjAudio.grupoId = grupo.grupoId;
                    msjAudio.mensajeId = u.id + grupo.grupoId + fechaAudioMillis + Settings.Secure
                            .getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
                    msjAudio.emisor = u.id;
                    msjAudio.receptores = targets.toString();
                    msjAudio.mensaje = "new Audio";
                    msjAudio.emisorEmail = u.email;
                    msjAudio.emisorLang = u.lang;
                    msjAudio.translation = false;
                    msjAudio.emitedAt = fechaAudioMillis;
                    msjAudio.tipo = Integer.parseInt(getString(R.string.MSG_TYPE_GROUP_AUDIO));
                    msjAudio.delay = 0;
                    msjAudio.fileUploaded = false;
                    msjAudio.audioName = ficheroAudio;
                    msjAudio.save();
                    showNewMessage(msjAudio);

                } catch (Exception e) {
                    // TODO: handle exception
                }
            } else {
                new File(ficheroAudio).delete();
            }
            break;
        }
        break;
    }

    return true;
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getActionMasked();
    switch (v.getId()) {
    case R.id.record_audio:
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            textRecordingPress.setVisibility(View.INVISIBLE);
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            fechaAudioMillis = Calendar.getInstance().getTimeInMillis();
            File directory = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent");
            directory.mkdirs();//from   w ww  .  ja v  a2s  .c o m
            ficheroAudio = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent/"
                    + fechaAudioMillis + ".mp4";
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setOutputFile(ficheroAudio);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
            }
            mediaRecorder.start();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    finalTime += 1000;
                    int seconds = (int) (finalTime / 1000) % 60;
                    int minutes = (int) ((finalTime / (1000 * 60)) % 60);
                    int hours = (int) ((finalTime / (1000 * 60 * 60)) % 24);
                    timeFinal = String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
                    timerAudio.setText(timeFinal);
                    if (!saveAudio) {
                        textRecording.setText(timeFinal);
                    }
                    handler.postDelayed(this, 1000);
                }
            });
            scaleAnimX = ObjectAnimator.ofFloat(recordAudioButton, "scaleX", 1, 1.2f);
            scaleAnimX.setDuration(800);
            scaleAnimX.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimX.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY = ObjectAnimator.ofFloat(recordAudioButton, "scaleY", 1, 1.2f);
            scaleAnimY.setDuration(800);
            scaleAnimY.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimY.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY.start();
            scaleAnimX.start();

            scaleRedBackgroundX = ObjectAnimator.ofFloat(audioRecordBackground, "scaleX", 1, 100f);
            scaleRedBackgroundX.setDuration(200);
            scaleRedBackgroundY = ObjectAnimator.ofFloat(audioRecordBackground, "scaleY", 1, 100f);
            scaleRedBackgroundY.setDuration(200);
            saveAudio = true;
            break;

        case MotionEvent.ACTION_MOVE:
            if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_white);
                timerAudio.setVisibility(View.GONE);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_red));
                if (!animatorBackground) {
                    textRecording.setText(timeFinal);
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    scaleRedBackgroundY.start();
                    scaleRedBackgroundX.start();
                    animatorBackground = true;
                }
                saveAudio = false;
            } else {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
                textRecording.setText(getString(R.string.audio_record));
                timerAudio.setVisibility(View.VISIBLE);
                if (animatorBackground) {
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    ViewHelper.setScaleX(audioRecordBackground, 1);
                    ViewHelper.setScaleY(audioRecordBackground, 1);
                    animatorBackground = false;
                }
                saveAudio = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            textRecordingPress.setVisibility(View.VISIBLE);
            animatorBackground = false;
            if (scaleAnimY.isRunning())
                scaleAnimY.end();
            if (scaleAnimX.isRunning())
                scaleAnimX.end();
            if (scaleRedBackgroundX.isRunning())
                scaleRedBackgroundX.end();
            if (scaleRedBackgroundY.isRunning())
                scaleRedBackgroundY.end();
            ViewHelper.setScaleX(audioRecordBackground, 1);
            ViewHelper.setScaleY(audioRecordBackground, 1);
            ViewHelper.setScaleX(recordAudioButton, 1);
            ViewHelper.setScaleY(recordAudioButton, 1);
            handler.removeCallbacksAndMessages(null);
            timerAudio.setText("00:00");
            timerAudio.setVisibility(View.VISIBLE);
            textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
            textRecording.setText(getString(R.string.audio_record));
            recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
            if (finalTime > 1000) {
                mediaRecorder.stop();
                mediaRecorder.release();
            } else {
                saveAudio = false;
                mediaRecorder.release();
            }
            finalTime = 0;
            timeFinal = "";
            if (saveAudio) {
                try {
                    hideKeyBoard();
                    final Message msjAudio = new Message();
                    String id = u.id + contact.idContacto + fechaAudioMillis + Settings.Secure
                            .getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
                    msjAudio.mensajeId = id;
                    msjAudio.emisor = u.id;
                    msjAudio.receptor = contact.idContacto;
                    msjAudio.emisorEmail = u.email;
                    msjAudio.receptorEmail = contact.email;
                    msjAudio.emisorLang = u.lang;
                    msjAudio.receptorLang = contact.lang;
                    msjAudio.emitedAt = fechaAudioMillis;
                    msjAudio.tipo = Integer.parseInt(getString(R.string.MSG_TYPE_AUDIO));
                    if (SpeakSocket.mSocket != null)
                        if (SpeakSocket.mSocket.connected()) {
                            msjAudio.status = 1;
                        } else {
                            msjAudio.status = -1;
                        }
                    msjAudio.fileUploaded = false;
                    msjAudio.audioName = ficheroAudio;
                    msjAudio.save();
                    Chats chat = new Select().from(Chats.class).where("idContacto = ?", msjAudio.receptor)
                            .executeSingle();
                    if (chat == null) {
                        Contact contact = new Select().from(Contact.class)
                                .where("id_contact = ?", msjAudio.receptor).executeSingle();
                        Chats newChat = new Chats();
                        newChat.mensajeId = msjAudio.mensajeId;
                        newChat.idContacto = msjAudio.receptor;
                        newChat.isLockedConversation = false;
                        newChat.lastStatus = msjAudio.status;
                        newChat.email = msjAudio.receptorEmail;
                        if (contact != null) {
                            newChat.photo = contact.photo;
                            newChat.fullName = contact.fullName;
                            newChat.lang = contact.lang;
                            newChat.screenName = contact.screenName;
                            newChat.photoload = true;
                            newChat.phone = contact.phone;
                        } else {
                            newChat.photo = null;
                            newChat.photoload = false;
                            newChat.fullName = msjAudio.receptorEmail;
                            newChat.lang = msjAudio.receptorLang;
                            newChat.screenName = msjAudio.receptorEmail;
                            newChat.phone = null;
                        }
                        newChat.emitedAt = msjAudio.emitedAt;
                        newChat.notRead = 0;
                        newChat.lastMessage = "send Audio";
                        newChat.show = true;
                        newChat.save();
                    } else {
                        if (!chat.photoload) {
                            Contact contact = new Select().from(Contact.class)
                                    .where("id_contact = ?", msjAudio.emisor).executeSingle();
                            if (contact != null) {
                                chat.photo = contact.photo;
                                chat.photoload = true;
                            } else {
                                chat.photo = null;
                                chat.photoload = false;
                            }
                        }
                        chat.mensajeId = msjAudio.mensajeId;
                        chat.lastStatus = msjAudio.status;
                        chat.emitedAt = msjAudio.emitedAt;
                        chat.notRead = 0;
                        chat.lastMessage = "send Audio";
                        chat.save();
                    }
                    showNewMessage(msjAudio);

                } catch (Exception e) {
                    // TODO: handle exception
                }
            } else {
                new File(ficheroAudio).delete();
            }
            break;
        }
        break;
    }

    return true;
}

From source file:org.telegram.ui.ArticleViewer.java

private boolean processTouchEvent(MotionEvent ev) {
    if (photoAnimationInProgress != 0 || animationStartTime != 0) {
        return false;
    }/*  w w w.  j av  a 2  s  .co  m*/

    if (ev.getPointerCount() == 1 && gestureDetector.onTouchEvent(ev) && doubleTap) {
        doubleTap = false;
        moving = false;
        zooming = false;
        checkMinMax(false);
        return true;
    }

    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
        discardTap = false;
        if (!scroller.isFinished()) {
            scroller.abortAnimation();
        }
        if (!draggingDown && !changingPage) {
            if (canZoom && ev.getPointerCount() == 2) {
                pinchStartDistance = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0));
                pinchStartScale = scale;
                pinchCenterX = (ev.getX(0) + ev.getX(1)) / 2.0f;
                pinchCenterY = (ev.getY(0) + ev.getY(1)) / 2.0f;
                pinchStartX = translationX;
                pinchStartY = translationY;
                zooming = true;
                moving = false;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            } else if (ev.getPointerCount() == 1) {
                moveStartX = ev.getX();
                dragY = moveStartY = ev.getY();
                draggingDown = false;
                canDragDown = true;
                if (velocityTracker != null) {
                    velocityTracker.clear();
                }
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
        if (canZoom && ev.getPointerCount() == 2 && !draggingDown && zooming && !changingPage) {
            discardTap = true;
            scale = (float) Math.hypot(ev.getX(1) - ev.getX(0), ev.getY(1) - ev.getY(0)) / pinchStartDistance
                    * pinchStartScale;
            translationX = (pinchCenterX - getContainerViewWidth() / 2)
                    - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX) * (scale / pinchStartScale);
            translationY = (pinchCenterY - getContainerViewHeight() / 2)
                    - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY) * (scale / pinchStartScale);
            updateMinMax(scale);
            photoContainerView.invalidate();
        } else if (ev.getPointerCount() == 1) {
            if (velocityTracker != null) {
                velocityTracker.addMovement(ev);
            }
            float dx = Math.abs(ev.getX() - moveStartX);
            float dy = Math.abs(ev.getY() - dragY);
            if (dx > AndroidUtilities.dp(3) || dy > AndroidUtilities.dp(3)) {
                discardTap = true;
            }
            if (canDragDown && !draggingDown && scale == 1 && dy >= AndroidUtilities.dp(30) && dy / 2 > dx) {
                draggingDown = true;
                moving = false;
                dragY = ev.getY();
                if (isActionBarVisible) {
                    toggleActionBar(false, true);
                }
                return true;
            } else if (draggingDown) {
                translationY = ev.getY() - dragY;
                photoContainerView.invalidate();
            } else if (!invalidCoords && animationStartTime == 0) {
                float moveDx = moveStartX - ev.getX();
                float moveDy = moveStartY - ev.getY();
                if (moving || scale == 1 && Math.abs(moveDy) + AndroidUtilities.dp(12) < Math.abs(moveDx)
                        || scale != 1) {
                    if (!moving) {
                        moveDx = 0;
                        moveDy = 0;
                        moving = true;
                        canDragDown = false;
                    }

                    moveStartX = ev.getX();
                    moveStartY = ev.getY();
                    updateMinMax(scale);
                    if (translationX < minX && (!rightImage.hasImage())
                            || translationX > maxX && !leftImage.hasImage()) {
                        moveDx /= 3.0f;
                    }
                    if (maxY == 0 && minY == 0) {
                        if (translationY - moveDy < minY) {
                            translationY = minY;
                            moveDy = 0;
                        } else if (translationY - moveDy > maxY) {
                            translationY = maxY;
                            moveDy = 0;
                        }
                    } else {
                        if (translationY < minY || translationY > maxY) {
                            moveDy /= 3.0f;
                        }
                    }

                    translationX -= moveDx;
                    if (scale != 1) {
                        translationY -= moveDy;
                    }

                    photoContainerView.invalidate();
                }
            } else {
                invalidCoords = false;
                moveStartX = ev.getX();
                moveStartY = ev.getY();
            }
        }
    } else if (ev.getActionMasked() == MotionEvent.ACTION_CANCEL
            || ev.getActionMasked() == MotionEvent.ACTION_UP
            || ev.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
        if (zooming) {
            invalidCoords = true;
            if (scale < 1.0f) {
                updateMinMax(1.0f);
                animateTo(1.0f, 0, 0, true);
            } else if (scale > 3.0f) {
                float atx = (pinchCenterX - getContainerViewWidth() / 2)
                        - ((pinchCenterX - getContainerViewWidth() / 2) - pinchStartX)
                                * (3.0f / pinchStartScale);
                float aty = (pinchCenterY - getContainerViewHeight() / 2)
                        - ((pinchCenterY - getContainerViewHeight() / 2) - pinchStartY)
                                * (3.0f / pinchStartScale);
                updateMinMax(3.0f);
                if (atx < minX) {
                    atx = minX;
                } else if (atx > maxX) {
                    atx = maxX;
                }
                if (aty < minY) {
                    aty = minY;
                } else if (aty > maxY) {
                    aty = maxY;
                }
                animateTo(3.0f, atx, aty, true);
            } else {
                checkMinMax(true);
            }
            zooming = false;
        } else if (draggingDown) {
            if (Math.abs(dragY - ev.getY()) > getContainerViewHeight() / 6.0f) {
                closePhoto(true);
            } else {
                animateTo(1, 0, 0, false);
            }
            draggingDown = false;
        } else if (moving) {
            float moveToX = translationX;
            float moveToY = translationY;
            updateMinMax(scale);
            moving = false;
            canDragDown = true;
            float velocity = 0;
            if (velocityTracker != null && scale == 1) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = velocityTracker.getXVelocity();
            }

            if ((translationX < minX - getContainerViewWidth() / 3 || velocity < -AndroidUtilities.dp(650))
                    && rightImage.hasImage()) {
                goToNext();
                return true;
            }
            if ((translationX > maxX + getContainerViewWidth() / 3 || velocity > AndroidUtilities.dp(650))
                    && leftImage.hasImage()) {
                goToPrev();
                return true;
            }

            if (translationX < minX) {
                moveToX = minX;
            } else if (translationX > maxX) {
                moveToX = maxX;
            }
            if (translationY < minY) {
                moveToY = minY;
            } else if (translationY > maxY) {
                moveToY = maxY;
            }
            animateTo(scale, moveToX, moveToY, false);
        }
    }
    return false;
}