安卓界面设计动态时间逐帧动画

         学习安卓界面有一段时间的同学,会发现大多是图片静止的,没有动画。一下为时间动态为例,与动态动画是同样的思维步骤。逐帧动画有两种实现方式:利用动画资源文件实现、利用Thread和Handler来实现。

目录

1、运行效果

2、涉及的知识点

3、实现思路

​4、布局文件activity_main.xml文件

5、主界面类MainActivity


1、运行效果

2、涉及的知识点

1、活动(Activity)

2、按钮(Button)

3、单击(onClick)

4、线程(Thread)

5、消息处理器(Handler)

3、实现思路

        逐帧动画,其实就要定时切换图片,定时操作可以利用子线程来完成,切换图片就需要事先将所有图片的资源标识保存到一个数组里(这也是一个难点,尤其当图片数量比较大时),通过改变图片资源标识数组的当前索引达到切换图片的目的。

但是,从安卓4.0版本以后,出于安全考虑,子线程不能直接更新主界面元素。怎么办呢?

这就需要一种能沟通主线程和子线程的机制,而消息处理器Handler正是这样一种机制,实现不同线程间的通信。在子线程里,定时更新图片当前索引,然后通过handler的sendEmptyMessage方法将更新后的图片当前索引发送到主线程,最后,在主线程里,通过handler的handleMessage方法获取子线程发送过来的图片当前索引,利用这个索引获取当前图片的资源标识,作为参数传给图像控件的setImageRource方法,就达到更新图片的目的。

4、布局文件activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="com.example.a13468.dynamictime.MainActivity">

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

      />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        android:onClick="doStart"/>

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"
            android:onClick="doStop"
            android:layout_marginLeft="40dp"/>
    </LinearLayout>

</LinearLayout>

5、主界面类MainActivity

package com.example.a13468.dynamictime;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
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 Button start;
    private  Button stop;
    private SimpleDateFormat sdf;
    private boolean isRunning;

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

        tvTime = (TextView) findViewById(R.id.tv_time);
        start=(Button) findViewById(R.id.start);
        stop=(Button ) findViewById(R.id.stop);

        sdf = new SimpleDateFormat("hh:mm:ss");
        tvTime.setText(sdf.format(new Date()));//获取当前时间

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0x0001) {
                    tvTime.setText(sdf.format(new Date()));
                }
            }
        };
    }


    public void doStart(View view) {
        //创建线程,定时发送消息
        isRunning=true;
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRunning) {

                    handler.sendEmptyMessage(0x0001);
                    //让线程睡眠500毫秒
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //启动线程
        thread.start();
    }
public void doStop(View view){
        isRunning=false;
        //销毁线程
        thread = null;

}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值