我有一個應用程序,我需要根據當前聚焦的子視圖調整視圖位置(這是一個列表具有可聚焦項目,當前焦點項目必須在屏幕中心 - 用於電視應用程序從電視遙控器控制)。
位置必須用動畫調整。
我得到它只有一個問題:如果用戶在動畫完成之前改變焦點(快速點擊「向上」按鈕兩次)下一個動畫以「跳躍」開始 - 它從與第一個動畫相同的位置開始。中斷翻譯動畫,並開始另一個
所以我試圖做的是我取消以前的動畫並開始另一個動畫,但是隨後新動畫從第一個動畫開始的位置開始,所以用戶可以在動畫中看到非常明顯的跳躍,看起來非常糟糕。
下面的代碼:
@Override
public void requestChildFocus(final View child, final View focused) {
super.requestChildFocus(child, focused);
//this test code included for explanation
Rect r = new Rect();
child.getDrawingRect(r); //this will return view's position ignoring animation state
Rect r2 = new Rect();
child.getGlobalVisibleRect(r2); //as will this one too
Log.d("Top: " + child.getTop() + "; Drawing rect: " + r.toString() + "; global visible rect: " + r2.toString());
//all of this methods will ignore changes that were made
//by animation object - they'll return numbers from LayoutParam
//calculate current position inside view and position to move to
//cursorOffset - is the "center" of the screen
final int currentPosition = child.getTop();
final int requaredPosition = cursorOffset - focused.getTop();
//cancel current running animation - layout params will not change
//if i do change layout params when cancelling animation, it looks even worse
//because of jumping back list jumps forward
if (currentAnimation != null) {
Animation animation = currentAnimation;
currentAnimation = null;
animation.cancel();
}
//just a regular translate animation
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, requaredPosition - currentPosition);
animation.setDuration(300);
animation.setFillEnabled(true);
animation.setFillBefore(true);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
currentAnimation = animation;
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
if (animation == currentAnimation) {
//change layout params if animation finished running (wasn't cancelled)
RelativeLayout.LayoutParams params = (LayoutParams) child.getLayoutParams();
params.setMargins(0, requaredPosition, 0, 0);
child.setLayoutParams(params);
}
}
});
child.startAnimation(animation);
}
所以必須質疑的是:我怎麼能開始從以前的翻譯留在(假設它被取消)動畫點在哪裏翻譯動畫?
或者用簡單的話來說,我怎樣才能確定一個視圖的當前可見矩形?