GithubHelp home page GithubHelp logo

flick's Introduction

Flick

Flick in action

Flick is a tiny library for flick dismissing images (or anything actually). You can read the announcement blog post to learn how Flick was created.

implementation 'me.saket:flick:1.3.0'

Usage

The sample project contains best practices for using Flick. You can download its APK from here for trying it out on your phone.

<me.saket.flick.FlickDismissLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</me.saket.flick.FlickDismissLayout>

Flick requires you to manually provide the content dimensions instead of it relying on the content View's dimensions. This is useful for scalable ImageViews, where the height will always be set to match-parent, but the actual image may or may not be consuming the entire space.

val callbacks = object : FlickCallbacks {
  override fun onFlickDismiss(animationDuration: Long) {
    // Called when the View has been flicked and the Activity
    // should be dismissed.
    flickDismissLayout.postDelayed({ finish() }, animationDuration)
  }

  override fun onMove(@FloatRange(from = -1.0, to = 1.0) moveRatio: Float) {
    // Called while this View is being moved around. Updating
    // background dimming is a good usecase for this callback.
  }
}

val contentSizeProvider = object : ContentSizeProvider {
  override fun heightForDismissAnimation(): Int {
    return imageView.zoomedImageHeight.toInt()
  }

  override fun heightForCalculatingDismissThreshold(): Int {
    // Zoomed in height minus the portions of image that has gone
    // outside display bounds, because they are longer visible.
    imageView.visibleZoomedImageHeight
  }
}

val flickDismissLayout: FlickDismissLayout = findViewById(...)
flickDismissLayout.gestureListener = FlickGestureListener(context, contentSizeProvider, callbacks)

Intercepting flicks

For usecases where the content can be scrolled further in the direction of the gesture, Flick exposes a way for intercepting flick detection,

// Block flick gestures if the image can pan further.
gestureListener.gestureInterceptor = { scrollY ->
  val isScrollingUpwards = scrollY < 0
  val directionInt = if (isScrollingUpwards) -1 else +1
  val canPanFurther = imageView.canScrollVertically(directionInt)

  when {
    canPanFurther -> InterceptResult.INTERCEPTED
    else -> InterceptResult.IGNORED
  }
}

License

Copyright 2019 Saket Narayan.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

flick's People

Contributors

saket avatar

Watchers

James Cloos 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.