大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关怎么在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实现竖屏滑动效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。