Animations driven by finger movement

Overview

OffsetAnimator

OffsetAnimator lets animate objects basing on touchevents, so users can be engaged in an animation process.

Yellow submarine

Usage

Add the library to your project:

  1. Add it in your root build.gradle at the end of repositories:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency:
dependencies {
    compile 'com.github.russelarms:offsetanimator:1.0.2'
}

Requirements

Min sdk version is 11. The library doesn't have any transitive dependencies.

Tutorial

A Comprehensive tutorial.

Scene

To create viewpager-based animation you should bind a scene to position updates:

ViewPagerAnimatorAdapter animatorAdapter = new ViewPagerAnimatorAdapter(scene.getScene());
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        animatorAdapter.onPageScrolled(position, positionOffset);
    }
});

Then create a scene instance and pass it a root view and an initializer function:

Scene.create(getRootView(), () -> initSteps());

Scripting

Script desired animation like this:

private void initSteps() {
    scene.page(0).step(0)
            .createAnimation(ocean.getY(), ocean.getY() - convertDIPToPixels(getContext(), 120))
            .setStartThreshold(0.5f)
            .setDuration(0.8f)
            .setInterpolator(new DecelerateInterpolator())
            .setListener(value -> ocean.setY(value));
}

A single page is taken as 1.0f. So duration 0.5f is a half of a distance.

Lazy initialization

It is not always convenient to specify exact view positions after some script done, so we can pass a lazy initializer. In this example animator will be initialized only on page 3 with corresponding coordinates:

scene.page(3).step(3)
        .createAnimation(() -> AnimatorFactory.createAnimator(fishLeftBottom.getY(), fishLeftBottom.getY() + screenDimensions.y / 2))
        .setDuration(0.5f)
        .setListener(value -> fishLeftBottom.setY(value));

Here's how to set the rotation:

scene.page(2).step(2)
        .createAnimation(0, 90)
        .setDuration(0.25f)
        .setListener(value -> submarine.setRotation(value));

Interpolators

We can specify an interpolator (from android.view.animation). The library ships its own SpringInterpolator:

scene.page(1).step(0)
        .createAnimation(1926, 1032)
        .setInterpolator(new SpringInterpolator(0.8f))
        .setListener(value -> submarine.setY(value));

Arc animations

Create arc animation:

scene.page(2).step(1)
        .createAnimation(() -> AnimatorFactory.createArcAnimator(submarine,
                Utils.centerX(submarine),
                convertDIPToPixels(getContext(), 48),
                submarine.getX() + submarine.getWidth() / 2,
                Utils.centerY(submarine),
                180f, Side.RIGHT))
        .setStartThreshold(0.5f)
        .setDuration(0.5f);

While standard OffsetAnimator requires user to set listeners and update fields by hand, arc animator doesn't need listener: it assigns value implicitly.

Inheritance

OffsetAnimator is open to be inherited:

public class AnotherOffsetAnimator extends OffsetAnimator {

    public AnotherOffsetAnimator(float x1, float x2) {
        super(x1, x2);
    }

    @Override
    public void animate(float position) {
        super.animate(position);
    }
}

and to be used with a scene:

scene.page(3).step(1)
        .createAnimation(() -> new AnotherOffsetAnimator(fishRight.getX(), fishRight.getX() + convertDIPToPixels(getContext(), 160)))
        .setDuration(0.5f)
        .setListener(value -> fishRight.setX(value));

License

OffsetAnimator is available under the MIT license. See the LICENSE file for more info. The library uses some code from https://github.com/asyl/ArcAnimator for arc animations.

The submarine and the fish images picked from freepik.com: Designed by Freepik Background vector created by Freepik

Copyright 2017 russelarms.

You might also like...
Android library to create complex multi-state animations.
Android library to create complex multi-state animations.

MultiStateAnimation Android library to create complex multi-state animations. Overview A class that allows for complex multi-state animations using An

Automatically manipulates the duration of animations dependent on view count. Quicksand .. the more you struggle.
Automatically manipulates the duration of animations dependent on view count. Quicksand .. the more you struggle.

QuickSand When showing a really enchanting explanatory animation to your users, but you know that after a while it'll get tedious and would stop users

Circle based animations for Android (min. API 11)
Circle based animations for Android (min. API 11)

CircularTools Circle based animations for Android (min. API 11) Currently implemented: Circular reveal Circular transform Radial reaction Reveal:YouTu

Lightweight Android library for cool activity transition animations

Bungee min SDK 16 (Android Jellybean 4.1) written in Java A lightweight, easy-to-use Android library that provides awesome activity transition animati

Repository for android animations Rx wrapper

RxAnimations RxAnimations is a library with the main goal to make android animations more solid and cohesive. Download compile 'oxim.digital:rxanim:

AXrLottie (Android) Renders animations and vectors exported in the bodymovin JSON format. (Using rLottie)
AXrLottie (Android) Renders animations and vectors exported in the bodymovin JSON format. (Using rLottie)

AXrLottie (Android) Renders animations and vectors exported in the bodymovin JSON format. (Using rLottie)

A lightweight android library that allows to you create custom fast forward/rewind animations like on Netflix.
A lightweight android library that allows to you create custom fast forward/rewind animations like on Netflix.

SuperForwardView About A lightweight android library that allows to you create custom fast forward/rewind animations like on Netflix. GIF Design Credi

Group of libraries to help you build better animations with Jetpack Compose
Group of libraries to help you build better animations with Jetpack Compose

Group of libraries to help you build better animations with Jetpack Compose

๐Ÿญ๐Ÿš€๐Ÿ’—  Tutorials about animations with Animators, Animated Vector Drawables, Shared Transitions, and more
๐Ÿญ๐Ÿš€๐Ÿ’— Tutorials about animations with Animators, Animated Vector Drawables, Shared Transitions, and more

๐Ÿญ๐Ÿš€๐Ÿ’— Tutorials about animations with Animators, Animated Vector Drawables, Shared Transitions, and more

Comments
  • Can't build for some reason...

    Can't build for some reason...

    I get this:

    Warning:One of the plugins you are using supports Java 8 language features. To try the support built into the Android plugin, remove the following from your build.gradle: apply plugin: 'me.tatarka.retrolambda' To learn more, go to https://d.android.com/r/tools/java-8-support-message.html

    Warning:One of the plugins you are using supports Java 8 language features. To try the support built into the Android plugin, remove the following from your build.gradle: apply plugin: 'me.tatarka.retrolambda' To learn more, go to https://d.android.com/r/tools/java-8-support-message.html

    Error:Execution failed for task ':app:transformClassesWithRetrolambdaForDebug'.

    Missing javaCompileTask for variant: debug/0 from output dir: D:\android\Android studio Projects\OffsetAnimator\app\build\intermediates\transforms\retrolambda\debug\0

    image

    opened by AndroidDeveloperLB 8
  • Animation just stops at the middle.

    Animation just stops at the middle.

    Sometimes, animation just stops at the middle of action. And this is not something that happens time after time. If it happend with animation once, then it stops work. It just follows the finger, but doesn't automatically go to key positions.

    opened by Nazacheres 4
  • Added simple logos & comments.

    Added simple logos & comments.

    Added a simple logo that appears abit nicer than the standard one. Also wanted to add some comments to help clarify why some of the code is placed there.

    opened by ThermoDev 1
  • ViewPagerAnimatorAdapter's problems

    ViewPagerAnimatorAdapter's problems

    on method onPageScrolled, if (positionOffset == 0.0f && prevPosition != position) { positionOffset = 1f; } and if (position == prevPosition) { scene.act(position, positionOffset); } are Mutual exclusion,why do you do this?

    opened by jackzhengpinwen 1
Releases(1.0.2)
Owner
Ruslan Urmeev
Mobile engineer, AppCenter SDKs contributor.
Ruslan Urmeev
Actions for android animations. Inspired by libgdx scene2d actions.

Android Animations Actions Actions for android animations. Inspired by libgdx scene2d actions. The main goal of this project is making creating of com

dtx12 137 Nov 29, 2022
[] An Android library which allows developers to easily add animations to ListView items

DEPRECATED ListViewAnimations is deprecated in favor of new RecyclerView solutions. No new development will be taking place, but the existing versions

Niek Haarman 5.6k Dec 30, 2022
FragmentTransactionExtended is a library which provide us a set of custom animations between fragments.

FragmentTransactionExtended FragmentTransactionExtended is a library which provide us a set of custom animations between fragments. FragmentTransactio

Antonio Corrales 1.1k Dec 29, 2022
Combine ViewPager and Animations to provide a simple way to create applications' guide pages.

WoWoViewPager WoWoViewPager combines ViewPager and Animations to provide a simple way to create applications' guide pages. When users are dragging WoW

Nightonke 2.7k Dec 30, 2022
Render After Effects animations natively on Android and iOS, Web, and React Native

Lottie for Android, iOS, React Native, Web, and Windows Lottie is a mobile library for Android and iOS that parses Adobe After Effects animations expo

Airbnb 33.5k Jan 4, 2023
๐ŸŒ  Transform into a different view or activity using morphing animations.

TransformationLayout ?? Transform into a different view or activity using morphing animations. Using Transformation motions of new material version. D

Jaewoong Eum 2k Jan 3, 2023
Android Transition animations explanation with examples.

UNMAINTAINED No maintainance is intended. The content is still valid as a reference but it won't contain the latest new stuff Android Transition Frame

Luis G. Valle 13.6k Dec 28, 2022
An Android library which provides simple Item animations to RecyclerView items

RecyclerViewItemAnimators Library Travis master: This repo provides: Appearance animations Simple animators for the item views Quick start You can now

Gabriele Mariotti 3.1k Dec 16, 2022
Library containing common animations needed for transforming ViewPager scrolling for Android v13+.

ViewPagerTransforms Library containing common animations needed for transforming ViewPager scrolling on Android v13+. This library is a rewrite of the

Ian Thomas 2.5k Dec 29, 2022
The lib can make the ActivityOptions animations use in Android api3.1+

ActivityOptionsICS ๆœฌ้กน็›ฎๅœๆญข็ปดๆŠค =========== f you are thinking on customizing the animation of Activity transition then probably you would look for Activit

Kale 591 Nov 18, 2022