Code example for ImageView

Methods: getDrawablesetImageBitmapsetVisibility

0
			super(res, bitmap);
			this.taskRef = new WeakReference<ContentPhotosDownloader>(task);
		} 
 
		public static ContentPhotosDownloader getTask(ImageView imageView) {
			Drawable drawable = imageView.getDrawable();
			if (drawable instanceof AsyncDrawable)
				return ((AsyncDrawable) drawable).taskRef.get();
 
			return null; 
		} 
	} 
 
	//Change to a static inner class from a normal inner class, otherwise it may holds a strong reference to GalleryFlipItem, which is not a good practise for AsyncTask  
	private static class ContentPhotosDownloader extends AsyncTask<Void, Void, Bitmap> {
		private String url;
 
		private WeakReference<FlipViewController> controllerRef;
		//Use WeakReference 
		private WeakReference<ImageView> imgViewRef;
		private WeakReference<ProgressBar> progressBarRef;
		private int pageIndex;
		private boolean hideProgress;
 
		public ContentPhotosDownloader(String url, ImageView imgView, ProgressBar progressBar, FlipViewController controller, int pageIndex) {
			Assert.assertNotNull(url);
			Assert.assertNotNull(imgView);
			Assert.assertNotNull(controller);
			//progressBar can be null 
 
			this.url = url;
			this.imgViewRef = new WeakReference<ImageView>(imgView);
			this.controllerRef = new WeakReference<FlipViewController>(controller);
			this.pageIndex = pageIndex;
			if (progressBar != null)
				progressBarRef = new WeakReference<ProgressBar>(progressBar);
		} 
 
		public String getUrl() {
			return url;
		} 
 
		public int getPageIndex() { 
			return pageIndex;
		} 
 
		private ProgressBar getProgressBar() {
			return progressBarRef != null ? progressBarRef.get() : null;
		} 
 
		@Override 
		protected void onPreExecute() { 
			super.onPreExecute(); 
 
			ProgressBar bar = getProgressBar();
			if (bar != null) {
				bar.setVisibility(View.VISIBLE);
				ImageView iv = imgViewRef.get();
				if (iv != null)
					iv.setVisibility(GONE);
			} 
 
			//Commented out; pd and imgBackground should not be accessed directly  
			/* 
			pd.setVisibility(View.VISIBLE); 
			imgBackground.setVisibility(View.GONE); 
			*/ 
		} 
 
		@Override 
		protected Bitmap doInBackground(Void... params) {
			InputStream is = null;
			try { 
				URL aURL = new URL(url);
				URLConnection conn = aURL.openConnection();
 
				conn.connect();
 
				is = new BufferedInputStream(conn.getInputStream());
 
				return BitmapFactory.decodeStream(is);
			} catch (IOException e) {
				AphidLog.e(e, "Failed to load bitmap from url: " + url);
			} finally { 
				IO.close(is);
			} 
 
			return null; 
		} 
 
		@Override 
		protected void onPostExecute(Bitmap result) {
			if (isCancelled())
				return; 
			 
			ImageView imgView = imgViewRef.get();
 
			if (imgView != null && AsyncDrawable.getTask(imgView) == this) { //make sure the ImageView instance has not been reused for another page
				if (result != null) {
					imgView.setImageBitmap(result);
					imgView.setVisibility(View.VISIBLE);
				} 
 
				ProgressBar bar = getProgressBar();
				if (bar != null)
					bar.setVisibility(View.GONE);