這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在android中使用viewpager實(shí)現(xiàn)豎屏滑動(dòng)效果,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都做網(wǎng)站、網(wǎng)站建設(shè),成都做網(wǎng)站公司-成都創(chuàng)新互聯(lián)已向成百上千家企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營(yíng)銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價(jià)格為您打造企業(yè)品質(zhì)網(wǎng)站。首先自定義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; } } }
要實(shí)現(xiàn)動(dòng)畫效果的核心就在 setPageTransformer(true, new VerticalPageTransformer());,大家都知道這個(gè)是設(shè)置Viewpager 設(shè)置動(dòng)畫的方法 ,那么要實(shí)現(xiàn)這個(gè)如果用系統(tǒng)自帶的切換動(dòng)畫效果的話 肯定是不符合Viewpager 豎屏滑動(dòng)的效果的。所以自定義PageTransformer 就是必然的趨勢(shì)了
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是什么
Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。
上述就是小編為大家分享的怎么在android中使用viewpager實(shí)現(xiàn)豎屏滑動(dòng)效果了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:怎么在android中使用viewpager實(shí)現(xiàn)豎屏滑動(dòng)效果-創(chuàng)新互聯(lián)
文章來(lái)源:http://jinyejixie.com/article6/issog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、定制網(wǎng)站、域名注冊(cè)、網(wǎng)頁(yè)設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容