问题清楚地说明了,我想在用户点击某个按钮时执行TranslateAnimation的速度变慢。
这就是我实例化TranslateAnim的方式,其中-textViewHeight
和layoutHeight
只是我之前已经实例化的一些值:
它从上到下。
TranslateAnim translateAnim = new TranslateAnim(0, 0, -textViewHeight, layoutHeight);
translateAnim.setDuration(20000);
translateAnim.setInterpolator(new LinearInterpolator());
textView.startAnimation(translateAnim);
这是我想减慢TranslateAnim的方法:
它没有像我预期的那样工作。
public void slowDown() {
translateAnim.setStartTime(translateAnim.getStartTime() - 20000);
translateAnim.setDuration((20000 - translateAnim.getElapsedTime()) * 2);
}
我也试过这样做,但仍然没有运气:
translateAnim.setInterpolator(new DecelerateInterpolator());
我的自定义TranslateAnim类扩展了TranslateAnimation:
public class TranslateAnim extends TranslateAnimation {
private long mElapsedAtPause, elapsedTime;
private boolean mPaused = false;
public TranslateAnim(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
super(fromXDelta, toXDelta, fromYDelta, toYDelta);
}
@Override
public boolean getTransformation(long currentTime, Transformation outTransformation) {
if (mPaused && mElapsedAtPause == 0) {
mElapsedAtPause = currentTime - getStartTime();
}
if (mPaused) {
setStartTime(currentTime - mElapsedAtPause);
}
elapsedTime = currentTime - getStartTime();
return super.getTransformation(currentTime, outTransformation);
}
public long getElapsedTime() {
return elapsedTime;
}
public void slowDown() {
translateAnim.setStartTime(translateAnim.getStartTime() - 20000);
translateAnim.setDuration((20000 - translateAnim.getElapsedTime()) * 2);
}
public void pause() {
mElapsedAtPause = 0;
mPaused = true;
}
public void resume() {
mPaused = false;
}
@Override
public void cancel() {
super.cancel();
elapsedTime = 0;
mElapsedAtPause = 0;
mPaused = false;
}
}
有没有解决方法呢?
答案 0 :(得分:1)
使用自定义插值器而不是线性插值器。线性导致它具有相等的时间片。自定义一个可以让中间的时间片更长。
例如:
public class CustomInterpolator implements Interpolator {
public boolean slowMode;
float lastInput;
float lastInputBeforeSlowed;
@Override
public float getInterpolation(float input) {
if (!slowMode) {
//Should be edited
lastInput = input;
return input;
} else {
return (input - lastInputBeforeSlowed) * .5f + lastInputBeforeSlowed;
}
}
public void enterSlowMode() {
slowMode = true;
lastInputBeforeSlowed = lastInput;
}
public void endSlowMode() {
slowMode = false;
//Should be edited
}
}