ParallaxPagerTransformer
介绍:
使用viewpager的PageTransformer实现的视差效果,代码非常简单。项目中有apk文件可以直接运行看效果
运行效果:
使用说明:
实现的原理,因为代码非常简单我就直接把该库的library代码copy出来了:
package com.xgc1986.parallaxPagerTransformer;
import android.support.v4.view.ViewPager;
import android.view.View;
public class ParallaxPagerTransformer implements ViewPager.PageTransformer {
private int id;
private int border = 0;
private float speed = 0.2f;
public ParallaxPagerTransformer(int id) {
this.id = id;
}
@Override
public void transformPage(View view, float position) {
View parallaxView = view.findViewById(id);
if (parallaxView != null) {
if (position > -1 && position < 1) {
float width = parallaxView.getWidth();
parallaxView.setTranslationX(-(position * width * speed));
float sc = ((float)view.getWidth() - border)/ view.getWidth();
if (position == 0) {
view.setScaleX(1);
view.setScaleY(1);
} else {
view.setScaleX(sc);
view.setScaleY(sc);
}
}
}
}
public void setBorder(int px) {
border = px;
}
public void setSpeed(float speed) {
this.speed = speed;
}
}
已下载
0