Android异步任务应用

任务要求:
需要提交所有类的Java源文件代码和布局文件代码(xml文件),注意:提交Java源文件代码时选择代码语言为Java,提交布局文件代码时选择代码语言为XML

任务描述:
利用异步任务方式,实现动画效果,界面初始状态为5个白色圆点,每隔1秒有一个圆点变成黑色圆点,当5个圆点都变为黑色以后间隔1秒所有圆点重新变为白色,重复之前的操作,所有圆点重复变化10次以后停止该动画。程序中图片资源链接:https://pan.baidu.com/s/1mdq5Mi_PoPNWtxNzRLK1LA 提取码:627v

activity_main.xml:

<?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="#D21957"
    android:gravity="center">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <ImageView
            android:id="@+id/first"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/point1"/>
        <ImageView
            android:id="@+id/second"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/point1"/>
        <ImageView
            android:id="@+id/third"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/point1"/>
        <ImageView
            android:id="@+id/fourth"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/point1"/>
        <ImageView
            android:id="@+id/fifth"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/point1"/>
    </LinearLayout>
</LinearLayout>

strings.xml:

<resources>
    <string name="app_name">异步任务</string>
</resources>

MainActivity.java:

package com.example.homework06;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private int current;
    private ImageView first;
    private ImageView second;
    private ImageView third;
    private ImageView fourth;
    private ImageView fifth;
    private ImageView[] images;
    private int num;//记录最后一个圆点变化次数

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

        first = findViewById(R.id.first);
        second = findViewById(R.id.second);
        third = findViewById(R.id.third);
        fourth = findViewById(R.id.fourth);
        fifth = findViewById(R.id.fifth);

        images = new ImageView[]{first, second, third, fourth, fifth};

        //启动异步任务
        MyAsyncTack myAsyncTack = new MyAsyncTack();
        myAsyncTack.execute();
    }

    private class MyAsyncTack extends AsyncTask<String,Integer,String> {
        @Override
        protected String doInBackground(String [] objects) {
            while(true) {
                try {
                    Thread.sleep(1000);
                    publishProgress(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(num > 9){
                    break;
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer[] values) {
            if(num < 10 && current < 5){
                images[current].setBackgroundResource(R.drawable.point);
                current++;
            }else {
                current = 0;
                num++;
                for(int i = 0; i < 5;++i){
                    images[i].setBackgroundResource(R.drawable.point1);
                }
            }
            super.onProgressUpdate(values);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FF小迷糊吖~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值