Example usage for android.view GestureDetector onTouchEvent

List of usage examples for android.view GestureDetector onTouchEvent

Introduction

In this page you can find the example usage for android.view GestureDetector onTouchEvent.

Prototype

public boolean onTouchEvent(MotionEvent ev) 

Source Link

Document

Analyzes the given motion event and if applicable triggers the appropriate callbacks on the OnGestureListener supplied.

Usage

From source file:Main.java

private static View.OnTouchListener generateOnTouchListener(Context context) {
    final GestureDetector detector = new GestureDetector(context,
            new GestureDetector.SimpleOnGestureListener() {
                @Override/*  w w w .  j a  va  2s . c o  m*/
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    return true;
                }
            });
    return new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return detector.onTouchEvent(motionEvent);
        }
    };
}

From source file:com.amazonaws.devicefarm.android.referenceapp.Fragments.Tabs.Inputs.Input_GestureFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.input_gesture_fragment, container, false);
    ButterKnife.inject(this, view);
    actionPad.setClickable(true);/*  w  ww.  j  a v a 2 s .  c o m*/
    actionPad.setFocusable(true);

    GestureDetector.SimpleOnGestureListener gestureListener = new GestureListener(gestureContent,
            getActivity());
    final GestureDetector gd = new GestureDetector(getActivity(), gestureListener);

    actionPad.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            gd.onTouchEvent(motionEvent);
            return false;
        }
    });
    return view;
}

From source file:name.raev.kaloyan.android.snowcams.CameraActivity.java

/** Called when the activity is first created. */
@Override//from  www .ja  v  a  2s .  com
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Gesture detection
    final GestureDetector gestureDetector = new GestureDetector(new DoubleTapDetector());
    OnTouchListener gestureListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

    // create view
    setContentView(R.layout.camera);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(new CameraPageAdapter());
    mPager.setPageMargin(8);

    mPager.setOnClickListener(this);
    mPager.setOnTouchListener(gestureListener);

    // find the initial page index - first check if passed with the intent
    int initialPage = getIntent().getIntExtra("index", 0);
    // check if there is a cached state
    Object cachedObject = getLastNonConfigurationInstance();
    if (cachedObject != null) {
        // cache found - scroll to the cached page index
        initialPage = (Integer) cachedObject;
    }

    // scroll to the initial page
    mPager.setCurrentItem(initialPage);
}

From source file:com.example.android.pdfrendererbasic.PdfRendererFragment.java

@Override
public void onStart() {
    super.onStart();
    try {/*from  ww w .  ja v  a2  s .  co m*/
        mPdfView.useBestQuality(true);
        PDFView.Configurator c = (firstTimeStart || currentFile == null ? mPdfView.fromAsset(FILENAME)
                : mPdfView.fromUri(currentFile));
        c.enableSwipe(false) // allows to block changing pages using swipe
                .swipeHorizontal(false).enableDoubletap(false).defaultPage(0).enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null).scrollHandle(null).enableAntialiasing(true) // improve rendering a little bit on low-res screens
                .load();
        gestureListener = new GestureListener(mPdfView, this);
        final GestureDetector gestureDetector = new GestureDetector(getActivity(), gestureListener);
        mPdfView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDetector.onTouchEvent(event);
                return true;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getActivity(), "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

From source file:pw.dedominic.airc.fragment.AddEditServerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_addedit_server, null);

    final GestureDetector gesture = new GestureDetector(getActivity(), new LeftSwipeDetect(getActivity()));

    view.setOnTouchListener(new View.OnTouchListener() {
        @Override//from ww  w  . j a va  2  s .  com
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
    });

    name = (EditText) view.findViewById(R.id.title_text);
    host = (EditText) view.findViewById(R.id.hostname_text);
    port = (EditText) view.findViewById(R.id.port_text);
    nick = (EditText) view.findViewById(R.id.nick_text);
    nick.setText(callback.getSettings().getDefaultNick());
    pass = (EditText) view.findViewById(R.id.pass_text);
    nick_pass = (EditText) view.findViewById(R.id.nickpass_text);
    tls = (CheckBox) view.findViewById(R.id.tls_check);
    saveBtn = (Button) view.findViewById(R.id.save_button);
    cancelBtn = (Button) view.findViewById(R.id.cancel_button);

    saveBtn.setOnClickListener(this);
    cancelBtn.setOnClickListener(this);

    if (getArguments() != null) {
        editServer((Server) getArguments().get("editable"));
    }
    return view;
}

From source file:com.maxleap.mall.fragments.MainFragment.java

private void initBanner(final View view) {
    RelativeLayout bannerLayout = (RelativeLayout) view.findViewById(R.id.banner_layout);
    bannerLayout.setVisibility(View.GONE);
    if (mBanners == null) {
        mBanners = new ArrayList<Banner>();
    }/*from w  w w.j  av a 2  s .co m*/
    if (mBanners.isEmpty()) {
        fetchBannerData(bannerLayout);
    } else {
        bannerLayout.setVisibility(View.VISIBLE);
    }
    viewPager = (ViewPager) view.findViewById(R.id.head_view_pager);
    mBannerAdapter = new BannerAdapter(mContext, mBanners);
    viewPager.setAdapter(mBannerAdapter);
    final Indicator indicatorLayout = (Indicator) view.findViewById(R.id.head_indicator_layout);
    indicatorLayout.setCount(mBanners.size());
    indicatorLayout.select(0);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            indicatorLayout.select(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            mSwipeRefreshLayout.setEnabled(state == ViewPager.SCROLL_STATE_IDLE);
        }
    });
    final GestureDetector tapGestureDetector = new GestureDetector(getActivity(), new TapGestureListener());
    viewPager.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            tapGestureDetector.onTouchEvent(event);
            return false;
        }
    });

}

From source file:com.example.android.basicgesturedetect.BasicGestureDetectFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    View gestureView = getActivity().findViewById(R.id.sample_output);
    gestureView.setClickable(true);/* ww  w.ja va2 s . c o m*/
    gestureView.setFocusable(true);

    // BEGIN_INCLUDE(init_detector)

    // First create the GestureListener that will include all our callbacks.
    // Then create the GestureDetector, which takes that listener as an argument.
    GestureDetector.SimpleOnGestureListener gestureListener = new GestureListener();
    final GestureDetector gd = new GestureDetector(getActivity(), gestureListener);

    /* For the view where gestures will occur, create an onTouchListener that sends
     * all motion events to the gesture detector.  When the gesture detector
     * actually detects an event, it will use the callbacks you created in the
     * SimpleOnGestureListener to alert your application.
    */

    gestureView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            gd.onTouchEvent(motionEvent);
            return false;
        }
    });
    // END_INCLUDE(init_detector)
}

From source file:org.coursera.android.shift.ShiftIconFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.shift_menu, container, false);

    mHead = layout.findViewById(R.id.head);
    final RelativeLayout.LayoutParams iconParams;
    iconParams = (RelativeLayout.LayoutParams) mHead.getLayoutParams();

    final GestureDetector gestureDetector = new GestureDetector(getActivity(), new SingleTapConfirm());

    mHead.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override/*from  w  ww  .  j av  a 2 s .c om*/
        public boolean onTouch(View v, MotionEvent event) {
            final int yLimit = layout.getMeasuredHeight() - mHead.getMeasuredHeight();
            final int xLimit = layout.getMeasuredWidth() - mHead.getMeasuredWidth();

            // single tap
            if (gestureDetector.onTouchEvent(event)) {
                mShiftLauncherView.showShiftMenu(getActivity());
                return true;
            }

            // drag icon
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialX = iconParams.leftMargin;
                initialY = iconParams.topMargin;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                return true;
            case MotionEvent.ACTION_CANCEL:
                return false;
            case MotionEvent.ACTION_UP:
                return true;
            case MotionEvent.ACTION_MOVE:
                int newX = initialX + (int) (event.getRawX() - initialTouchX);
                int newY = initialY + (int) (event.getRawY() - initialTouchY);

                iconParams.leftMargin = newX < 0 ? 0 : Math.min(newX, xLimit);
                iconParams.topMargin = newY < 0 ? 0 : Math.min(newY, yLimit);

                layout.updateViewLayout(mHead, iconParams);
                return true;
            }
            return false;
        }
    });
    return layout;
}

From source file:com.yahoo.mobile.client.android.yodel.ui.ImageGalleryActivity.java

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_gallery);
    setTitle("");

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(toolbar);/*  w w  w .ja  v a 2 s.  c o  m*/
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(getResources().getColor(android.R.color.black));
    }

    final List<Photo> photos = new Gson().fromJson(getIntent().getStringExtra(EXTRA_PHOTO_LIST),
            new TypeToken<List<Photo>>() {
            }.getType());
    mGalleryPagerAdapter = new GalleryPagerAdapter(getSupportFragmentManager(), photos);
    mCaptionPagerIndicator = (CaptionViewPagerIndicator) findViewById(R.id.caption_pager_indicator);
    mCaptionPagerIndicator.setCurrentItem(0, mGalleryPagerAdapter.getCount());
    mCaptionPagerIndicator.setCaption(photos.get(0).getCaption());

    final ViewPager galleryPager = (ViewPager) findViewById(R.id.image_view_pager);
    galleryPager.setAdapter(mGalleryPagerAdapter);
    galleryPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mCaptionPagerIndicator.setCurrentItem(position, mGalleryPagerAdapter.getCount());
            mCaptionPagerIndicator.setCaption(photos.get(position).getCaption());
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    // Delay any activity transition until the ViewPager is ready to be drawn
    supportPostponeEnterTransition();
    galleryPager.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            galleryPager.getViewTreeObserver().removeOnPreDrawListener(this);
            supportStartPostponedEnterTransition();
            return true;
        }
    });
    final GestureDetector clickDetector = new GestureDetector(this,
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    toggleSystemUiVisibility();
                    return true;
                }
            });

    galleryPager.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return clickDetector.onTouchEvent(event);
        }
    });
}