进度条小于下载的图像不可见

时间:2012-11-02 10:20:58

标签: android progress-bar static-class

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

我的课程DownloadImage:

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,除非它被声明为静态,如果我认为静态我无法创建它我的活动。

任何解决方案?

谢谢,抱歉我的英文不好

2 个答案:

答案 0 :(得分:0)

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

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

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

并更改构造函数,以便将ProgressBar作为参数。

答案 1 :(得分:0)

使用WeakReference确实获取调用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'。