我正在尝试复制用于Android的 Quartz新闻应用中使用的动画。
这是我用来完成此任务的代码。
//On clicking button start animation
private void handleActionButtonClick(Button actionButton) {
Log.d(TAG, "Action button clicked");
handler.postDelayed(new Runnable() {
@Override
public void run() {
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(messageAdapter.getItemCount() - 1);
if (viewHolder instanceof OutgoingMessageViewHolder) {
final OutgoingMessageViewHolder outgoingTextMessageViewHolder = (OutgoingMessageViewHolder) viewHolder;
View messageView = outgoingTextMessageViewHolder.getTextView();
int[] actionButtonPos = new int[2];
actionButton.getLocationInWindow(actionButtonPos);
int startX = actionButtonPos[0];
int startY = actionButtonPos[1];
int[] newMessagePos = new int[2];
messageView.getLocationInWindow(newMessagePos);
int x = newMessagePos[0] - startX;
int y = newMessagePos[1] - startY;
AnimatorSet flySet = new AnimatorSet();
Animator flyX = ObjectAnimator.ofFloat(actionButton, View.TRANSLATION_X, (float) x);
Animator flyY = ObjectAnimator.ofFloat(actionButton, View.TRANSLATION_Y, (float) y);
flySet.playTogether(flyX, flyY);
flySet.setDuration(450);
flySet.setInterpolator(new OvershootInterpolator(1.0f));
flySet.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
outgoingTextMessageViewHolder.getTextView().setText(actionButton.getText());
outgoingTextMessageViewHolder.itemView.setAlpha(1.0f);
}
});
AnimatorSet flyAndFadeSet = new AnimatorSet();
ObjectAnimator fade = ObjectAnimator.ofFloat(actionButton, View.ALPHA, new float[]{0.0f}).setDuration(200);
flyAndFadeSet.playSequentially(flySet, fade);
final Button button = actionButton;
flyAndFadeSet.addListener(new BaseAnimationListener() {
public void onAnimationEnd(Animator animation) {
button.setAlpha(0.0f);
hideUserActions();
}
});
flyAndFadeSet.start();
hideOtherActions(actionButton, false);
}
}
}, messageAnimator.getAddDuration());
}
但由于结束动画上的位置和消息视图彼此不匹配,我无法获得理想的结果。另外我想知道这是正确的方法。
链接到 Quartz app 实施 Quartz app聊天动画
链接到我的实施图片
我的聊天应用动画