AndroidTouchGalleryLibrary 优化
AndroidTouchGalleryLibrary 是一个非常好用的库,
但是使用的时候,需要小心处理,容易引发OutOfMemoryError,同时使用UrlTouchImageView的时候,
从网络下载是没有缓存的,因此每次都是新加载图片,同时使用在线家在较大图片容易导致程序挂掉
因此将原先的UrlTouchImageView类中的此段代码更换
原代码:
//No caching load public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap> { @Override protected Bitmap doInBackground(String... strings) { String url = strings[0]; Bitmap bm = null; try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); int totalLen = conn.getContentLength(); InputStreamWrapper bis = new InputStreamWrapper(is, 8192, totalLen); bis.setProgressListener(new InputStreamProgressListener() { @Override public void onProgress(float progressValue, long bytesLoaded, long bytesTotal) { publishProgress((int)(progressValue * 100)); } }); bm = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } return bm; } @Override protected void onPostExecute(Bitmap bitmap) { if (bitmap == null) { mImageView.setScaleType(ScaleType.CENTER); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo); mImageView.setImageBitmap(bitmap); } else { mImageView.setScaleType(ScaleType.MATRIX); mImageView.setImageBitmap(bitmap); } mImageView.setVisibility(VISIBLE); mProgressBar.setVisibility(GONE); } @Override protected void onProgressUpdate(Integer... values) { mProgressBar.setProgress(values[0]); } }
更换为:
//No caching load public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap> { @Override protected Bitmap doInBackground(String... strings) { String url = strings[0]; Bitmap bm = null; try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); int totalLen = conn.getContentLength(); InputStreamWrapper bis = new InputStreamWrapper(is, 8192, totalLen); bis.setProgressListener(new InputStreamProgressListener() { @Override public void onProgress(float progressValue, long bytesLoaded, long bytesTotal) { publishProgress((int)(progressValue * 100)); } }); BitmapFactory.Options options=new BitmapFactory.Options(); options.inTempStorage = new byte[100*1024]; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inSampleSize = 2;//压缩 options.inInputShareable = true; bm = BitmapFactory.decodeStream(bis, null, options); bis.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } return bm; } @Override protected void onPostExecute(Bitmap bitmap) { if (bitmap == null) { mImageView.setScaleType(ScaleType.CENTER); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo); mImageView.setImageBitmap(bitmap); } else { mImageView.setScaleType(ScaleType.MATRIX); mImageView.setImageBitmap(bitmap); } mImageView.setVisibility(VISIBLE); mProgressBar.setVisibility(GONE); } @Override protected void onProgressUpdate(Integer... values) { mProgressBar.setProgress(values[0]); } }