问题描述
我正在尝试创建一些东西,使其在viewpager上显示我的页面,以使其具有良好的感觉和设计,我检查了此,它在某种程度上有所帮助,并遵循了 link 关于如何实现它,但最终遇到了一些问题.首先,对于在API11以下运行的设备(API8是我的目标),它将无法正常工作或看起来效果不佳.其次,我无法使重点页面比下一页和上一页大.为了设计,我希望它看起来像这样:
I'm trying to create something that will display my pages on a viewpager for it to have nice feel and design and I checked out this link which somehow helps and also follow this link on how I can implement it but I ended up with some issues. First is that it won't really work or look that good for devices that runs below API11 (API8 is my target). and Second is that I can't manage to make the focused page to be bigger than the next and previous page. I wanted it look something like this for the sake of design:
希望有人可以在此方面或其他任何方式上帮助我.
Hope someone can help me on this or any other way to achieve this.
推荐答案
阅读首先.然后应用您自己的PageTransformer
实现.像这样:
Read this first. Then apply your own PageTransformer
implementation. Something like this:
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.5f;
private static final float MAX_SCALE = 0.8f;
private static final float MIN_FADE = 0.2f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) {
view.setAlpha(MIN_FADE);
} else if (position < 0) {
view.setAlpha(1 + position * (1 - MIN_FADE));
view.setTranslationX(-pageWidth * MAX_SCALE * position);
ViewCompat.setTranslationZ(view, position);
float scaleFactor = MIN_SCALE
+ (MAX_SCALE - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else if (position == 0) {
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(MAX_SCALE);
ViewCompat.setTranslationZ(view, 0);
view.setScaleY(MAX_SCALE);
} else if (position <= 1) {
ViewCompat.setTranslationZ(view, -position);
view.setAlpha(1 - position * (1 - MIN_FADE));
view.setTranslationX(pageWidth * MAX_SCALE * -position);
float scaleFactor = MIN_SCALE
+ (MAX_SCALE - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else {
view.setAlpha(MIN_FADE);
}
}
}
您还需要控制子绘制顺序以将当前选定的视图设置在顶部(仅在您的视图彼此重叠时才需要):
You will also need to control the child drawing order to set the current selected View on top (only needed, if your views overlay each other):
public class MyPager extends ViewPager {
public MyPager(Context context) {
this(context, null);
}
public MyPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected int getChildDrawingOrder(int childCount, int i) {
if(i == childCount - 1) {
return getCurrentItem();
} else {
return i >= getCurrentItem()? i + 1 : i;
}
}
}
如果屏幕上有更多可见的页面,请将ViewPager.setOffscreenPageLimit
设置为2或更大.
Set ViewPager.setOffscreenPageLimit
to 2 or even more, if you have more visible pages on screen.
这是结果:
这篇关于如何实现显示上一页和下一页以及重点突出的页面的ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!