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.
}
}