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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了保持Android应用程序的运行,可以使用以下方法: 1.使用前台服务:前台服务是一种可以在通知栏中显示通知的服务。这样可以让用户知道应用程序正在运行,并且可以随时停止服务。要创建前台服务,请使用startForeground()方法。 ```java public class MyService extends Service { private static final int NOTIFICATION_ID = 1; private static final String CHANNEL_ID = "MyServiceChannel"; @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("My Service") .setContentText("Running...") .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentIntent(pendingIntent) .build(); startForeground(NOTIFICATION_ID, notification); // do your work here return START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel = new NotificationChannel( CHANNEL_ID, "My Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(serviceChannel); } } } ``` 2.使用JobScheduler:JobScheduler是一种系统服务,可以在特定条件下运行作业。例如,当设备处于充电状态时,或者当设备连接到WiFi网络时。要使用JobScheduler,请创建一个JobService类,并在AndroidManifest.xml文件中注册它。 ```java public class MyJobService extends JobService { @Override public boolean onStartJob(JobParameters params) { // do your work here return false; // true if there is still work to do, false otherwise } @Override public boolean onStopJob(JobParameters params) { return true; // true to reschedule the job, false to drop the job } } ``` ```xml <service android:name=".MyJobService" android:permission="android.permission.BIND_JOB_SERVICE" /> ``` 3.使用AlarmManager:AlarmManager是一种系统服务,可以在指定的时间间隔内运行代码。要使用AlarmManager,请创建一个PendingIntent,并使用setRepeating()方法设置重复间隔。 ```java Intent intent = new Intent(this, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_HALF_HOUR, pendingIntent); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值