设为首页 加入收藏

TOP

优质Android小部件:索尼滚动相册(二)
2017-10-13 10:30:50 】 浏览:10147
Tags:优质 Android 部件 索尼 滚动 相册
; ); mCellCalculator.setStatic(); mImageLoader.loadCurrentLargeBitmap(); } break; default: break; } return true; } //缓慢拖动 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { mScrollDistance += distanceY; if(mScrollDistance > 0 && !mScrollRollBack){ mImageLoader.rollBackward(); mScrollRollBack = true; } else if(mScrollDistance < 0 && mScrollRollBack){ mImageLoader.rollForward(); mScrollRollBack = false; } LOG("OnGestureListener onScroll " + distanceY + " all" + mScrollDistance); mRollResult = mCellCalculator.setStatus(-mScrollDistance); invalidate(); return true; } //快速拖动 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (Math.abs(velocityY) > MIN_FLING) { LOG("OnGestureListener onFling " + velocityY); if (mExecutorService == null) { mExecutorService = Executors.newSingleThreadExecutor(); } mIsFling = true; mExecutorService.submit(new FlingTask(velocityY)); } return true; } //利用一个异步任务来处理滚动多张Images private class FlingTask implements Runnable { float mVelocity; float mViewHeight; int mSleepTime; boolean mRollBackward; FlingTask(float velocity) { mRollBackward = velocity < 0 ? true : false; mVelocity = Math.abs(velocity / 4); mViewHeight = RollImageView.this.getHeight() / 2; mSleepTime = (int)(4000 / Math.abs(velocity) * 100); //the slower velocity of fling, the longer interval for roll } @Override public void run() { int i = 0; try{ while (mVelocity > mViewHeight) { mCellCalculator.setStatus(mRollBackward ? -mViewHeight : mViewHeight); mHandler.sendEmptyMessage(MSG_INVALATE); //determines the count of roll. The using of mViewHeight has no strictly logical mVelocity -= mViewHeight; if (((i++) & 1) == 0) { //roll forward once for every two setStatus if(mRollBackward){ mImageLoader.rollBackward(); }else { mImageLoader.rollForward(); } } Thread.sleep(mSleepTime); } mCellCalculator.setStatic(); mImageLoader.loadCurrentLargeBitmap(); mHandler.sendEmptyMessage(MSG_INVALATE); } catch(Exception e){ } finally{ } } } View Code

 CellCalculater分析

  首先阐明下向前移动/向后移动的概念。需要显示的图片路径存储为一个List,假设显示在最前的图片的索引为index,则当前显示的图片为[index,index+3],向前则表示index加1,向后则表示index减1.

  CellCalculater的计算情形主要在于用户通过手势操作,表达了需要向前或者向后移动一张图片的意图。在View中能够获取到的只是手势移动的距离,所以在CellCalculater中需要对传进来的移动距离进行处理,输出移动结果。在我的实现中,当移动距离超过图片高度一半的时候,就表示显示的图片需要移动一位,否则当手势操作结束的时候就设置为static状态。主要代码如下:  

    public DefaultCellCalculator(int showCnt){
        mCnt = showCnt;
        mCells = new Cell[mCnt];
        mAlphas = new float[mCnt];
        STATIC_ALPHA = new int[mCnt];
        STATIC_ALPHA[mCnt - 1] = 0; //最后一张图的透明度为0
        int alphaUnit = (255 - FIRST_ALPHA) / (mCnt - 2);
        for(int i = mCnt - 2; i >= 0; i--){  //定义静态时每张图的透明度
            STATIC_ALPHA[i] = FIRST_ALPHA + (mCnt - 2 - i) * alphaUnit;
        }
    }

    @Override
    public Cell[] getCells() {
        return  mCells;
    }
    
    //用户手势移动,distance表示移动距离,正负值分别意味着需要向前/向后移动
    @Override
    public int setStatus(float distance) {
        if(distance > 0){
            return calculateForward(distance);
        } else if(distance < 0){
            return calculateBackward(distance);
        } else{
            initCells();
        }
        return 0;
    }

    //
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android 高级面试题及答案 下一篇Android防微信首页左右滑动切换

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目