พฤติกรรมแปลก ๆ ของ ValueAnimator: ส่งกลับค่าลบ

ดังนั้นฉันจึงพยายามใช้ ValueAnimator เพื่อทำให้ค่าเคลื่อนไหวจาก 100 เป็น 0 เช่นนี้

animator = ValueAnimator.ofInt((int) expansion * 100, targetExpansion * 100);
animator.setInterpolator(interpolator);
animator.setDuration(duration);

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        final int animatedValue = (int) valueAnimator.getAnimatedValue();
        Log.w(">>>>>>>", "animatedValue: " + animatedValue);
    }
});

animator.addListener(new ExpansionListener(targetExpansion));

animator.start();

โดยที่ส่วนขยายและ targetExpansion อาจเป็น 1/0 หรือ 0/1 และโค้ดนี้ให้ผลลัพธ์ดังต่อไปนี้

08-25 11:43:42.776 23457-23457/myapp W/>>>>>>>: animatedValue: 100
08-25 11:43:42.777 23457-23457/myapp W/>>>>>>>: animatedValue: 100
08-25 11:43:42.795 23457-23457/myapp W/>>>>>>>: animatedValue: 91
08-25 11:43:42.816 23457-23457/myapp W/>>>>>>>: animatedValue: 81
08-25 11:43:42.835 23457-23457/myapp W/>>>>>>>: animatedValue: 73
08-25 11:43:42.855 23457-23457/myapp W/>>>>>>>: animatedValue: 65
08-25 11:43:42.874 23457-23457/myapp W/>>>>>>>: animatedValue: 58
08-25 11:43:42.894 23457-23457/myapp W/>>>>>>>: animatedValue: 50
08-25 11:43:42.913 23457-23457/myapp W/>>>>>>>: animatedValue: 44
08-25 11:43:42.933 23457-23457/myapp W/>>>>>>>: animatedValue: 38
08-25 11:43:42.952 23457-23457/myapp W/>>>>>>>: animatedValue: 32
08-25 11:43:42.972 23457-23457/myapp W/>>>>>>>: animatedValue: 27
08-25 11:43:42.991 23457-23457/myapp W/>>>>>>>: animatedValue: 22
08-25 11:43:43.011 23457-23457/myapp W/>>>>>>>: animatedValue: 18
08-25 11:43:43.031 23457-23457/myapp W/>>>>>>>: animatedValue: 13
08-25 11:43:43.050 23457-23457/myapp W/>>>>>>>: animatedValue: 10
08-25 11:43:43.070 23457-23457/myapp W/>>>>>>>: animatedValue: 6
08-25 11:43:43.089 23457-23457/myapp W/>>>>>>>: animatedValue: 3
08-25 11:43:43.109 23457-23457/myapp W/>>>>>>>: animatedValue: 0
08-25 11:43:43.128 23457-23457/myapp W/>>>>>>>: animatedValue: -2
08-25 11:43:43.148 23457-23457/myapp W/>>>>>>>: animatedValue: -4
08-25 11:43:43.167 23457-23457/myapp W/>>>>>>>: animatedValue: -6
08-25 11:43:43.187 23457-23457/myapp W/>>>>>>>: animatedValue: -8
08-25 11:43:43.206 23457-23457/myapp W/>>>>>>>: animatedValue: -9
08-25 11:43:43.226 23457-23457/myapp W/>>>>>>>: animatedValue: -10
08-25 11:43:43.245 23457-23457/myapp W/>>>>>>>: animatedValue: -11
08-25 11:43:43.265 23457-23457/myapp W/>>>>>>>: animatedValue: -12
08-25 11:43:43.285 23457-23457/myapp W/>>>>>>>: animatedValue: -12
08-25 11:43:43.305 23457-23457/myapp W/>>>>>>>: animatedValue: -13
08-25 11:43:43.324 23457-23457/myapp W/>>>>>>>: animatedValue: -13
08-25 11:43:43.344 23457-23457/myapp W/>>>>>>>: animatedValue: -13
08-25 11:43:43.363 23457-23457/myapp W/>>>>>>>: animatedValue: -12
08-25 11:43:43.383 23457-23457/myapp W/>>>>>>>: animatedValue: -12
08-25 11:43:43.402 23457-23457/myapp W/>>>>>>>: animatedValue: -12
08-25 11:43:43.421 23457-23457/myapp W/>>>>>>>: animatedValue: -11
08-25 11:43:43.441 23457-23457/myapp W/>>>>>>>: animatedValue: -11
08-25 11:43:43.460 23457-23457/myapp W/>>>>>>>: animatedValue: -10
08-25 11:43:43.480 23457-23457/myapp W/>>>>>>>: animatedValue: -9
08-25 11:43:43.499 23457-23457/myapp W/>>>>>>>: animatedValue: -8
08-25 11:43:43.519 23457-23457/myapp W/>>>>>>>: animatedValue: -8
08-25 11:43:43.539 23457-23457/myapp W/>>>>>>>: animatedValue: -7
08-25 11:43:43.559 23457-23457/myapp W/>>>>>>>: animatedValue: -6
08-25 11:43:43.578 23457-23457/myapp W/>>>>>>>: animatedValue: -5
08-25 11:43:43.598 23457-23457/myapp W/>>>>>>>: animatedValue: -4
08-25 11:43:43.617 23457-23457/myapp W/>>>>>>>: animatedValue: -3
08-25 11:43:43.637 23457-23457/myapp W/>>>>>>>: animatedValue: -3
08-25 11:43:43.656 23457-23457/myapp W/>>>>>>>: animatedValue: -2
08-25 11:43:43.676 23457-23457/myapp W/>>>>>>>: animatedValue: -1
08-25 11:43:43.695 23457-23457/myapp W/>>>>>>>: animatedValue: -1
08-25 11:43:43.715 23457-23457/myapp W/>>>>>>>: animatedValue: 0
08-25 11:43:43.735 23457-23457/myapp W/>>>>>>>: animatedValue: 0
08-25 11:43:43.754 23457-23457/myapp W/>>>>>>>: animatedValue: 0
08-25 11:43:43.774 23457-23457/myapp W/>>>>>>>: animatedValue: 0
08-25 11:43:43.793 23457-23457/myapp W/>>>>>>>: animatedValue: 0

และคำถามก็คือ: เหตุใดจึงสร้างค่าลบแปลก ๆ ที่ตั้งค่าตั้งแต่ 0 ถึง 0 ซึ่งโดยตัวมันเองนั้นใหญ่กว่าปกติ (33 รายการ) แล้วมีประโยชน์ (18 รายการ) ตั้งค่า ยิ่งไปกว่านั้น สิ่งเดียวกันนี้เกิดขึ้นเมื่อฉันพยายามเปลี่ยนจาก 0 เป็น 100:

08-25 11:53:48.377 23457-23457/myapp W/>>>>>>>: animatedValue: 0
08-25 11:53:48.390 23457-23457/myapp W/>>>>>>>: animatedValue: 0
08-25 11:53:48.409 23457-23457/myapp W/>>>>>>>: animatedValue: 9
08-25 11:53:48.432 23457-23457/myapp W/>>>>>>>: animatedValue: 19
08-25 11:53:48.451 23457-23457/myapp W/>>>>>>>: animatedValue: 27
08-25 11:53:48.472 23457-23457/myapp W/>>>>>>>: animatedValue: 35
08-25 11:53:48.490 23457-23457/myapp W/>>>>>>>: animatedValue: 43
08-25 11:53:48.513 23457-23457/myapp W/>>>>>>>: animatedValue: 50
08-25 11:53:48.529 23457-23457/myapp W/>>>>>>>: animatedValue: 56
08-25 11:53:48.549 23457-23457/myapp W/>>>>>>>: animatedValue: 63
08-25 11:53:48.569 23457-23457/myapp W/>>>>>>>: animatedValue: 68
08-25 11:53:48.588 23457-23457/myapp W/>>>>>>>: animatedValue: 73
08-25 11:53:48.607 23457-23457/myapp W/>>>>>>>: animatedValue: 78
08-25 11:53:48.628 23457-23457/myapp W/>>>>>>>: animatedValue: 83
08-25 11:53:48.646 23457-23457/myapp W/>>>>>>>: animatedValue: 87
08-25 11:53:48.666 23457-23457/myapp W/>>>>>>>: animatedValue: 90
08-25 11:53:48.686 23457-23457/myapp W/>>>>>>>: animatedValue: 94
08-25 11:53:48.705 23457-23457/myapp W/>>>>>>>: animatedValue: 97
08-25 11:53:48.725 23457-23457/myapp W/>>>>>>>: animatedValue: 100
08-25 11:53:48.744 23457-23457/myapp W/>>>>>>>: animatedValue: 102
08-25 11:53:48.764 23457-23457/myapp W/>>>>>>>: animatedValue: 104
08-25 11:53:48.783 23457-23457/myapp W/>>>>>>>: animatedValue: 106
08-25 11:53:48.803 23457-23457/myapp W/>>>>>>>: animatedValue: 108
08-25 11:53:48.822 23457-23457/myapp W/>>>>>>>: animatedValue: 109
08-25 11:53:48.842 23457-23457/myapp W/>>>>>>>: animatedValue: 110
08-25 11:53:48.862 23457-23457/myapp W/>>>>>>>: animatedValue: 111
08-25 11:53:48.881 23457-23457/myapp W/>>>>>>>: animatedValue: 112
08-25 11:53:48.901 23457-23457/myapp W/>>>>>>>: animatedValue: 112
08-25 11:53:48.920 23457-23457/myapp W/>>>>>>>: animatedValue: 113
08-25 11:53:48.940 23457-23457/myapp W/>>>>>>>: animatedValue: 113
08-25 11:53:48.959 23457-23457/myapp W/>>>>>>>: animatedValue: 113
08-25 11:53:48.979 23457-23457/myapp W/>>>>>>>: animatedValue: 112
08-25 11:53:48.998 23457-23457/myapp W/>>>>>>>: animatedValue: 112
08-25 11:53:49.017 23457-23457/myapp W/>>>>>>>: animatedValue: 112
08-25 11:53:49.037 23457-23457/myapp W/>>>>>>>: animatedValue: 111
08-25 11:53:49.058 23457-23457/myapp W/>>>>>>>: animatedValue: 111
08-25 11:53:49.076 23457-23457/myapp W/>>>>>>>: animatedValue: 110
08-25 11:53:49.096 23457-23457/myapp W/>>>>>>>: animatedValue: 109
08-25 11:53:49.116 23457-23457/myapp W/>>>>>>>: animatedValue: 108
08-25 11:53:49.135 23457-23457/myapp W/>>>>>>>: animatedValue: 108
08-25 11:53:49.154 23457-23457/myapp W/>>>>>>>: animatedValue: 107
08-25 11:53:49.174 23457-23457/myapp W/>>>>>>>: animatedValue: 106
08-25 11:53:49.194 23457-23457/myapp W/>>>>>>>: animatedValue: 105
08-25 11:53:49.213 23457-23457/myapp W/>>>>>>>: animatedValue: 104
08-25 11:53:49.233 23457-23457/myapp W/>>>>>>>: animatedValue: 103
08-25 11:53:49.252 23457-23457/myapp W/>>>>>>>: animatedValue: 103
08-25 11:53:49.272 23457-23457/myapp W/>>>>>>>: animatedValue: 102
08-25 11:53:49.291 23457-23457/myapp W/>>>>>>>: animatedValue: 101
08-25 11:53:49.311 23457-23457/myapp W/>>>>>>>: animatedValue: 101
08-25 11:53:49.330 23457-23457/myapp W/>>>>>>>: animatedValue: 100
08-25 11:53:49.350 23457-23457/myapp W/>>>>>>>: animatedValue: 100
08-25 11:53:49.369 23457-23457/myapp W/>>>>>>>: animatedValue: 100
08-25 11:53:49.389 23457-23457/myapp W/>>>>>>>: animatedValue: 100
08-25 11:53:49.408 23457-23457/myapp W/>>>>>>>: animatedValue: 100

ดูเหมือนเอฟเฟกต์การตีกลับซึ่งฉันไม่คาดหวังและไม่ต้องการ


person Stan    schedule 25.08.2017    source แหล่งที่มา
comment
คุณใช้ LinearInterpolator กับแอนิเมชั่นของคุณหรือไม่?   -  person waleedsarwar86    schedule 25.08.2017
comment
คุณใช้อินเทอร์โพเลเตอร์อะไรอยู่?   -  person Rafal Malek    schedule 25.08.2017
comment
คุณใช้ BounceInterpolator หรือไม่? ถ้าเป็นเช่นนั้น นั่นคือสาเหตุว่าทำไมสิ่งนี้ถึงเกิดขึ้น.. ตัวประมาณค่าที่การเปลี่ยนแปลงจะเด้งกลับในตอนท้าย   -  person Ricardo    schedule 25.08.2017
comment
'ตัวแก้ไข Interpolator ส่วนตัว = FastOutSlowInInterpolator ใหม่ ();' มันอาจเป็นเหตุผลก็ได้   -  person Stan    schedule 25.08.2017
comment
ลองใช้ Interpolator มาตรฐาน และตรวจสอบว่าค่ายังคงเกินขีดจำกัดหรือไม่   -  person Ricardo    schedule 25.08.2017
comment
พยายามตามปกติ AccelerateDecelerateInterpolator แต่ผลลัพธ์ก็เหมือนเดิม   -  person Stan    schedule 25.08.2017


คำตอบ (1)


ปัญหาของคุณอยู่ใน Interpolator ที่คุณใช้อยู่ FastOutSlowInInterpolator อาจเกินขีดจำกัดระหว่างการแก้ไข

จากเอกสาร:

getInterpolation() - ส่งคืน: float: ค่าประมาณค่า ค่านี้อาจมากกว่า 1.0 สำหรับตัวสอดแทรกที่ยิงเกินเป้าหมาย หรือน้อยกว่า 0 สำหรับตัวสอดแทรกที่ยิงต่ำกว่าเป้าหมาย

person Ricardo    schedule 25.08.2017
comment
ที่จริงแล้วมีการตั้งค่าโค้ดไว้ OvershootInterpolator และฉันไม่ได้พูดถึงมัน - person Stan; 25.08.2017