0

我目前有一个需要在触摸时滚动的视图(不能使用滚动视图或滚动)。目前您可以看到我只是添加了 0.5f,这不是很流畅或有用。我想要与较新android中的下拉导航栏相同的效果(还需要知道每帧当前的Y偏移量,以便它也可以用于计算其他效果)。

任何想法或帮助将不胜感激。先感谢您!

    drawerCardWallet.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
            if(mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();                                 
            }
            else {
                mVelocityTracker.clear();
            }
            mVelocityTracker.addMovement(event);
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {                              

            moveY = event.getRawY();
            if(drawerCardWallet.getY() > topHeaderLayout.getBottom() || cardImage.getY() < (topFooterMainLayout.getHeight()*2) - (cardImage.getHeight()/100*25)) {
                cardCurrentOffset = cardImage.getY();
                drawerWalletCurrentOffset = drawerCardWallet.getY();
                mVelocityTracker.addMovement(event);
                mVelocityTracker.computeCurrentVelocity(1000);
                velocityX = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
                velocityY = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId);

                if(Math.abs(velocityX) + 100 > Math.abs(velocityY)) {
                    // right left
                }
                else {
                    // up down                          
                    if(event.getAction() != MotionEvent.ACTION_OUTSIDE) {                           
                        if(moveY > previousRawY) {
                            // Moving down

                            if(drawerCardWallet.getY() < screenHeight - (topFooterMainLayout.getHeight()*2) - (cardImage.getHeight()/100*25)) {                                 
                                for(float i=0;i<moveY-previousRawY;i++) {
                                    cardCurrentOffset += .5f;
                                    drawerWalletCurrentOffset += .5f;

                                    cardImage.setTranslationY(cardCurrentOffset);
                                    drawerCardWallet.setTranslationY(drawerWalletCurrentOffset);
                                }
                            }
                        }
                        else {
                            // Moving Up

                            if(drawerCardWallet.getY() >= topHeaderLayout.getHeight()+3) {

                                for(float i=0;i<previousRawY-moveY;i++) {
                                    cardCurrentOffset -= .5f;
                                    drawerWalletCurrentOffset -= .5f;

                                    cardImage.setTranslationY(cardCurrentOffset);
                                    drawerCardWallet.setTranslationY(drawerWalletCurrentOffset);
                                }
                            }
                        }

                        previousRawY = moveY;
                    }
                }
            }
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            mVelocityTracker.addMovement(event);                                 
            mVelocityTracker.recycle();
        }

        if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            mVelocityTracker.recycle();
        }
        return true;
    }
});
4

0 回答 0