安卓 AsyncTask 异步任务

实验代码(未修改)

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".AsyncTask1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/img01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp">
        </ImageView>
        <Button
            android:id="@+id/btn_dl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="点击下载图片">
        </Button>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="20dp">
            <Button
                android:id="@+id/left_hand"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_margin="20dp"
                android:text="左手">
            </Button>
            <Button
                android:id="@+id/right_hand"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_margin="20dp"
                android:text="右手">
            </Button>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="20dp">
            <Button
                android:id="@+id/left_foot"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_margin="20dp"
                android:text="左脚">
            </Button>
            <Button
                android:id="@+id/right_foot"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_margin="20dp"
                android:text="右脚">
            </Button>
        </LinearLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

java

package com.kk.laodi;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AsyncTask1 extends AppCompatActivity {
    private Button btn_dl, right_hand, left_hand, right_foot, left_foot;
    private ImageView img01;
    private String image_path = "https://img.lianzhixiu.com/uploads/allimg/202104/9999/7490b1acfa.jpg";
    private ProgressDialog dialog;
    private MyTask myTask;  // 异步任务写外面,不要创建实例。

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

        btn_dl = (Button)this.findViewById(R.id.btn_dl);
        right_hand = (Button)this.findViewById(R.id.right_hand);
        right_foot = (Button)this.findViewById(R.id.right_foot);
        left_hand = (Button)this.findViewById(R.id.left_hand);
        left_foot = (Button)this.findViewById(R.id.left_foot) ;
        img01 = (ImageView)this.findViewById(R.id.img01);


        dialog = new ProgressDialog(this);
        dialog.setTitle("提示");
        dialog.setMessage("正在下载,请稍后...");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  // 对话框里面设置进度条。最后显示对话框就可以。
//        dialog.setCancelable(false);  // 设置不能消失

        btn_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myTask != null && !myTask.isCancelled()) {
                    return;
                }
                myTask = new MyTask();
                myTask.execute(image_path);
            }
        });

        left_hand.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(myTask !=null && myTask.getStatus() == AsyncTask.Status.RUNNING){
                    myTask.cancel(true);
                }
            }
        });
    }

    public class MyTask extends AsyncTask<String, Integer, Bitmap>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            dialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
//            dialog.setProgress(values[0]);

            if(isCancelled()){
                return;
            }

            switch (values[0]) {
                case 1:
                    left_foot.setText("点我");
                    break;
                case 2:
                    left_hand.setText("点我");
                    break;
                case 3:
                    right_foot.setText("点我");
                    break;
                case 4:
                    right_hand.setText("点我");
                    break;
                case 5:
                    left_foot.setText("左脚");
                    break;
                case 6:
                    left_hand.setText("左手");
                    break;
                case 7:
                    right_foot.setText("右脚");
                    break;
                case 8:
                    right_hand.setText("右手");
                    break;
            }
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            /**
             * 完成闪按钮功能
             */
            int i = 0;
            while(i <= 100){

                if(isCancelled()){
                    break;
                }

                publishProgress(i);
                i = i % 8;
                delay_ms(2000);
                publishProgress(i);
                i++;
            }
//
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            dialog.dismiss();
            img01.setImageBitmap(result);
        }
    }

    public void delay_ms(int t) {
        try
        {
            Thread.currentThread().sleep(t);//毫秒
        }
        catch(Exception e){
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值