Timer实现扫描ui定时器
公司让做一个蓝牙扫描连接的东西,但是官网的蓝牙demo程序,扫描连接页面实在太丑了,没办法,自己写一个稍微好看的页面,使用timer做纯ui层的扫描功能(Ps:有一个神奇的点,android简单的动画效果直接使用gif就行,这有点夸张了呀,谷歌好强大,使用image和glide,可以直接显示加载gif的图片,你说离谱不离谱,谷歌太强了)
step1: gif自己去网上找,这是主页功能代码
package com.example.scanuidemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private ImageView ivTime;
private Timer timer;
private TimerTask timerTask;
private int nowProgress;
private TextView tvTimeLeft;
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message message) {
if (message.what == 100) {
int post = (int) message.obj;
tvTimeLeft.setText(String.valueOf(post));
//ui层在这里,功能代码 应该放在这里 比如查找的代码
//一旦超过100s,应该停止查找,cancel timer,关闭执行查找的destroy方法,
if (post==300){
releaseTimer();
}
}
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivTime = findViewById(R.id.iv_time);
tvTimeLeft = findViewById(R.id.tv_left);
nowProgress = 0;
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
nowProgress++;
Message msg = new Message();
msg.what = 100;
msg.obj = nowProgress;
mHandler.sendMessage(msg);
}
};
timer.schedule(timerTask, 0, 1000);
Glide.with(this).load(R.drawable.smart_gif).into(ivTime);
}
private void releaseTimer() {
if (timer != null) {
timer.cancel();
}
if (timerTask != null) {
timerTask.cancel();
}
}
}
step2: 布局页
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f5f5"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_time"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_time"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="0"
android:textColor="@color/black"
android:textSize="26dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingBottom="5dp"
android:text="%"
android:textColor="@color/black"
android:textSize="13dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="设备扫描中…"
android:textColor="#ff7f7f7f"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:text="请确保手机靠近设备,且无线网络畅通"
android:textColor="@color/black"
android:textSize="16dp" />
</LinearLayout>
run,success
end