Parallax-Layer-Layout

Layered parallax effect to any Android views

Github星跟蹤圖

Parallax Layer Layout

Bintray Build Status

Lets you add layered parallax effect to your Android views or images based on things like device rotation.

We use it at InfoJobs for our error and empty states.

Usage

In your layout wrap your layers inside a ParallaxLayerLayout:

<com.schibsted.spain.parallaxlayerlayout.ParallaxLayerLayout
    android:id="@+id/parallax"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:id="@+id/layer_3"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:background="@color/colorPrimaryDark" />
    <View
        android:id="@+id/layer_2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@color/colorPrimary" />
    <View
        android:id="@+id/layer_1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@color/colorAccent" />
            
    </com.schibsted.spain.parallaxlayerlayout.ParallaxLayerLayout>

In your code create a TranslationUpdater and link it with the ParallaxLayerLayout:

sensorTranslationUpdater = new SensorTranslationUpdater(this);
parallaxLayout.setTranslationUpdater(sensorTranslationUpdater);

// onResume
sensorTranslationUpdater.registerSensorManager();

// onPause
sensorTranslationUpdater.unregisterSensorManager();

Note: Just for the sensor updater you'll need to register and unregister it in your onResume/onPause.

# Download
Grab the latest version from jCenter:

dependencies {
  compile 'com.schibsted.spain:parallax-layer-layout:1.1.1'
}

How it works

The layout applies a maximum offset to each of its views, starting with a base offset and adding one offset increment for each layer.

The higher view in the Z index is the closest one from the user perspective so it will only move with the base offset. The further the view from the user, the more it will move.

The parallax effect is applied based on an horizontal and vertical translations, ranging from -1.0 to 1.0. This translation is provided by an external TranslationUpdater. The library includes some:

  • SensorTranslationUpdater: Updates the translation based on the orientation sensors (roll and pitch).
  • AnimatedTranslationUpdater: Applies the translation automatically as a continuous animation.

Customize

Parallax offset

You can change the default offset for the effect within your xml with the attributes parallaxOffsetBase and parallaxOffsetIncrement:

<com.schibsted.spain.parallaxlayerlayout.ParallaxLayerLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:parallaxOffsetBase="10dp"
    app:parallaxOffsetIncrement="5dp">

The defaul values are 10dp for the base and 5dp for the increment

Vertical and horizontal effect scale

Sometimes you might want to reduce or increase the effect for vertical or horizontal movement.

<com.schibsted.spain.parallaxlayerlayout.ParallaxLayerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:parallaxScaleVertical="0.25"
        >

In this example the views will move vertically a 25% with respect to what they move horizontally.

Note: The parallax scale doesn't scale the views, but rather the parallax movement

Custom Z index

By default the parallax effect is applied in the views Z index. This means that the last view in the layout will be the one closer to the user, and so it will move less; and the first view will be the furthest and will move more.

If you need a different parallax order you can change this behavior by overriding the views index with the attribute layout_parallaxZIndex:

<com.schibsted.spain.parallaxlayerlayout.ParallaxLayerLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/layer_2"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:background="@color/colorPrimaryDark"
        app:layout_parallaxZIndex="1" />

    <View
        android:id="@+id/layer_3"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@color/colorPrimary"
        app:layout_parallaxZIndex="2" />

    <View
        android:id="@+id/layer_1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@color/colorAccent"
        app:layout_parallaxZIndex="0" />

</com.schibsted.spain.parallaxlayerlayout.ParallaxLayerLayout>

Static layer

You can disable the parallax effect in any of the views to stop them from moving with the attribute layout_parallaxEnabled:

<View
    android:id="@+id/layer_static"
    android:layout_width="180dp"
    android:layout_height="180dp"
    android:background="@color/colorPrimaryDark"
    app:layout_parallaxEnabled="false" />

Contribute

For bugs, requests, questions and discussions please use Github Issues.

Attributions

This library was developed by Rafa Vázquez with the idea of moving images from Alex Bailón for the Infojobs' Android application.

Device requirements

This layout needs a gyroscope powered device. Sadly, there are some low-end phones that doesn't have it. In that case, the layered will appear like a static image.

License

Copyright 2016 Schibsted Classified Media Spain S.L.


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.

主要指標

概覽
名稱與所有者AdevintaSpain/Parallax-Layer-Layout
主編程語言Java
編程語言Java, Shell (語言數: 2)
平台
許可證
所有者活动
創建於2016-07-27 08:22:45
推送於2021-02-26 12:03:22
最后一次提交2021-02-26 13:01:23
發布數4
最新版本名稱v1.1.2 (發布於 )
第一版名稱v1.0.0 (發布於 )
用户参与
星數1k
關注者數24
派生數83
提交數30
已啟用問題?
問題數14
打開的問題數7
拉請求數13
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?