• 微信号:Zh-y9213
您当前的位置:爱资源分享网 > > 网站源码 > 程序源码

Android实现当ImageView在屏幕中滚动时体现视差效果

栏目:程序源码时间:2021-10-23 03:33阅读数:57人阅读

ScrollParallaxImageView

ScrollParallaxImageView扩展ImageView,并提供视差效果,当它在屏幕上滚动。它可以在任何视图,可以滚动其内容,如ListView,RecyclerView,ScrollView等。

屏幕截图

包括ScrollParallaxImageView到您的项目

用gradle:

 
dependencies {
   compile 'com.gjiazhe:scrollparallaximageview:1.0'
}

使用ScrollParallaxImageView在布局文件就像ImageView

 
<com.gjiazhe.scrollparallaximageview.ScrollParallaxImageView
    android:id="@+id/img"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:src="@drawable/pic1"
    android:scaleType="centerCrop" />

在Java代码中设置视差样式

 
ScrollParallaxImageView iv = (ScrollParallaxImageView) findViewById(R.id.img);
iv.setParallaxStyles(new VerticalMovingStyle()); // or other parallax styles

在库中有几个视差样式,但你也可以自定义。只需创建一个类实现接口ScrollParallaxImageView.ParallaxStyle

 
public class MyParallaxStyle implements ScrollParallaxImageView.ParallaxStyle {
  @Override
    public void transform(ScrollParallaxImageView view, Canvas canvas, int x, int y) {
        // Transform the view here, x and y stands for the coordinate of
        // ScrollParallaxImageView's top-left corner.
    }

    @Override
    public void onAttachedToImageView(ScrollParallaxImageView view) {
        // Do something when the style is set to a ScrollParallaxImageView.
    }

    @Override
    public void onDetachedFromImageView(ScrollParallaxImageView view) {
        // Do something when the style is remove from a ScrollParallaxImageView.
    }
}


资源均来自第三方,谨慎下载,前往第三方网站下载

本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:737597453@qq.com