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

时间:2017-05-03 10:03:16

标签: android android-glide progress

我正在尝试设置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;
      }
   }

4 个答案:

答案 0 :(得分: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 :(得分:1)

不需要inflaters或覆盖getView(),您只是不必要地减慢了应用程序的速度并使其复杂化。使用visiblity视图更简单。

  • 在您的布局中创建进度视图和ImageView,并使ImageView消失。
  • 制作一个AsyncTask下载图片并将其设置为ImageView并执行。
  • AsyncTask的{​​{1}}上,进度视图消失,onPostExecute可见。

答案 2 :(得分: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);中,您可以将下载的图像路径传递到您想要显示的位置。希望它有所帮助。

答案 3 :(得分: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 );
          }
      });
 }