gpt4 book ai didi

类似于 NewsInShorts 应用程序的 Android Vertical Viewpager

转载 作者:行者123 更新时间:2023-11-29 00:05:17 25 4
gpt4 key购买 nike

如何像在 NewsInShort 应用程序中一样在 Android 中创建垂直 View 寻呼机。 https://play.google.com/store/apps/details?id=com.nis.app

这个应用程序包含新闻提要,当我们向上滑动当前新闻时,下一条新闻会随着动画出现,就像我向上滑动当前新闻一点点,然后下一条新闻就会那样显示。向下滑动也是如此。

我在 Github 上找到了一个库。
https://github.com/rharter/ViewPager-Android

但是这个库没有提供像 NewsInShort App 这样的东西。

如果有人曾经创建过这样的 View 或库,请告诉我。

谢谢。

最佳答案

Although it's too late answer, but it could be help someone else who want to create animation (viewpager transformer) like inshorts app.

public class VerticalViewPager extends ViewPager {

public VerticalViewPager(Context context) {
super(context);
init();
}

public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
setPageTransformer(true, new DepthPageTransformer());
setOverScrollMode(OVER_SCROLL_NEVER);
}


/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();

float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;

ev.setLocation(newX, newY);

return ev;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}

public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;

public void transformPage(View view, float position) {
int pageWidth = view.getWidth();

if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);

} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(view.getWidth() * -position);

//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
view.setScaleX(1);
view.setScaleY(1);

} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);

// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);

// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);

} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}

and you need to use VerticalViewPager in xml

关于类似于 NewsInShorts 应用程序的 Android Vertical Viewpager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34300213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com