Giter Site home page Giter Site logo

loupe's Introduction

Loupe Android Arsenal

Loupe is an ImageView Helper for Android that supports zooming and swipe-to-dismiss action.

Loupe provides modern image viewer ui with super simple code. You can implement the Twitter-like image viewer in 10 minutes.

Download

Loupe is available on jCenter()

dependencies {
  implementation 'com.igreenwood:loupe:0.5.0'
}

Quick Start

In your Activity, add the following code. (Loupe is also working with Fragments.)

val loupe = Loupe(imageView).apply { // imageView is normal ImageView
  onViewTranslateListener = object : Loupe.OnViewTranslateListener {

    override fun onStart(view: ImageView) {
      
    }

    override fun onViewTranslate(view: ImageView, amount: Float) {
      
    }

    override fun onRestore(view: ImageView) {
      
    }

    override fun onDismiss(view: ImageView) {
      finish()
    }
  }
}

That's all. Now your ImageView supports zooming and swipe-to-dismiss action ๐Ÿ˜„

Dismiss Animation

In default, Loupe uses vertical translate animation on dismissing the ImageView.

If you use Shared Elements Transition, set useDismissAnimation to false.

val loupe = Loupe(imageView).apply {
  useDismissAnimation = false
  onViewTranslateListener = object : Loupe.OnViewTranslateListener {

    override fun onStart(view: ImageView) {}

    override fun onViewTranslate(view: ImageView, amount: Float) {}

    override fun onRestore(view: ImageView) {}

    override fun onDismiss(view: ImageView) {}
  }
}
Vertical Translate Animation Shared Elements Transition

OnViewTranslateListener

If you want to do some action while dimissing ImageView, use OnViewTranslateListener.

val loupe = Loupe(imageView).apply {
  onViewTranslateListener = object : Loupe.OnViewTranslateListener {

    override fun onStart(view: ImageView) {
      // called when the user start swiping down/up the view.
      hideToolbar()
    }

    override fun onViewTranslate(view: ImageView, amount: Float) {
      // called when every time the view y position is updated.
      changeBackgroundAlpha(amount)
    }

    override fun onRestore(view: ImageView) {
      // called when user cancelled the swiping.
      showToolbar()
    }

    override fun onDismiss(view: ImageView) {
      // called when the view translating animation has ended.
      finish()
    }
  }
}

For more details, see the sample program.

Using Loupe with image loader libraries

Loupe is just a helper of the ImageView. You can use Loupe with any image loader libraries. If you use with the image loader library, it would be better that initialize Loupe after the image loading has finished. With Glide, something like this.

Glide.with(imageView.context).load(url)
  .listener(object : RequestListener<Drawable> {
      override fun onLoadFailed(
        e: GlideException?,
        model: Any?,
        target: Target<Drawable>?,
        isFirstResource: Boolean
      ): Boolean {
        return false
      }

      override fun onResourceReady(
        resource: Drawable?,
        model: Any?,
        target: Target<Drawable>?,
        dataSource: DataSource?,
        isFirstResource: Boolean
      ): Boolean {
        val loupe = Loupe(image).apply {

            onViewTranslateListener = object : Loupe.OnViewTranslateListener {

            override fun onStart(view: ImageView) {}

            override fun onViewTranslate(view: ImageView, amount: Float) {}

            override fun onRestore(view: ImageView) {}

            override fun onDismiss(view: ImageView) {}
        }
      return false
    }
  }
).into(imageView)

Customization

Here is the customizable parameters.

Customizable parameters

Loupe(image).apply {
  useDismissAnimation = true // If you use shared elements transition, set false
  maxZoom = 8f
  dismissAnimationDuration = 250L // duration millis for dismiss animation
  restoreAnimationDuration = 250L // duration millis for restore animation
  flingAnimationDuration = 250L // duration millis for image fling animation
  scaleAnimationDuration = 250L // duration millis for double tap scale animation
  overScaleAnimationDuration = 250L // duration millis for over scale animation
  overScrollAnimationDuration = 250L // duration millis for over scrolling animation
  viewDragFriction = 1f // view drag friction for swipe to dismiss(1f : drag distance == view move distance. Smaller value, view is moving more slower)
  dragDismissDistanceInViewHeightRatio = 0.3f // distance threshold for swipe to dismiss(If the view drag distance is bigger than threshold, view will be dismissed. Otherwise view position will be restored to initial position.)
  flingDismissActionThresholdInDp = 48 // fling threshold for dismiss action(If the user fling the view and the view drag distance is smaller than threshold, fling dismiss action will be triggered)
  dismissAnimationInterpolator = DecelerateInterpolator() // animationn interpolator
  restoreAnimationInterpolator = DecelerateInterpolator() // animationn interpolator
  flingAnimationInterpolator = DecelerateInterpolator() // animationn interpolator
  doubleTapScaleAnimationInterpolator = DecelerateInterpolator() // animationn interpolator
  overScaleAnimationInterpolator = DecelerateInterpolator() // animationn interpolator
  overScrollAnimationInterpolator = DecelerateInterpolator() // animationn interpolator

  onViewTranslateListener = object : Loupe.OnViewTranslateListener {

    override fun onStart(view: ImageView) {}

    override fun onViewTranslate(view: ImageView, amount: Float) {}

    override fun onRestore(view: ImageView) {}

    override fun onDismiss(view: ImageView) {}
  }
}

You can try parameters with the sample program.

Requirements

Supported on API Level 21 and above.

Proguard

-dontwarn com.igreenwood.loupe**
-keep class com.igreenwood.loupe** { *; }
-keep interface com.igreenwood.loupe** { *; }

Developed By

Issei Aoki - [email protected]

Apps using loupe

If you are using my library, please let me know your app name ๐Ÿ˜„

License

The MIT License (MIT)

Copyright (c) 2020 Issei Aoki

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

loupe's People

Contributors

igreenwood avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.