1、设置动画
模仿一些软件文字慢慢淡出效果,主要用到移动动画(负责view移动),缩放动画(负责view在移动过程中不断的缩小)和透明度动画(在移动的过程中减少文字透明度)。
<span style="font-family:Comic Sans MS;font-size:14px;">int[] location = new int[2];
tv_score_animation.getLocationInWindow(location); //获取当前view的位置</span>
<span style="font-family:Comic Sans MS;font-size:14px;">private void animation(final int addscore, int[] location) {
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0,
-location[1]);
translateAnimation.setDuration(800);
tv_score_animation.setText("+" + addscore);
// 缩放时以x和y坐标轴的中心点开始缩放
final Animation scaleAnimation = new ScaleAnimation(1f, 0.2f, 1f, 0.2f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 设置尺寸变化动画
final Animation alphaAnimation = new AlphaAnimation(1.0f, 0f); // 设置透明度变化动画
scaleAnimation.setDuration(800);
alphaAnimation.setDuration(800);
AnimationSet set = new AnimationSet(true); // 创建动画集对象
set.addAnimation(scaleAnimation); // 添加尺寸变化动画
set.addAnimation(alphaAnimation); // 添加透明度渐变动画
set.addAnimation(translateAnimation); // 添加位置变化动画
set.setFillAfter(true); // 停留在最后的位置
set.setFillEnabled(true);
tv_score_animation.setAnimation(set); // 设置动画
set.startNow(); // 启动动画
}</span>
2、布局文件
这里使用了两个textview控件,一个用于移动,一个则固定在原有的位置记录当前的值。这两个控件所表示的值是不同的,这样就会有移动的textview是从固定的textview中慢慢的退出的。
<span style="font-family:Comic Sans MS;font-size:14px;"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/tv_score_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textColor="#aa000000"
android:textSize="50sp" />
<TextView
android:id="@+id/tv_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="50"
android:textColor="#aa000000"
android:textSize="50sp" />
</RelativeLayout></span>