简体   繁体   中英

Writing function for onTouchListner, when Imageview held for more than 2 seconds?

I am working on a app to unlock a door by sliding an Imageview upwards and holding it for aa few seconds. I have designed a layout in a ConstraintLayout Layout.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:padding="50dp">


<ImageView
    android:id="@+id/background_image"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/switch_button_background" />

<ImageView
    android:id="@+id/fill_image"
    android:layout_width="match_parent"
    android:layout_height="390dp"
    app:layout_constraintBottom_toBottomOf="@+id/background_image"

    app:layout_constraintEnd_toEndOf="@+id/background_image"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/background_image"
    app:layout_constraintTop_toTopOf="@+id/background_image"
    app:layout_constraintVertical_bias="0.4"
    app:srcCompat="@drawable/switch_button_fill_color">

</ImageView>

<LinearLayout
    android:id="@+id/linear_view"
    android:layout_width="200dp"
    android:layout_height="400dp"
    android:clipChildren="true"
    android:gravity="center|bottom"

    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="@id/fill_image"
    app:layout_constraintEnd_toEndOf="@+id/fill_image"
    app:layout_constraintStart_toStartOf="@+id/fill_image"
    app:layout_constraintTop_toTopOf="@+id/fill_image"
    app:layout_constraintVertical_bias="0.498">

    <ImageView
        android:id="@+id/thumb_icon"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:adjustViewBounds="true"
        android:src="@drawable/trimmy"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.48"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.72" />


</LinearLayout>


<ImageView
    android:id="@+id/img_unlock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@id/fill_image"
    app:layout_constraintEnd_toEndOf="@id/fill_image"
    app:layout_constraintStart_toStartOf="@id/fill_image"
    app:layout_constraintTop_toTopOf="@id/fill_image"
    app:layout_constraintVertical_bias="0.23000002"
    app:srcCompat="@drawable/ic_unlock" />

<ImageView
    android:id="@+id/main_login"
    android:layout_width="91dp"
    android:layout_height="60dp"
    app:layout_constraintBottom_toTopOf="@+id/background_image"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.7"
    app:srcCompat="@drawable/team_xx" />

I have tried to handle dragging event by OnThouchListner:

thumbIcon.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:                        
                    yDown = event.getY();                        
                    break;

                case MotionEvent.ACTION_MOVE:
                    float movedY;
                    movedY = event.getY();
                    float distance = movedY - yDown;
                    float currentDistance = thumbIcon.getY() + distance;
                    
                    if (currentDistance > 450) {
                        return false;
                    } else if (currentDistance < 50) {
                        return false;
                    } else {
                        thumbIcon.setY(thumbIcon.getY() + distance);
                    }
                    break;
                    
                case MotionEvent.ACTION_UP:
                    undoAnimation();
                    break;
            }
            return true;
        }
    });

I added if statements to limit Imageview moving bounds. However since I want to hold the thumb image upwards for a few seconds. I have no idea how to code this task? It would be great if anyone could help me.

EDIT:

I have came up with sort of workaround to tackle this issue. Although it's not perfect but it is the only possible solution I could come up with.

 switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // ... 
                    countDownTimer.start();
                    break;

I have added a countDownTimer to my code. for going off in two seconds. Then I check if the thumb icon or my moveable view is at the right position. then I execute whatever method I want in onFinish() block of my code:

countDownTimer = new CountDownTimer(2000, 250) {            
        @Override
        public void onFinish() {
            if (linearLayout.getY() >= thumbIcon.getY()) {
                //Execute my code here...
            }

I leave this question unanswered since this approach is not ideal. And this solution was inspired by Henry Twist comment.

You can set long click listener to your Linear Layout of your Image View it-self.

  thumbIcon.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
               ...
            }
        });

Just noticed that your linear layout around your image view is smaller than image view.

To hold the thumb image upwards for a few seconds you can start a Long Click Handler with a delay in milliseconds in your MotionEvent.ACTION_DOWN and you can remove this handler in on MotionEvent.ACTION_UP . When the Long Click handler triggered you can set a boolean value eg: enabledDragging = true where then in your MotionEvent.ACTION_MOVE you can use this flag to activate your move logic. Example of this will be like below:

thumbIcon.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                 startLongClickHandler();
                 break;
            case MotionEvent.ACTION_MOVE:
                 if(!enabledDragging)
                   return true;
                 //your move logic .....
                 break;
            case MotionEvent.ACTION_UP:
                 removeLongClickHandler();
                 break;
       }
       return true;
     }
 });

And the Long Click Handler methods will be like:

private boolean enabledDragging = false;
     
private void startLongClickHandler(){
  longClickHandler.postDelayed(mLongPressed, 2000);//2sec
}

private void removeLongClickHandler(){
   longClickHandler.removeCallbacks(mLongPressed);
   enabledDragging = false;
}

private final Handler longClickHandler = new Handler();
private Runnable mLongPressed = new Runnable() {
public void run() {
   enabledDragging = true;
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM