android实现间隔一秒打印递增数字

new 一个thread

以下是MainActivity.java

package com.example.project0825;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    BindService.MyBinder myBinder;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("--Service connected--");
            myBinder = (BindService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("--Service Disconnected--");
        }
    };

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

    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.bind_service:
                final Intent intent = new Intent(this, BindService.class);
                bindService(intent, conn, Service.BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                myBinder.isStopTHread();
                unbindService(conn);
                break;
            default:
                break;
        }
    }
}


以下是BindService.java

package com.example.project0825;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import androidx.annotation.Nullable;

public class BindService extends Service {
    boolean isStop = false;
    Thread mThread;
    // 定义onBind方法返回的对象
    private MyBinder myBinder = new MyBinder();

    public class MyBinder extends Binder {
        public void isStopTHread() {
            isStop = true;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service is Binded");
        return myBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Service is onCreate");
        mThread = new Thread() {
            @Override
            public void run() {
                super.run();
                int i = 0;
                while (!isStop) {
                    try {
                        System.out.println(i++);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        mThread.start();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("Service is onUnbind");
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Service is onDestroy");
    }
}

附上布局文件activity_main.xml

<?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:orientation="vertical"
    tools:context="com.example.project0825.MainActivity"
    >

    <Button
        android:id="@+id/bind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onViewClick"
        android:text="@string/bind_text"
        />

    <Button
        android:id="@+id/unbind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onViewClick"
        android:text="@string/unbind_text"
        />

</LinearLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小程序中可以使用 `wx.createSelectorQuery` 和 `wx.createAnimation` 方法来实现数字快速滚动的效果。具体实现过程如下: 1. 在 `wxml` 文件中添加一个 `view` 标签用来显示数字,并设置一个唯一的 `id`。 ``` <view id="number">{{number}}</view> ``` 2. 在 `js` 文件中获取 `view` 标签的位置信息,计算出需要滚动的距离,并使用 `wx.createAnimation` 方法创建一个动画实例。 ``` const query = wx.createSelectorQuery() query.select('#number').boundingClientRect(res => { const scrollTop = res.top // 获取当前元素的位置信息 const distance = (192000 - scrollTop) / 10 // 计算需要滚动的距离 const animation = wx.createAnimation({ duration: 100, // 动画持续时间 timingFunction: 'linear', // 动画效果 }) animation.translateY(distance).step() // 设置动画效果 this.setData({ animationData: animation.export(), // 导出动画据 }) }) query.exec() ``` 3. 在需要更新数字的地方使用 `setInterval` 方法,在每次更新数字时调用上面的动画实例。 ``` let number = 0 // 初始数字 function updateNumber() { setInterval(() => { number += 1000 // 每次递增1000 if (number > 192000) { number = 192000 // 数字达到指定值时停止递增 } this.setData({ number: number.toLocaleString(), // 将数字转换为千分位格式 }) const query = wx.createSelectorQuery() query.select('#number').boundingClientRect(res => { const scrollTop = res.top // 获取当前元素的位置信息 const distance = (192000 - scrollTop) / 10 // 计算需要滚动的距离 const animation = wx.createAnimation({ duration: 100, // 动画持续时间 timingFunction: 'linear', // 动画效果 }) animation.translateY(distance).step() // 设置动画效果 this.setData({ animationData: animation.export(), // 导出动画据 }) }) query.exec() }, 1000) } ``` 4. 在 `onLoad` 方法中调用 `updateNumber` 方法,开始更新数字和播放动画。 ``` onLoad: function () { this.updateNumber() }, ``` 这样,数字就会在一秒内从初始值递增到指定值,并快速滚动到指定位置停止。可以根据实际需求调整动画的持续时间、效果等参

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值