如何在未下载图像时显示进度加载

问题描述 投票:1回答:4

我试图设置progressdialogprogressbar而未下载的图像,我已经使用位于drawable文件夹中的图像,我将此图像设置为默认图像而未下载图片。我怎么能像progress那样重新定义默认图像:

enter image description here

JAVA:

public class Art extends Fragment {
RecyclerView recyclerView;
GridView myGrid;
CategoryAdapter adapter;

public Art() {
    // Required empty public constructor
}

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup  container, Bundle savedInstanceState) {
    final ArrayList<String> movieList =  new ArrayList<>();
    movieList.add("https://myimage.com");
    movieList.add("https://myimage.com");
    View v =  inflater.inflate(R.layout.fragment_art, container, false);
    myGrid= (GridView) v.findViewById(R.id.gridViewCategory);
    adapter =new CategoryAdapter(getActivity(),movieList);
    myGrid.setAdapter(adapter);
    myGrid.setOnItemClickListener(new   android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent popup = new Intent(getActivity(), Pop.class);
                    popup.putExtra("WallpaperURL", movieList.get(position));
                    startActivity(popup);
        }
    });
    return v;
   }
}
class CategoryAdapter extends BaseAdapter {
   private Context context;
   private ArrayList<String> imageId=new ArrayList<String>();
    CategoryAdapter(Context context,ArrayList<String> imageId){
    this.context=context;
    this.imageId=imageId;
}
@Override
public int getCount() {
    return imageId.size();
}

@Override
public Object getItem(int position) {
    return imageId.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}
class ViewHolder{
    ImageView CategoryImage;
    ViewHolder(View view){
        CategoryImage = (ImageView) view.findViewById(R.id.cat_imageView);
    }
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row=convertView;
    ViewHolder holder=null;
    if(row == null){
        LayoutInflater inflater= (LayoutInflater)    context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.picture_item,parent,false);
        holder=new ViewHolder(row);
        row.setTag(holder);
    }
    else{
        holder=(ViewHolder) row.getTag();
    }
      Glide.with(context).load(imageId.get(position)).placeholder(R.drawable.arrw).into(holder.CategoryImage);
      return row;
      }
   }
android android-glide progress
4个回答
2
投票

我想你可能想要这样

view.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
//enable loading

Glide.with(context)
     .load(IMAGES.get(position))
     .error(R.drawable.matrade_logo2)
     .placeholder(R.drawable.placeholder)
     .listener(new RequestListener < String, GlideDrawable > () {
         @Override
         public boolean onException(Exception e, String model, Target < GlideDrawable > target, boolean isFirstResource) {
             //
             return false;
         }

         @Override
         public boolean onResourceReady(GlideDrawable resource, String model, Target < GlideDrawable > target, boolean isFromMemoryCache, boolean isFirstResource) {
             // hide loading if image finished downloading
             return false;
         }
     })
     .into(imageView);

1
投票

没有必要使用inflaters或覆盖getView(),你只是不必要地放慢速度并使你的应用程序复杂化。使用视图可见性更简单。

  • 在你的布局中创建进度视图和ImageView并使ImageView消失。
  • 制作一个下载图像的AsyncTask并将其设置为ImageView并执行它。
  • AsyncTaskonPostExecute上,让进度视图消失,ImageView可见。

1
投票

你做得不对。如果要下载图像,最好的方法是使用asyncTask类。尝试这样的东西,它肯定会帮助你。或使用像Glide,Picasso等图书馆。以下是如何在不使用某些图书馆的情况下完成的工作。

  private class DownloadFileFromURL extends AsyncTask<String, String, File> 
  {
    String downloadedFilePath = "";
    String fileName = "";

    /**
     * Before starting background thread
     * Show Progress Bar Dialog
     */

    DownloadFileFromURL(String fName) {
        fileName = fName;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        onCreateDialog(progress_bar_type);
    }

    /**
     * Downloading file in background thread
     */
    @SuppressWarnings("ResultOfMethodCallIgnored")
    @Override
    protected File doInBackground(String... f_url) {
        int count = 0;
        File convertedFile = null;
        try {
            URL url = new URL(f_url[0]);

            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);
            connection.connect();
            // getting file length
            // getting file length
            int lenghtOfFile = connection.getContentLength();
            String contentType = connection.getContentType();


            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            downloadedFilePath = getActivity().getFilesDir().getAbsolutePath() + "/" + fileName;
            OutputStream output = new FileOutputStream(downloadedFilePath);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        return null;
    }

    /**
     * Updating progress bar
     */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        progressDialogForDownload.setProgress(Integer.parseInt(progress[0]));
    }

    /**
     * After completing background task
     * Dismiss the progress dialog
     **/

    @Override
    protected void onPostExecute(File file_url) {
        // dismiss the dialog after the file was downloaded
        if (progressDialogForDownload != null && progressDialogForDownload.isShowing()) {
            progressDialogForDownload.dismiss();
            progressDialogForDownload = null;
        }

        openFileWithIntent(this.downloadedFilePath);
    }

}

在这种方法中,openFileWithIntent(this.downloadedFilePath);you可以传递下载的图像路径,您想要显示它。希望能帮助到你。


1
投票

尝试

showLoader(); //show loader or progress here
Glide
    .with( context ) // could be an issue!
    .load(url)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>() {  
           @Override
           public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation){
              //hide loader or progress here
              imageView1.setImageBitmap( bitmap );
          }
      });
 }
© www.soinside.com 2019 - 2024. All rights reserved.