开发者

How do you switch 2 different Views by using Drag Gesture in Android?

so im trying to create an interface where the user can drag their finger from the right to the left t开发者_Python百科o get to the second view, and the first view being dragged will move from right to left. very similar to an android device home screen, where the user can drag a screen to the left or right and another screen will emerge from there. I have so far a View switcher where flipper will store two xml files. But i do not know how to use the drag gesture? any examples would be greatly appreciated.

Thank you


Here's a link which u might find usefull..get back to me for any query.

HorizontalPager for Android HorizontalPager is a modified version of Marc Reichelt's RealViewSwitcher. It's essentially a horizontal ScrollView that snaps to a full-width child (like the Android homescreen's switching behavior). This modified version supports vertically scrolling children


I just write one example integrate the drag gesture and image switch effect. Hope it helps.

All the needed resources and code as the following:

  1. main layout : add a ImageSwitcher into the layout

  2. two drawable named as R.drawable.s3, R.drawable.s4

  3. two animation files, slide_right_in, slide_left_out

        public class PureTestActivity extends Activity{
            final String TAG = PureTestActivity.class.getSimpleName();
            /** Called when the activity is first created. */
    
            LinearLayout mainLL;
            GestureDetector gestureDetector;
            ImageSwitcher vs;
            int imageIdx = 0;
            int[] imageResId = {R.drawable.s3, R.drawable.s4};
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
    
    
                mainLL = (LinearLayout)findViewById(R.id.main);
    
                vs = (ImageSwitcher)findViewById(R.id.viewSwitcher1);
                vs.setFactory(new ViewFactory(){
    
            @Override
            public View makeView() {
    
                  ImageView i = new ImageView(PureTestActivity.this);
                  i.setBackgroundColor(0xFF000000);
                  i.setImageResource(R.drawable.s3);
                  i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                  i.setLayoutParams(new ImageSwitcher.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                  return i;
            }
        });
    
    
        vs.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        vs.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        //start for touch events  Gesture detection
        gestureDetector = new GestureDetector(new MyGestureDetector());
        View.OnTouchListener gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
    
                Log.d(TAG, "ontouch event.");
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        vs.setOnTouchListener(gestureListener);
    }     
       class MyGestureDetector extends SimpleOnGestureListener {
    
            final String TAG = MyGestureDetector.class.getSimpleName();
    
            // for touch left or touch right events
            private static final int SWIPE_MIN_DISTANCE = 60;   //default is 120
            private static final int SWIPE_MAX_OFF_PATH = 250;
            private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    
            @Override
            public boolean onDown(MotionEvent e) {
    
                Log.d(TAG, " on down events :" + e.getAction() );
                return true;
            }
    
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    
                Log.d(TAG, " on filing event, first velocityX :" + velocityX +
                        " second velocityY" + velocityY);
                try {
                    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                        return false;
                    // right to left swipe
                    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            onHorizonTouch(true);  // left
    
                    }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            onHorizonTouch(false); // right   
    
                    }
                } catch (Exception e) {
                    // nothing
                }
                return false;
            }
    
            void onHorizonTouch(Boolean toLeft){
                if(toLeft && imageIdx>0)
                {
                    vs.setInAnimation(AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_right_in));
                    // 设置切出动画
                    vs.setOutAnimation(AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_left_out));
                    PureTestActivity.this.vs.setImageResource(imageResId[--imageIdx]);
                }
                if(!toLeft && imageIdx<1)
                {
                    vs.setInAnimation(AnimationUtils.loadAnimation(PureTestActivity.this,
                            android.R.anim.slide_in_left));
                    vs.setOutAnimation(AnimationUtils.loadAnimation(PureTestActivity.this,
                            android.R.anim.slide_out_right));
                    PureTestActivity.this.vs.setImageResource(imageResId[++imageIdx]);
                }
    
            };
        }
    

    }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜