任务要求:
需要提交所有类的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);
}
}
}