下面列出了怎么用android.graphics.Interpolator的API类实例代码及写法,或者点击链接到github查看源代码。
public void run() {
long now = AnimationUtils.currentAnimationTimeMillis();
if (now >= mFadeStartTime) {
int nextFrame = (int) now;
int framesCount = 0;
Interpolator interpolator = mScrollBarInterpolator;
interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
nextFrame += mScrollBarFadeDuration;
interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
mState = ScrollAnimatorState.FADING;
mHost.invalidate();
}
}
public void run() {
long now = AnimationUtils.currentAnimationTimeMillis();
if (now >= fadeStartTime) {
// the animation fades the scrollbars out by changing
// the opacity (alpha) from fully opaque to fully
// transparent
int nextFrame = (int) now;
int framesCount = 0;
Interpolator interpolator = scrollBarInterpolator;
// Start opaque
interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
// End transparent
nextFrame += scrollBarFadeDuration;
interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
state = FADING;
// Kick off the fade animation
// host.invalidate(true);
host.invalidate();
}
}
private void drawScrollBars(Canvas canvas) {
boolean invalidate = false;
if (mIsHandlingTouchEvent) {
mThumbDrawable.setAlpha(255);
} else {
if (!mIsAlwaysVisible) {
final ScrollAnimator cache = mScrollCache;
final ScrollAnimatorState state = cache.mState;
if (state == ScrollAnimatorState.OFF) {
return;
}
if (state == ScrollAnimatorState.FADING) {
if (cache.mInterpolatorValues == null) {
cache.mInterpolatorValues = new float[1];
}
float[] values = cache.mInterpolatorValues;
if (cache.mScrollBarInterpolator.timeToValues(values) == Interpolator.Result.FREEZE_END) {
cache.mState = ScrollAnimatorState.OFF;
} else {
mThumbDrawable.setAlpha(Math.round(values[0]));
}
invalidate = true;
} else {
mThumbDrawable.setAlpha(255);
}
}
}
if (updateThumbRect(0)) {
final int scrollY = getScrollY();
final int scrollX = getScrollX();
mThumbDrawable.setBounds(mThumbRect.left + scrollX, mThumbRect.top + scrollY, mThumbRect.right + scrollX,
mThumbRect.bottom + scrollY);
mThumbDrawable.draw(canvas);
}
if (invalidate) {
invalidate();
}
}
private void onDrawScrollBars(Canvas canvas) {
boolean invalidate = false;
if (mIsHanlingTouchEvent) {
mThumbDrawable.setAlpha(255);
} else {
// Copy from View.class
final ScrollabilityCache cache = mScrollCache;
// cache.scrollBar = mThumbDrawable;
final int state = cache.state;
if (state == ScrollabilityCache.OFF) {
return;
}
if (state == ScrollabilityCache.FADING) {
// We're fading -- get our fade interpolation
if (cache.interpolatorValues == null) {
cache.interpolatorValues = new float[1];
}
float[] values = cache.interpolatorValues;
// Stops the animation if we're done
if (cache.scrollBarInterpolator.timeToValues(values) == Interpolator.Result.FREEZE_END) {
cache.state = ScrollabilityCache.OFF;
} else {
// in View.class is "cache.scrollBar.mutate()"
mThumbDrawable.setAlpha(Math.round(values[0]));
}
invalidate = true;
} else {
// reset alpha, in View.class is "cache.scrollBar.mutate()"
mThumbDrawable.setAlpha(255);
}
}
// Draw the thumb
if (updateThumbRect(0)) {
final int scrollY = mView.getScrollY();
final int scrollX = mView.getScrollX();
mThumbDrawable.setBounds(mThumbRect.left + scrollX, mThumbRect.top + scrollY, mThumbRect.right + scrollX,
mThumbRect.bottom + scrollY);
mThumbDrawable.draw(canvas);
}
if (invalidate) {
mView.invalidate();
}
}
public InterpolatorAssert(Interpolator actual) {
super(actual, InterpolatorAssert.class);
}