Mr.Alright---MTK时钟定时器滑动不流畅的优化

遇到一个问题,就是mtk的时钟在上滑切换计时器的时候,很难切换,现象如下

带着问题先确定了问题发生的位置,alps/vendor/mediatek/proprietary/packages/apps/DeskClock/src/com/android/deskclock/VerticalViewPager.java中

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final boolean toIntercept = super.onInterceptTouchEvent(flipXY(ev));
        // Return MotionEvent to normal
        flipXY(ev);
        return toIntercept;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final boolean toHandle = super.onTouchEvent(flipXY(ev));
        // Return MotionEvent to normal
        flipXY(ev);
        return toHandle;
    }

    private MotionEvent flipXY(MotionEvent ev) {
        final float width = getWidth();
        final float height = getHeight();

        final float x = (ev.getY() / height) * width;
        final float y = (ev.getX() / width) * height;

        ev.setLocation(x, y);

        return ev;
    }

是一个垂直的viewpager,但是这种方式显然很难用,如视频中的,因此我决定自己拦截滑动事件,自行处理,前提是先把原来的事件给干掉

//    @Override
//    public boolean onTouchEvent(MotionEvent ev) {
//        final boolean toHandle = super.onTouchEvent(flipXY(ev));
//        // Return MotionEvent to normal
//        flipXY(ev);
//        return toHandle;
//    }

接下来,我在alps/vendor/mediatek/proprietary/packages/apps/DeskClock/src/com/android/deskclock/DeskClock.java中定义了触摸的监听,并在dispatchTouchEvent中将事件分发给注册了监听的页面

private ArrayList onTouchListeners = new ArrayList<>();

    public interface ToucherListenerForFragment {
        public boolean onTouch(MotionEvent ev);
    }

    public void registerMyOnTouchListener(ToucherListenerForFragment listener) {
        onTouchListeners.add(listener);
    }

    public void unregisterMyOnTouchListener(ToucherListenerForFragment listener) {
        onTouchListeners.remove(listener);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        for (ToucherListenerForFragment listener : onTouchListeners) {
            listener.onTouch(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

继而我在计时器的页面设置监听器,alps/vendor/mediatek/proprietary/packages/apps/DeskClock/src/com/android/deskclock/timer/TimerFragment.java,并处理滑动的距离

    private DeskClock.ToucherListenerForFragment mToucherListenerForFragment;
    private float preY;
    private int mCurrIndex;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.timer_fragment, container, false);

        /// M: [ALPS03421969] [Sync with N1] replacing fragment mgr with child fragment mgr
        。。。。。。


        // Add ++++++++++++++++++++++++++++++ start ^_^
        mToucherListenerForFragment = ev -> {
            boolean b = false;
            if(ev.getAction() == MotionEvent.ACTION_DOWN) {
                preY = ev.getY();
            } else if (ev.getAction() == MotionEvent.ACTION_UP) {
                int index = 0;
                if (ev.getY() - preY > 10) {
                    index = mCurrIndex - 1 < 0 ? 0 : mCurrIndex - 1;
                    b = true;
                    mCurrIndex = index;
                } else if (preY - ev.getY() > 10) {
                    index = mCurrIndex + 1 == mAdapter.getTimers().size() ? mCurrIndex : mCurrIndex + 1;
                    b = true;
                    mCurrIndex = index;
                }
               
                mViewPager.setCurrentItem(index);
            }
            return b;
        };
        ((DeskClock)getActivity()).registerMyOnTouchListener(mToucherListenerForFragment);
        // Add ++++++++++++++++++++++++++++++++++++++++++++ end ^_^
        return view;
    }


    @Override
    public void onStart() {
        super.onStart();

       。。。。。。

        // If a specific timer should be displayed, display the corresponding timer tab.
        if (showTimerId != -1) {
            final Timer timer = DataModel.getDataModel().getTimer(showTimerId);
            if (timer != null) {
                final int index = DataModel.getDataModel().getTimers().indexOf(timer);
                mViewPager.setCurrentItem(index);

                // Add +++++++++++++++++++++++++++++++  start ^_^
                mCurrIndex = index;
                // Add by +++++++++++++++++++++++++++++++  end ^_^
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        // We may have received a new intent while paused.
        final Intent intent = getActivity().getIntent();
        if (intent != null && intent.hasExtra(TimerService.EXTRA_TIMER_ID)) {
            // This extra is single-use; remove after honoring it.
            final int showTimerId = intent.getIntExtra(TimerService.EXTRA_TIMER_ID, -1);
            intent.removeExtra(TimerService.EXTRA_TIMER_ID);

            final Timer timer = DataModel.getDataModel().getTimer(showTimerId);
            if (timer != null) {
                // A specific timer must be shown; show the list of timers.
                final int index = DataModel.getDataModel().getTimers().indexOf(timer);
                mViewPager.setCurrentItem(index);
                // Add +++++++++++++++++++++++++++++++++++++++ start ^_^
                mCurrIndex = index;
                // Add ++++++++++++++++++++++++++++++++++++++++ end ^_^

                animateToView(mTimersView, null, false);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        DataModel.getDataModel().removeTimerListener(mAdapter);
        DataModel.getDataModel().removeTimerListener(mTimerWatcher);

        // Add ++++++++++++++++++++++++++++++++++++++++ start ^_^
        ((DeskClock)getActivity()).unregisterMyOnTouchListener(mToucherListenerForFragment);
        // Add ++++++++++++++++++++++++++++++++++++++++ end ^_^
    }

其中我们用到了alps/vendor/mediatek/proprietary/packages/apps/DeskClock/src/com/android/deskclock/timer/TimerPagerAdapter.java中的,getTimer方法,我们需要把这个方法改成public的

 /*private*/ public List getTimers() {
        return DataModel.getDataModel().getTimers();
    }

到此,我们就可以很流畅的滑动定时器啦!

Mr.Alright---MTK时钟定时器滑动不流畅的优化_第1张图片

你可能感兴趣的:(系统修改,android,定时器优化,垂直的viewpager)