2012-12-04 40 views
10

Tôi đang cố gắng sử dụng AccelerateDecelerateInterpolator và tùy chỉnh nó. Tôi có thể thấy rằng Interpolators như DecelerateInterpolator có một trường "yếu tố" để bạn có thể thay đổi hành vi của nó. nhưng AccelerateDecelerateInterpolator không có. Khi tôi đang sử dụng AccelerateDecelerateInterpolator, tôi gần như thậm chí không thể nhận thấy rằng nội suy đang làm bất cứ điều gì. Hoạt ảnh trông rất tuyến tính. Vì vậy, có cách nào để yếu tố AccelerateDecelerateInterpolator hoặc thay đổi nó trong bất kỳ cách nào? Cảm ơnTùy chỉnh android AccelerateDecelerateInterpolator

Trả lời

9

Bạn có thể thực hiện chức năng nới lỏng tiêu chuẩn với Interpolator của riêng bạn. Ví dụ, đây sẽ là việc thực hiện cho easeInOutQuint:

public class MVAccelerateDecelerateInterpolator implements Interpolator { 

    // easeInOutQuint 
    public float getInterpolation(float t) { 
     float x = t*2.0f; 
     if (t<0.5f) return 0.5f*x*x*x*x*x; 
     x = (t-0.5f)*2-1; 
     return 0.5f*x*x*x*x*x+1; 
    } 
} 

enter image description here

+0

FYI, hàm easeInOutQuint có thể được tìm thấy tại easings.net. –

1

Tôi đã cố gắng xác định TimeInterpolator của riêng mình dưới dạng AccelerateDecelerateInterpolator tùy chỉnh. Tôi không hài lòng với kết quả, nhưng nó có thể đưa ra một vài ý tưởng. Ngoài ra còn có các yếu tố trong mã: 0,05f. Hãy khám phá:

TimeInterpolator interpolator = new TimeInterpolator() { 
     @Override 
     public float getInterpolation(float input) { 
      return input + 0.05f * (float) Math.sin(2 * Math.PI * input); 
     } 
    }; 

Đây là giải pháp tinh tế hơn. Tangent có lẽ là một chức năng tốt hơn cho công việc, hơn là sin. Giải pháp này acelerates lúc bắt đầu và cuối cùng. Ngoài ra còn có một yếu tố để xác định giảm tốc.

TimeInterpolator interpolator = new TimeInterpolator() { 
     private final float mFactor = 1.1f; // less than pi/2 
     private float  oldRetVal; 
     private float  oldInputVal; 
     private double  initValue = Math.tan(-mFactor); 
     private double  endValue = 2 * Math.tan(mFactor); 

     @Override 
     public float getInterpolation(float input) { 
      if (oldInputVal != input) { 
       oldInputVal = input; 
       oldRetVal = (float) ((Math.tan(mFactor * (2 * input - 1)) - initValue)/endValue); 
      } 
      return oldRetVal; 
     } 
    }; 
1

Android đã thêm PathInterpolatorCompat vào thư viện hỗ trợ v4. Bây giờ sử dụng điều này: https://gist.github.com/ebabel/8ff41cad01e9ce1dd9ce bạn có thể chỉ định một easyInOutQuint, easeInOutQuart hoặc dễ dàngInOutExpo một cách dễ dàng!

public static void expand(final View v) { 
     v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     final int targetHeight = v.getMeasuredHeight(); 

     if (v.getHeight() != targetHeight) { 
      // Older versions of android (pre API 21) cancel animations for views with a height of 0 so use 1 instead. 
      v.getLayoutParams().height = 1; 
      v.setVisibility(View.VISIBLE); 


      Animation a = new Animation() { 
       @Override 
       protected void applyTransformation(float interpolatedTime, Transformation t) { 
        v.getLayoutParams().height = interpolatedTime == 1 
          ? ViewGroup.LayoutParams.WRAP_CONTENT 
          : (int) (targetHeight * interpolatedTime); 
        v.requestLayout(); 
       } 

       @Override 
       public boolean willChangeBounds() { 
        return true; 
       } 
      }; 

      a.setInterpolator(EasingsConstants.easeInOutQuart); 
      a.setDuration(computeDurationFromHeight(v)); 
      v.startAnimation(a); 
     } else { 
      Log.d("AnimationUtil", "expand Already expanded "); 
     } 
    } 

/** 
* 1dp/ms * multiplier 
*/ 
private static int computeDurationFromHeight(View v) { 
    return (int) (v.getMeasuredHeight()/v.getContext().getResources().getDisplayMetrics().density) * DURATION_MULTIPLIER; 
} 

Và đừng quên build.gradle của bạn:

compile "com.android.support:support-v4:22.2.0" 
+0

phương thức mở rộng được sao chép từ anh chàng này: http://stackoverflow.com/a/13381228/247325 –

Các vấn đề liên quan