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
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;
}
}
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;
}
};
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"
phương thức mở rộng được sao chép từ anh chàng này: http://stackoverflow.com/a/13381228/247325 –
- 1. Android tùy chỉnh stackview
- 2. Máy ảnh tùy chỉnh android
- 3. Nút tùy chỉnh nút chuyển đổi Android
- 4. Android tạo tùy chỉnh menu tràn
- 5. Android phông chữ tùy chỉnh từ xaViews
- 6. Tùy chỉnh màu của ListView Android?
- 7. Thanh tiến trình tùy chỉnh trong Android?
- 8. Hình ảnh hộp kiểm tùy chỉnh android
- 9. Android ProgressBar UI bố trí tùy chỉnh
- 10. Android lưu mảng đối tượng tùy chỉnh
- 11. Thanh tác vụ Android tùy chỉnh
- 12. Android SeekBar Tùy chỉnh ngón tay cái
- 13. Phông chữ tùy chỉnh trong Android
- 14. tạo tài khoản tùy chỉnh android
- 15. Biểu đồ động tùy chỉnh trong Android
- 16. Android: hình dạng nút tùy chỉnh
- 17. Android - Màu nền AlertDialog tùy chỉnh
- 18. Tạo hộp thoại tùy chỉnh trong Android
- 19. Android: Bố cục Spinner Tùy chỉnh
- 20. Android - phím và bàn phím tùy chỉnh
- 21. Bố cục tùy chỉnh Android cho AutoCompleteTextView
- 22. Android: Tùy chỉnh TextView Inflate Exception
- 23. thoại tùy chỉnh Android hiển thị tiêu đề android thanh
- 24. Android - Chụp ảnh và lưu chúng với tên tùy chỉnh đến đích tùy chỉnh qua Intent
- 25. Cách tùy chỉnh các mục danh sách trong Android AlertDialog
- 26. cách tạo lịch tùy chỉnh trong ứng dụng android?
- 27. Android: Nút tùy chỉnh OnClickListener không được gọi
- 28. Cách tạo thanh tiến trình tùy chỉnh trong Android?
- 29. Cách tùy chỉnh bánh mì nướng trong Android
- 30. Vấn đề nhấp chuột đối tượng tùy chỉnh trong android
FYI, hàm easeInOutQuint có thể được tìm thấy tại easings.net. –