在 Android 开发中,积分签到常需配合动画提升交互体验。点击签到按钮后,显示 +30 积分文字并向上移动逐渐消失。
大致思路: 动画部分由垂直平移和透明度变化两个动画组成。通过 AnimationSet 将两个动画添加到集合,然后开始播放。UI 更新使用 Handler 发送消息处理。
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView mSignIn;
private ImageView redDot;
private TextView signSuccess;
private AnimationSet set;
private String isSign;
private TextView textView;
private Handler mHandler = new Handler() {
private int i = 100;
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
:
i = i + ;
mSignIn.setImageResource(R.drawable.icon_signed);
redDot.setVisibility(View.GONE);
signSuccess.startAnimation(set);
signSuccess.setVisibility(View.GONE);
textView.setText( + i);
;
:
;
}
}
};
{
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSignIn = (ImageView) findViewById(R.id.iv_sign);
redDot = (ImageView) findViewById(R.id.iv_redpoint);
textView = (TextView) findViewById(R.id.tv_score);
signSuccess = (TextView) findViewById(R.id.iv_sign_success);
signSuccess.getLeft();
signSuccess.getTop();
(left, left, top, top - );
translate.setDuration();
(, );
alpha.setDuration();
alpha.setFillAfter();
set = ();
set.addAnimation(translate);
set.addAnimation(alpha);
}
{
signSuccess.setVisibility(View.VISIBLE);
();
message.what = ;
mHandler.sendMessage(message);
}
}


