Android Image Swipe Back effect

Android Image Swipe Back effect

Swipe back effect like Facebook Linkedin

Effect:

gif gif

Linkedin <–> My Implementation

Pre-requisite

Your theme style of this particular activity should be setup with

<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">#00000000</item>
<item name="android:windowIsTranslucent">true</item>

The below code is recommended if you hit any weird behaviour while re-enter your activity.

rootlayout.getBackground().setAlpha(255);

Talk is cheap

  1. Override the image on touch listener with our imageOnTouchListener.

Image On Touch Listener Event

Action Down: Touch Event

  1. Save the start Raw XY of the touch

Action Move: Drag Event

  1. Get current Raw XY of the touch
  2. Calculate Diff of Current XY with Start XY
  3. Save the Diff of XY to mMotionX and mMotionY
  4. Update the layoutParams of View
  5. Update the Alpha value of RootLayout

Action Cancel: Cancel Event

  1. If image Move more than predefined value (100), call onBackPressed() to achieve Image Swipe Back event
  2. Else, reset the image back to the intial position.

Show me the code

Closing

Personally I think this code can be more optimize, yet this is the best I could delivered at this moment.

If you have any suggestion on this code please comment, highly appreciate for it.

Thanks for reading.

Github

Comments