欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

帧动画(时间的开始和暂停)

程序员文章站 2022-03-16 21:33:05
...
 ![在这里插入图片描述](https://img-blog.csdn.net/20181016113842853?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMTI3Mjk4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
 在进入程序时自动获取系统当前时间,后单击开始实现秒针的跳转,单击暂停按钮时停止秒针的跳转

layout代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="net.hw.dynamictime.MainActivity">

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="70sp"
        android:textColor="#ff0000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doStart"
            android:text="@string/start" />

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doStop"
            android:text="@string/stop" />
            </LinearLayout>

</LinearLayout>

string代码

<resources>
    <string name="app_name">动态时间</string>
    <string name="start">开始</string>
    <string name="stop">停止</string>
</resources>

Java代码

package net.hw.dynamictime;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity {
    private Thread thread;
    private Handler handler;
    private TextView tvTime;
    private SimpleDateFormat sdf;
    private boolean anniu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);
        //通过资源标识获得控件实例
        tvTime = findViewById(R.id.tv_time);
        //实例化简单日期格式化对象
        sdf = new SimpleDateFormat("hh:mm:ss");
        tvTime.setText(sdf.format(new Date()));
        //创建消息处理器,送的消息进行处理,更新主界面元素(标签)的内容
        handler = new Handler(){
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                if(msg.what ==0x0001){
                    tvTime.setText(sdf.format(new Date()));

                }
            }
        };
        //创建线程,定时发送消息

    }
    public  void doStart(View view){
        anniu =true;
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(anniu){
                    //向主线程发送消息
                    handler.sendEmptyMessage(0x001);
                    //让线程睡眠500毫秒
                    try {
                        Thread.sleep(500);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        });

        //启动线程
        thread.start();
    }
    public void doStop(View view){
        anniu = false;
        thread =null;
    }
    protected void onDestroy(){
        super.onDestroy();
        //销毁线程
        thread = null;


    }
}