Search in sources :

Example 86 with MotionEvent

use of android.view.MotionEvent in project JamsMusicPlayer by psaravan.

the class QuickScroll method init.

/**
     * Initializing the QuickScroll, this function must be called.
     * <p/>
     *
     * @param type       the QuickScroll type. Available inputs: <b>QuickScroll.TYPE_POPUP</b> or <b>QuickScroll.TYPE_INDICATOR</b>
     * @param list       the ListView
     * @param scrollable the adapter, must implement Scrollable interface
     */
public void init(final int type, final ListView list, final Scrollable scrollable, final int style) {
    if (isInitialized)
        return;
    this.type = type;
    listView = list;
    this.scrollable = scrollable;
    groupPosition = -1;
    fadeInAnimation = new AlphaAnimation(.0f, 1.0f);
    fadeInAnimation.setFillAfter(true);
    fadeOutAnimation = new AlphaAnimation(1.0f, .0f);
    fadeOutAnimation.setFillAfter(true);
    fadeOutAnimation.setAnimationListener(new AnimationListener() {

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            isScrolling = false;
        }
    });
    isScrolling = false;
    listView.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            if (isScrolling && (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_DOWN)) {
                return true;
            }
            return false;
        }
    });
    final RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    final RelativeLayout container = new RelativeLayout(getContext());
    container.setBackgroundColor(Color.TRANSPARENT);
    containerParams.addRule(RelativeLayout.ALIGN_TOP, getId());
    containerParams.addRule(RelativeLayout.ALIGN_BOTTOM, getId());
    container.setLayoutParams(containerParams);
    if (this.type == TYPE_POPUP || this.type == TYPE_POPUP_WITH_HANDLE) {
        scrollIndicatorTextView = new TextView(getContext());
        scrollIndicatorTextView.setTextColor(Color.WHITE);
        scrollIndicatorTextView.setVisibility(View.INVISIBLE);
        scrollIndicatorTextView.setGravity(Gravity.CENTER);
        final RelativeLayout.LayoutParams popupParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        popupParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        scrollIndicatorTextView.setLayoutParams(popupParams);
        setPopupColor(GREY_LIGHT, GREY_DARK, 1, Color.WHITE, 1);
        setTextPadding(TEXT_PADDING, TEXT_PADDING, TEXT_PADDING, TEXT_PADDING);
        container.addView(scrollIndicatorTextView);
    } else {
        scrollIndicator = createPin();
        scrollIndicatorTextView = (TextView) scrollIndicator.findViewById(ID_PIN_TEXT);
        (scrollIndicator.findViewById(ID_PIN)).getLayoutParams().width = 25;
        container.addView(scrollIndicator);
    }
    // setting scrollbar width
    final float density = getResources().getDisplayMetrics().density;
    getLayoutParams().width = (int) (10 * density);
    scrollIndicatorTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 32);
    // scrollbar setup
    if (style != STYLE_NONE) {
        final RelativeLayout layout = new RelativeLayout(getContext());
        final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.ALIGN_LEFT, getId());
        params.addRule(RelativeLayout.ALIGN_TOP, getId());
        params.addRule(RelativeLayout.ALIGN_RIGHT, getId());
        params.addRule(RelativeLayout.ALIGN_BOTTOM, getId());
        layout.setLayoutParams(params);
        final View scrollbar = new View(getContext());
        scrollbar.setBackgroundColor(GREY_SCROLLBAR);
        final RelativeLayout.LayoutParams scrollBarParams = new RelativeLayout.LayoutParams(1, LayoutParams.MATCH_PARENT);
        scrollBarParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        scrollBarParams.topMargin = SCROLLBAR_MARGIN;
        scrollBarParams.bottomMargin = SCROLLBAR_MARGIN;
        scrollbar.setLayoutParams(scrollBarParams);
        layout.addView(scrollbar);
        ViewGroup.class.cast(listView.getParent()).addView(layout);
        // creating the handlebar
        if (this.type == TYPE_INDICATOR_WITH_HANDLE || this.type == TYPE_POPUP_WITH_HANDLE) {
            handleBar = new View(getContext());
            setHandlebarColor(BLUE_LIGHT, BLUE_LIGHT, BLUE_LIGHT_SEMITRANSPARENT);
            final RelativeLayout.LayoutParams handleParams = new RelativeLayout.LayoutParams((int) (12 * density), (int) (36 * density));
            handleBar.setLayoutParams(handleParams);
            ((RelativeLayout.LayoutParams) handleBar.getLayoutParams()).addRule(RelativeLayout.CENTER_HORIZONTAL);
            layout.addView(handleBar);
            listView.setOnScrollListener(new OnScrollListener() {

                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    if (onScrollListener != null)
                        onScrollListener.onScrollStateChanged(view, scrollState);
                }

                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if (onScrollListener != null)
                        onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                    if (!isScrolling && totalItemCount - visibleItemCount > 0) {
                        moveHandlebar(getHeight() * firstVisibleItem / (totalItemCount - visibleItemCount));
                    }
                }
            });
        }
    }
    isInitialized = true;
    ViewGroup.class.cast(listView.getParent()).addView(container);
}
Also used : LayoutParams(android.view.ViewGroup.LayoutParams) ViewGroup(android.view.ViewGroup) View(android.view.View) AlphaAnimation(android.view.animation.AlphaAnimation) SuppressLint(android.annotation.SuppressLint) MotionEvent(android.view.MotionEvent) OnScrollListener(android.widget.AbsListView.OnScrollListener) AlphaAnimation(android.view.animation.AlphaAnimation) Animation(android.view.animation.Animation) AnimationListener(android.view.animation.Animation.AnimationListener)

Example 87 with MotionEvent

use of android.view.MotionEvent in project FlyRefresh by race604.

the class PullHeaderLayout method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    MotionEvent vtev = MotionEvent.obtain(ev);
    final int actionMasked = MotionEventCompat.getActionMasked(ev);
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);
    switch(actionMasked) {
        case MotionEvent.ACTION_DOWN:
            {
                if ((mIsBeingDragged = !mScroller.isFinished())) {
                    final ViewParent parent = getParent();
                    if (parent != null) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
                /*
                 * If being flinged and user touches, stop the fling. isFinished
                 * will be false if being flinged.
                 */
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                // Remember where the motion event started
                mLastMotionY = (int) ev.getY();
                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
                break;
            }
        case MotionEvent.ACTION_MOVE:
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (activePointerIndex == -1) {
                Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
                break;
            }
            final int y = (int) MotionEventCompat.getY(ev, activePointerIndex);
            int deltaY = mLastMotionY - y;
            if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                deltaY -= mScrollConsumed[1];
                vtev.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }
            if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
                final ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                mIsBeingDragged = true;
                if (deltaY > 0) {
                    deltaY -= mTouchSlop;
                } else {
                    deltaY += mTouchSlop;
                }
            }
            if (mIsBeingDragged) {
                // Scroll to follow the motion event
                mLastMotionY = y - mScrollOffset[1];
                final int scrolledDeltaY = moveBy(deltaY);
                final int unconsumedY = deltaY - scrolledDeltaY;
                if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                    mLastMotionY -= mScrollOffset[1];
                    vtev.offsetLocation(0, mScrollOffset[1]);
                    mNestedYOffset += mScrollOffset[1];
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mIsBeingDragged) {
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
                if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                    flingWithNestedDispatch(-initialVelocity);
                }
                mActivePointerId = INVALID_POINTER;
                endDrag();
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            if (mIsBeingDragged && getChildCount() > 0) {
                mActivePointerId = INVALID_POINTER;
                endDrag();
            }
            break;
        case MotionEventCompat.ACTION_POINTER_DOWN:
            {
                final int index = MotionEventCompat.getActionIndex(ev);
                mLastMotionY = (int) MotionEventCompat.getY(ev, index);
                mActivePointerId = MotionEventCompat.getPointerId(ev, index);
                break;
            }
        case MotionEventCompat.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);
            mLastMotionY = (int) MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
            break;
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return true;
}
Also used : VelocityTracker(android.view.VelocityTracker) ViewParent(android.view.ViewParent) MotionEvent(android.view.MotionEvent)

Example 88 with MotionEvent

use of android.view.MotionEvent in project ToggleDrawable by renaudcerrato.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContainer = (ViewGroup) findViewById(R.id.container_cardview);
    mContainer.getBackground().setAlpha(0);
    mContainer.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() != MotionEvent.ACTION_DOWN)
                return false;
            if (!isFaded)
                return false;
            toggle();
            return true;
        }
    });
    mSeekBar = ((SeekBar) findViewById(R.id.seekbar));
    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            for (ToggleDrawable drawable : mToggleDrawables) drawable.setProgress(progress / (float) seekBar.getMax());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
    add(new SearchArrowDrawable(this));
    add(new SearchCrossDrawable(this));
    add(new DrawerArrowDrawable(this));
    add(new MyCustomToggleDrawable(this));
}
Also used : SearchCrossDrawable(com.mypopsy.drawable.SearchCrossDrawable) SeekBar(android.widget.SeekBar) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) MotionEvent(android.view.MotionEvent) ToggleDrawable(com.mypopsy.drawable.ToggleDrawable) SearchArrowDrawable(com.mypopsy.drawable.SearchArrowDrawable) DrawerArrowDrawable(com.mypopsy.drawable.DrawerArrowDrawable)

Example 89 with MotionEvent

use of android.view.MotionEvent in project fitscales by paulburton.

the class Switch method cancelSuperTouch.

private void cancelSuperTouch(MotionEvent ev) {
    MotionEvent cancel = MotionEvent.obtain(ev);
    cancel.setAction(MotionEvent.ACTION_CANCEL);
    super.onTouchEvent(cancel);
    cancel.recycle();
}
Also used : MotionEvent(android.view.MotionEvent)

Example 90 with MotionEvent

use of android.view.MotionEvent in project Onboarding by eoinfogarty.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    SceneTransformer sceneTransformer = new SceneTransformer();
    ScenePagerAdapter scenePagerAdapter = new ScenePagerAdapter(getSupportFragmentManager(), sceneTransformer);
    binding.tutorialPager.setAdapter(scenePagerAdapter);
    // set limit same as number of fragments
    binding.tutorialPager.setOffscreenPageLimit(3);
    binding.tutorialPager.setPageTransformer(true, sceneTransformer);
    TextPagerAdapter textAdapter = new TextPagerAdapter();
    binding.textPager.setAdapter(textAdapter);
    binding.indicator.setViewPager(binding.textPager);
    binding.indicator.setSnap(true);
    binding.indicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
        // do nothing
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // translate up start button
            if (position == 1) {
                binding.start.setVisibility(View.VISIBLE);
                binding.start.setTranslationY(binding.textPager.getBottom() * (1 - positionOffset));
                binding.indicator.setAlpha(1 - positionOffset);
            }
        }
    });
    // to control the two view pagers at once we put a layout above them that intercepts the touches
    binding.touchInterceptorLayout.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            binding.tutorialPager.onTouchEvent(event);
            binding.textPager.onTouchEvent(event);
            return true;
        }
    });
    binding.start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Start clicked", Toast.LENGTH_SHORT).show();
        }
    });
    if (savedInstanceState != null) {
        // re-add the listeners from the scene fragments
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            sceneTransformer.addSceneChangeListener((SceneTransformer.SceneChangeListener) fragment);
        }
    }
}
Also used : ViewPager(android.support.v4.view.ViewPager) View(android.view.View) Fragment(android.support.v4.app.Fragment) SceneTransformer(redstar.onboarding.adapter.SceneTransformer) MotionEvent(android.view.MotionEvent) ScenePagerAdapter(redstar.onboarding.adapter.ScenePagerAdapter) TextPagerAdapter(redstar.onboarding.adapter.TextPagerAdapter)

Aggregations

MotionEvent (android.view.MotionEvent)717 View (android.view.View)255 TextView (android.widget.TextView)113 ImageView (android.widget.ImageView)91 Point (android.graphics.Point)63 OnTouchListener (android.view.View.OnTouchListener)48 GestureDetector (android.view.GestureDetector)47 Paint (android.graphics.Paint)40 ViewGroup (android.view.ViewGroup)36 Test (org.junit.Test)35 AdapterView (android.widget.AdapterView)34 RecyclerView (android.support.v7.widget.RecyclerView)33 ListView (android.widget.ListView)33 SuppressLint (android.annotation.SuppressLint)31 Intent (android.content.Intent)30 Instrumentation (android.app.Instrumentation)27 AbsListView (android.widget.AbsListView)27 OnClickListener (android.view.View.OnClickListener)24 Rect (android.graphics.Rect)22 Handler (android.os.Handler)20