您好,登录后才能下订单哦!
这期内容当中小编将会给大家带来有关怎么在android中使用viewpager实现竖屏滑动效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
首先自定义Viewpager
public class VerticalViewPager extends ViewPager { private OnItemClickListener mOnItemClickListener; public VerticalViewPager(Context context) { super(context); init(); } public VerticalViewPager(Context context, AttributeSet attrs) { super(context, attrs); init(); setup(); } 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 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; } private float scaleY ; @Override public boolean onTouchEvent(MotionEvent ev) { System.out.println("----------getY" + ev.getY()); System.out.println("----------scaleY" + scaleY); if (ev.getAction()==MotionEvent.ACTION_MOVE){ } if (ev.getAction()==MotionEvent.ACTION_DOWN){ scaleY = ev.getY(); } if (ev.getAction()==MotionEvent.ACTION_UP){ if (scaleY == ev.getY()){ System.out.println("------------------======"); scaleY= 0; return false; } } try { return super.onTouchEvent(swapXY(ev)); }catch (Exception e){ } return true; } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } public interface OnItemClickListener { void onItemClick(int position); } private void setup() { final GestureDetector tapGestureDetector = new GestureDetector(getContext(), new TapGestureListener()); setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { tapGestureDetector.onTouchEvent(event); return false; } }); } private class TapGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapConfirmed(MotionEvent e) { if(mOnItemClickListener != null) { mOnItemClickListener.onItemClick(getCurrentItem()); } return true; } } }
要实现动画效果的核心就在 setPageTransformer(true, new VerticalPageTransformer());,大家都知道这个是设置Viewpager 设置动画的方法 ,那么要实现这个如果用系统自带的切换动画效果的话 肯定是不符合Viewpager 竖屏滑动的效果的。所以自定义PageTransformer 就是必然的趋势了
private class VerticalPageTransformer implements ViewPager.PageTransformer { private float MIN_SCALE = 0.5f; @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 <= 0) { // [-1,1] view.setAlpha(1 + position); view.setTranslationX(-view.getWidth() * position); view.setPivotY( view.getMeasuredHeight()); view.setPivotX( view.getMeasuredWidth()/2); view.setTranslationY( position * view.getHeight()); float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position)); view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); } else if (position <= 1) { // [-1,1] view.setAlpha(1); view.setTranslationX(-view.getWidth() * position); view.setTranslationY( position * view.getHeight()); float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position)); } else { // (1,+Infinity] view.setAlpha(0); view.setTranslationY( position * view.getHeight()); } } }
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
上述就是小编为大家分享的怎么在android中使用viewpager实现竖屏滑动效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。