android中怎么利用Scroller实现过渡滑动效果
创始人
2024-12-20 06:42:09
0次

主要介绍一下Scroller这个类,它可以实现过渡滑动的效果,使滑动看起来不是那么生硬,当然它用大量的重绘来实现,invalidate();通过源码看:

看构造方法

/**   * Create a Scroller with the default duration and interpolator.   */  public Scroller(Context context) {    this(context, null);  }  /**   * Create a Scroller with the specified interpolator. If the interpolator is   * null, the default (viscous) interpolator will be used. "Flywheel" behavior will   * be in effect for apps targeting Honeycomb or newer.   */  public Scroller(Context context, Interpolator interpolator) {    this(context, interpolator,        context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB);  }  /**   * Create a Scroller with the specified interpolator. If the interpolator is   * null, the default (viscous) interpolator will be used. Specify whether or   * not to support progressive "flywheel" behavior in flinging.   */  public Scroller(Context context, Interpolator interpolator, boolean flywheel) {    mFinished = true;    if (interpolator == null) {      mInterpolator = new ViscousFluidInterpolator();    } else {      mInterpolator = interpolator;    }    mPpi = context.getResources().getDisplayMetrics().density * 160.0f;    mDeceleration = computeDeceleration(ViewConfiguration.getScrollFriction());    mFlywheel = flywheel;    mPhysicalCoeff = computeDeceleration(0.84f); // look and feel tuning  }

我们用默认的就行,传个context就行了,其他的什么差值器,先不管了

然后调用startScroll,传递我们歧视滑动位置和滑动的偏移量,还有可选的默认持续时间,默认为250毫秒这个方法是用来赋值的,接下来会调用invalidate()进行重新绘制,然后就会onDraw(),这时候会调用computeScroll()这个方法,我们重写这个方法,computeScrollOffset()是判断动画有没有结束的一个方法,没结束的时候,我们根据滑动的偏移位置进行移动也就是scrollto到scroller的当前位置,再次调用invalidate(),由此无数的重回进行拼接形成了平滑的滑动

/**   * 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;  }

public void startScroll(int startX, int startY, int dx, int dy) {    startScroll(startX, startY, dx, dy, DEFAULT_DURATION);  } 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;  }

public class MoveFreeView extends View{  private int movedX;  private int movedY;  private Scroller mScroller;  public MoveFreeView(Context context) {    super(context);  }  public MoveFreeView(Context context, @Nullable AttributeSet attrs) {    super(context, attrs);    mScroller = new Scroller(context);  }  public MoveFreeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);  }  @Override  public boolean onTouchEvent(MotionEvent event) {    //获取触摸点到边界坐标    int x = (int) event.getX();    int y = (int) event.getY();    switch (event.getAction()){      case MotionEvent.ACTION_DOWN:        movedX = x;        movedY = y;        break;      case MotionEvent.ACTION_MOVE:        int offsetX = x-movedX;        int offsetY = y-movedY;        layout(getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY);        break;    }    return super.onTouchEvent(event);  }  //供外界调用通过传递x,y的的滑动距离  public void smoothScrollTo(int destinyX,int destinyY){    //向右侧,下方滑动,请传递负值    int scrollX = getScrollX();    int scrollY = getScrollY();    int delta = destinyX - scrollX;    int deltaY = destinyY - scrollY;    mScroller.startScroll(scrollX,scrollY,delta,deltaY,5000);    invalidate();  }  @Override  public void computeScroll() {    super.computeScroll();    //true则表示滑动未结束    if (mScroller.computeScrollOffset()){      ((View) getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());      invalidate();    }  }}

private MoveFreeView button;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    button = (MoveFreeView) findViewById(R.id.custon);    button.smoothScrollTo(-400,-300);    //    button.startAnimation(AnimationUtils.loadAnimation(this,R.anim.translate));//    ObjectAnimator animtor1 = ObjectAnimator.ofFloat(button, "translationX", 0, 300);//    ObjectAnimator animtor2 = ObjectAnimator.ofFloat(button, "translationY", 0, 300);//    ObjectAnimator animator3 = ObjectAnimator.ofFloat(button,"rotationX",0.0f,360f);//    ObjectAnimator animator4 = ObjectAnimator.ofFloat(button,"scaleX",1.5f,0.5f);//    AnimatorSet set= new AnimatorSet();//    set.setDuration(5000);//    set.playTogether(animtor1,animtor2,animator3,animator4);//    set.addListener(new Animator.AnimatorListener() {//      @Override//      public void onAnimationStart(Animator animator) {////      }////      @Override//      public void onAnimationEnd(Animator animator) {//        //动画结束时做一些事情//      }////      @Override//      public void onAnimationCancel(Animator animator) {////      }////      @Override//      public void onAnimationRepeat(Animator animator) {////      }//    });//    set.start();  }}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...