整个布局平滑上/下.
检查此链接上的应用程序inshorts和murmur.
我试过这段代码……
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() { // The majority of the magic happens here setPagetransformer(true,new VerticalPagetransformer()); // The easIEst way to get rID of the overscroll drawing that happens on the left and right setoverScrollMode(OVER_SCRolL_NEVER); } private class VerticalPagetransformer implements Pagetransformer { @Suppresslint("NewAPI") @OverrIDe public voID transformPage(VIEw vIEw,float position) { if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. vIEw.setAlpha(0); } else if (position <= 1) { // [-1,1] vIEw.setAlpha(1); // Counteract the default slIDe Transition vIEw.setTranslationX(vIEw.getWIDth() * -position); //set Y position to swipe in from top float yposition = position * vIEw.getHeight(); vIEw.setTranslationY(yposition); } else { // (1,+Infinity] // This page is way off-screen to the right. vIEw.setAlpha(0); } } } /** * 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)); }}
在MainActivity.java中
VerticalVIEwPager Pager2;PagerAdapter adapter;String[] articleTitle;String[] articlename;String[] articlediscription;OnCreate()Pager2=(VerticalVIEwPager)findVIEwByID(R.ID.pager);// Pass results to VIEwPagerAdapter Classadapter = new VIEwPagerAdapter(getActivity(),articleTitle,articlename,articlediscription,btnBack,articleImage);// Binds the Adapter to the VIEwPagerPager2.setAdapter(adapter);
activity_main.xml中
<com.example.flipnews.VerticalVIEwPager androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:ID="@+ID/pager" />
在我的代码中简单的向上轻扫,就像这个link.
但我想创建更好的动画效果,如上面提到的应用程序.
或手机内置照片库效果.
提前致谢.
解决方法 我经过多次研究后找到了解决方案,希望对其他人有所帮助提示:将视图寻呼机背景颜色设置为黑色以获得更好的滑动效果.
private static class VerticalPagetransformer implements Pagetransformer { private static 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(1); vIEw.setScaleX(1); vIEw.setScaleY(1); float yposition = position * vIEw.getHeight(); vIEw.setTranslationY(yposition); vIEw.setTranslationX(-1 * vIEw.getWIDth() * position); } else if (position <= 1) { // (0,1] // Fade the page out. vIEw.setAlpha(1 - position); vIEw.setTranslationX(-1 * vIEw.getWIDth() * 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); } } } 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)); }}总结
以上是内存溢出为你收集整理的Android:布局动画,如Inshorts新闻应用全部内容,希望文章能够帮你解决Android:布局动画,如Inshorts新闻应用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)