0

目前,我有一个类来下载 Asynctask 的图像扩展。我想实现一个小的进度条来向用户显示在该空间中加载的图像。我试图将 Asynctask 类放在类活动中。现在的问题是调用类我需要类是静态的。但是如果我把静态类我不能控制进度对话框。

我的班级下载图片:

public class DownloadImages extends Activity{

    public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
//      progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

        public DownloadImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;

            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
            Log.e("TAG","IMAGEN CARGADA");
        }

    //    protected void onPreExecute(){
    //    }
    }
}

要创建 ProgressBar,我认为我需要将类作为 Activity:

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

这是我为我的问题创建的解决方案:

public class DownloadImages extends Activity{

    ProgressBar progressBar = new ProgressBar(DownloadImages.this, null, android.R.attr.progressBarStyleSmall);

    public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;

            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
            Log.e("TAG","IMAGEN CARGADA");
        }

        protected void onPreExecute(){
            progressBar.setVisibility(View.VISIBLE);
        }
    }
}

现在的问题是如果我调用DownloadImage,这个类必须是静态的,但是如果我做静态我不能使用progressBar,除非它被声明为静态,如果我认为静态我不能用我的活动创建它。

有什么解决办法吗?

谢谢,对不起我的英语不好

4

2 回答 2

0

只需将您的ProgressBar实例传递给DownloadImage任务构造函数。

就像这样(我假设您从活动创建 AsyncTask,因此this将指向它):

Progressbar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleSmall);
DownloadImage downloadImage = new DownloadImage(someImageView, progressBar);

并更改您的构造函数,以便它可以ProgressBar作为参数获取。

于 2012-11-02T10:25:17.793 回答
0

使用 Wea​​kReference 获取您从中调用 DownloadImage 类的 Activity 的 Context,ProgressBar 在其构造函数的第一个参数中需要 Context。

public class DownloadImages extends Activity{

ProgressBar progressBar = new ProgressBar(DownloadImages.this, null, android.R.attr.progressBarStyleSmall);

public static class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    private WeakReference<DownloadImages> mWeakReference;

    public DownloadImage(DownloadImages downloadImages, ImageView bmImage) {
        this.bmImage = bmImage;
        mWeakReference= new WeakReference<DownloadImages>(downloadImages);
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;

        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }
    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
        Log.e("TAG","IMAGEN CARGADA");
    }

    protected void onPreExecute(){
        mWeakReference.get().progressBar.setVisibility(View.VISIBLE);
    }
}

}

现在,当您启动 AsyncTask 时,从 Activity 传递“this”。

于 2012-11-02T10:30:50.527 回答