Follow this approach.
First , create your own WebImageView class as follows.
public class WebImageView extends ImageView { private Drawable placeholder, image; public WebImageView(Context context) { super(context); } public WebImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WebImageView(Context context, AttributeSet attrs) { super(context, attrs); } public void setPlaceholderImage(Drawable drawable) { placeholder = drawable; if (image == null) { setImageDrawable(placeholder); } } public void setPlaceholderImage(int resid) { placeholder = getResources().getDrawable(resid); if (image == null) { setImageDrawable(placeholder); } } public void setImageUrl(String url) { DownloadTask task = new DownloadTask(); task.execute(url); } private class DownloadTask extends AsyncTask<String, Void, Bitmap> { @Override protected Bitmap doInBackground(String... params) { String url = params[0]; try { URLConnection conn = (new URL(url)).openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current=bis.read()) != -1) { baf.append((byte)current); } byte[] imageData = baf.toByteArray(); return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); } catch (Exception e) { return null; } } @Override protected void onPostExecute(Bitmap result) { image = new BitmapDrawable(result); if (image != null) { setImageDrawable(image); } } } }
Next , in Office, use the above custom ImageView as follows:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebImageView imageView = (WebImageView) findViewById(R.id.webimage); imageView.setPlaceholderImage(R.drawable.ic_launcher); imageView.setImageUrl("http://www.google.co.in/images/srpr/logo3w.png"); }
In short, you set up a placeholder image for ImageView, which is replaced with the actual image when the download is completed. Therefore, the GridView will display immediately without delay.
Implementation Details : Therefore, in your custom view (with image + text), instead of a simple ImageView, use WebImageView, as shown above. When you get a JSON response, install a TextView with the caption and a WebImageView with the image URL. Therefore, the title will be displayed immediately and the image will load lazily.
source share