<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="100" android:fromYDelta="80%p" android:interpolator="@android:anim/linear_interpolator" android:toYDelta="0" /><?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="100" android:fromYDelta="0" android:interpolator="@android:anim/linear_interpolator" android:toYDelta="-80%p" /><?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/tvSend" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="SEND" android:textColor="#ffffff" android:textSize="12sp" /> <TextView android:id="@+id/tvDone" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="✓" android:textColor="#ffffff" android:textSize="12sp" /> </merge>
public class SendCommentButton extends ViewAnimator implements View.OnClickListener { public static final int STATE_SEND = 0; public static final int STATE_DONE = 1; private static final long RESET_STATE_DELAY_MILLIS = 2000; private int currentState; private OnSendClickListener onSendClickListener; private Runnable revertStateRunnable = new Runnable() { @Override public void run() { setCurrentState(STATE_SEND); } }; public SendCommentButton(Context context) { super(context); init(); } public SendCommentButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { LayoutInflater.from(getContext()).inflate(R.layout.view_send_comment_button, this, true); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); currentState = STATE_SEND; super.setOnClickListener(this); } @Override protected void onDetachedFromWindow() { removeCallbacks(revertStateRunnable); super.onDetachedFromWindow(); } public void setCurrentState(int state) { if (state == currentState) { return; } currentState = state; if (state == STATE_DONE) { setEnabled(false); postDelayed(revertStateRunnable, RESET_STATE_DELAY_MILLIS); setInAnimation(getContext(), R.anim.slide_in_done); setOutAnimation(getContext(), R.anim.slide_out_send); } else if (state == STATE_SEND) { setEnabled(true); setInAnimation(getContext(), R.anim.slide_in_send); setOutAnimation(getContext(), R.anim.slide_out_done); } showNext(); } @Override public void onClick(View v) { if (onSendClickListener != null) { onSendClickListener.onSendClickListener(this); } } public void setOnSendClickListener(OnSendClickListener onSendClickListener) { this.onSendClickListener = onSendClickListener; } @Override public void setOnClickListener(OnClickListener l) { //Do nothing, you have you own onClickListener implementation (OnSendClickListener) } public interface OnSendClickListener { public void onSendClickListener(View v); } }
这个博客介绍了如何使用ViewAnimator实现按钮的切换动画。通过XML定义滑动动画,并在Java代码中设置动画效果和状态切换,实现了点击按钮时显示SEND和已完成(✓)状态的平滑过渡。同时,博客还提供了自定义监听器以便处理点击事件。
384

被折叠的 条评论
为什么被折叠?



