关于Scroller的一点问题


一般在用Scroller做滑动的时候,用在ViewGroup中的地方比较多,因为Scroller滑动的是内容,用在View中好像没什么意义


在使用过程中一般都会是这样的代码:

@Override
     public void computeScroll() {
     
         //先判断mScroller滚动是否完成
         if (mScroller.computeScrollOffset()) {
         
             //这里调用View的scrollTo()完成实际的滚动
             scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
             
             //必须调用该方法,否则不一定能看到滚动效果
             postInvalidate();
         }
         super.computeScroll();
     }

在滑动未完成的时候,会一直调用scrollTo方法, 原来以为mScroller.getCurrX()和mScroller.getCurrY()的值不断变化是由于不断调用了scrollTo方法造成的。其实并不是这样

这两个值的操作是在mScroller.computeScrollOffest中完成的,跟scrollTo方法毫无关系


/**
     * Call this when you want to know the new location.  If it returns true,
     * the animation is not yet finished.
     */ 
    public boolean computeScrollOffset() {
        if (mFinished) {
            return false;
        }

        int timePassed = (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime);
    
        if (timePassed < mDuration) {
            switch (mMode) {
            case SCROLL_MODE:
                final float x = mInterpolator.getInterpolation(timePassed * mDurationReciprocal);
                mCurrX = mStartX + Math.round(x * mDeltaX);
                mCurrY = mStartY + Math.round(x * mDeltaY);
                break;
            case FLING_MODE:
                final float t = (float) timePassed / mDuration;
                final int index = (int) (NB_SAMPLES * t);
                float distanceCoef = 1.f;
                float velocityCoef = 0.f;
                if (index < NB_SAMPLES) {
                    final float t_inf = (float) index / NB_SAMPLES;
                    final float t_sup = (float) (index + 1) / NB_SAMPLES;
                    final float d_inf = SPLINE_POSITION[index];
                    final float d_sup = SPLINE_POSITION[index + 1];
                    velocityCoef = (d_sup - d_inf) / (t_sup - t_inf);
                    distanceCoef = d_inf + (t - t_inf) * velocityCoef;
                }

                mCurrVelocity = velocityCoef * mDistance / mDuration * 1000.0f;
                
                mCurrX = mStartX + Math.round(distanceCoef * (mFinalX - mStartX));
                // Pin to mMinX <= mCurrX <= mMaxX
                mCurrX = Math.min(mCurrX, mMaxX);
                mCurrX = Math.max(mCurrX, mMinX);
                
                mCurrY = mStartY + Math.round(distanceCoef * (mFinalY - mStartY));
                // Pin to mMinY <= mCurrY <= mMaxY
                mCurrY = Math.min(mCurrY, mMaxY);
                mCurrY = Math.max(mCurrY, mMinY);

                if (mCurrX == mFinalX && mCurrY == mFinalY) {
                    mFinished = true;
                }

                break;
            }
        }
        else {
            mCurrX = mFinalX;
            mCurrY = mFinalY;
            mFinished = true;
        }
        return true;
    }




mCurrX = mStartX + Math.round(x * mDeltaX);
mCurrY = mStartY + Math.round(x * mDeltaY);


 /**
     * Start scrolling by providing a starting point, the distance to travel,
     * and the duration of the scroll.
     * 
     * @param startX Starting horizontal scroll offset in pixels. Positive
     *        numbers will scroll the content to the left.
     * @param startY Starting vertical scroll offset in pixels. Positive numbers
     *        will scroll the content up.
     * @param dx Horizontal distance to travel. Positive numbers will scroll the
     *        content to the left.
     * @param dy Vertical distance to travel. Positive numbers will scroll the
     *        content up.
     * @param duration Duration of the scroll in milliseconds.
     */
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        mMode = SCROLL_MODE;
        mFinished = false;
        mDuration = duration;
        mStartTime = AnimationUtils.currentAnimationTimeMillis();
        mStartX = startX;
        mStartY = startY;
        mFinalX = startX + dx;
        mFinalY = startY + dy;
        mDeltaX = dx;
        mDeltaY = dy;
        mDurationReciprocal = 1.0f / (float) mDuration;
    }


mDeltaY 是在startScroll的时候赋值的


其实scrollTo,scrollBy跟mScroller是毫无关系的,只是借助mScroller的computeScrollOffset方法完成比较平滑的动画

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Linux上安装Kafka并进行测试,您可以按照以下步骤进行操作: 1. 首先,确保您的Linux系统已经安装Java。您可以在终端中运行以下命令来检查Java安装情况: ``` java -version ``` 如果您没有安装Java,请根据您的Linux发行版进行安装。 2. 下载Kafka。您可以通过访问Kafka的官方网站(https://kafka.apache.org/downloads)来下载最新版本的Kafka。选择合适的二进制文件下载链接。 3. 解压下载的Kafka压缩文件。您可以使用以下命令将其解压到所需的目录中(例如/opt目录): ``` tar -xzf kafka_<version>.tgz -C /opt ``` 4. 进入Kafka目录: ``` cd /opt/kafka_<version> ``` 5. 启动Zookeeper服务。Kafka依赖于Zookeeper来管理集群状态。您可以在启动之前编辑`config/zookeeper.properties`文件以进行必要的配置更改。然后,运行以下命令启动Zookeeper服务: ``` bin/zookeeper-server-start.sh config/zookeeper.properties ``` 6. 启动Kafka服务。在另一个终端窗口中,运行以下命令启动Kafka服务器: ``` bin/kafka-server-start.sh config/server.properties ``` 7. 创建一个主题。您可以使用以下命令创建一个名为`test`的主题: ``` bin/kafka-topics.sh --create --topic test --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 ``` 8. 发布和消费消息。在同一个终端窗口中,使用以下命令发布一些消息: ``` bin/kafka-console-producer.sh --topic test --bootstrap-server localhost:9092 ``` 在另一个终端窗口中,使用以下命令消费消息: ``` bin/kafka-console-consumer.sh --topic test --bootstrap-server localhost:9092 --from-beginning ``` 这样,您就可以在Kafka中发布和消费消息了。 请注意,以上步骤假设您已经在单个节点上安装运行Kafka。如果您希望在多个节点上设置Kafka集群,还需要进行其他配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值