Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3657313
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Java

2021-07-13 17:31:54

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 = "";

    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{

        @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){

        }

    }

}

阅读(1161) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~