android开发类似keep倒计时

先看效果

 

activity:


/**
 * 类似keep倒计时
 */
public class CountActivity extends Activity implements View.OnClickListener {

    private RelativeLayout fl_bg;//开始按钮
    private TextView start_tv;//开始按钮
    private TextView number_tv;//数字

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.count_num);

        fl_bg = findViewById(R.id.fl_bg);
        start_tv = findViewById(R.id.start_tv);
        number_tv = findViewById(R.id.number_tv);
        start_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.start_tv){
            start_tv.setText("");
            ObjectAnimator animator1 = ObjectAnimator.ofFloat(start_tv, "scaleY", 1,4);//沿着Y轴放大
            ObjectAnimator animator2 = ObjectAnimator.ofFloat(start_tv, "scaleX", 1,4);//沿着X轴放大
            AnimatorSet animatorSet = new AnimatorSet();//创建一个动画集合类
//            bouncer.play(animator1).with(animator2);//play:先播放animator with:同时播放animator2 after:在某动画后播放 before:再某动画前播放
            animatorSet.playTogether(animator1,animator2);
            animatorSet.setDuration(1000);//持续时间
            animatorSet.start();//开始动画
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    number_tv.setVisibility(View.VISIBLE);
                    fl_bg.setBackgroundColor(getResources().getColor(R.color.sport_color));
                    doCount();
                }
            });
        }
    }

    private void doCount() {
        final int repeatCount=3;//定义重复字数
        AnimatorSet animatorSet = new AnimatorSet();//创建一个动画集合类
        ObjectAnimator animator1=ObjectAnimator.ofFloat(number_tv, "scaleY",  1f, 0.1f);//沿着Y轴缩小
        animator1.setRepeatCount(3);
        animator1.setDuration(1500);
        animator1.setInterpolator(new AnticipateOvershootInterpolator());
        animator1.addListener(new AnimatorListenerAdapter() {
            int count=repeatCount;// 
            @Override
            public void onAnimationEnd(Animator animation) {
                // 动画结束 隐藏控件
                number_tv.setVisibility(View.GONE);
            }
            @Override
            public void onAnimationRepeat(Animator animation) {
                number_tv.setText(count==0?"GO":""+count);
                count--;
            }

            @Override
            public void onAnimationStart(Animator animation) {
                number_tv.setText(""+count);//设置显示的数字
                count--;
            }
        });
        ObjectAnimator animator2=ObjectAnimator.ofFloat(number_tv, "scaleX",  1f, 0.1f);//沿着X轴缩小
        animator2.setRepeatCount(3);
        animator2.setDuration(1500);
        animator2.setInterpolator(new AnticipateOvershootInterpolator());
        ObjectAnimator animator3=ObjectAnimator.ofFloat(number_tv, "alpha",  5f, 0f);//透明度
        animator3.setRepeatCount(3);
        animator3.setDuration(1500);

        animatorSet.playTogether(animator1,animator2,animator3);

        animatorSet.start();
    }

}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fl_bg"
    android:gravity="center">

    <TextView
        android:gravity="center"
        android:id="@+id/start_tv"
        android:layout_width="500px"
        android:layout_height="500px"
        android:background="@drawable/shap_start_btn"
        android:text="开始"
        android:textColor="@color/white"
        android:textSize="60px" />

    <TextView
        android:visibility="gone"
        android:gravity="center"
        android:id="@+id/number_tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/sport_color"
        android:textColor="@color/white"
        android:textSize="400px" />

</RelativeLayout>

一个drawable文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/sport_color" />
    <corners android:radius="90dp" />
</shape>

颜色:

 <color name="sport_color">#45C48E</color>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值