Skip to content

ImageView with 3D Parallax effect 视差3D效果ImageView

License

Notifications You must be signed in to change notification settings

shenguojun/ParallaxImageView

Repository files navigation

ParallaxImageView

跟随重力感应的视差3D效果ImageView,效果图如下:

demo

引入

步骤一:

root目录中的 build.gradle 加入 jitpack repositories:

allprojects {
  repositories {
    ...
      maven { url 'https://jitpack.io' }
  }
}

步骤二:

添加 dependency,最新版本

dependencies {
  implementation 'com.github.shenguojun:ParallaxImageView:$latestVersion'
}

使用

步骤一:

在XML或代码中创建GravityImageView,由于我们需要预留一些空间来进行左右晃动,其中scale参数表示图片放大的倍数。图片将按照这个比例进行放大,并且在上下左右晃动的时候展示出放大后被遮挡的部分。

<com.shengj.parallaximageview.GravityImageView
    android:id="@+id/image1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:scale="1.25"/>

步骤二:

在使用的Activity或者Fragment中注册GravitySensor并设置回调,GravitySensor将根据页面生命周期对传感器进行暂停。

// 注册声明周期以及传感器回调
lifecycle.addObserver(GravitySensor(this).also {
    it.listener = this
})

步骤三:

将传感器的回调直接设置给GravityImageView,其中x, y是表示跟重力方向一致,-x, -y表示跟重力方向相反

override fun onGravityChange(x: Float, y: Float) {
    binding.image1.onGravityChange(x, y)
    binding.image3.onGravityChange(-x, -y)
}

就这样我们就完成了一个随着重力作用而晃动的视差图了!

License

MIT License

Copyright (c) 2021 Lawrence/申国骏

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.

About

ImageView with 3D Parallax effect 视差3D效果ImageView

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages