Skip to content

Instantly share code, notes, and snippets.

@kimuchi1203
Created March 16, 2013 05:03
Show Gist options
  • Select an option

  • Save kimuchi1203/5175071 to your computer and use it in GitHub Desktop.

Select an option

Save kimuchi1203/5175071 to your computer and use it in GitHub Desktop.
public class UrlImageView extends ImageView {
private static final String LOG_TAG = "View";
private Map<String, Bitmap> bitmapStore;
private String url;
public UrlImageView(Context context) {
super(context);
bitmapStore = new HashMap<String, Bitmap>();
}
public UrlImageView(Context context, AttributeSet attrs) {
super(context, attrs);
bitmapStore = new HashMap<String, Bitmap>();
}
public void setUrl(String url2) {
this.url = url2;
}
public void setImage() {
setImage(this.url);
}
public void setImage(final String url) {
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
if(MainActivity.LOG_ON) Log.d(LOG_TAG, "setImage "+url);
final Bitmap bmp = getBitmap(url);
if(null!=bmp){
handler.post(new Runnable(){
@Override
public void run() {
if(MainActivity.LOG_ON) Log.d(LOG_TAG, "setImageBitmap "+url);
UrlImageView.this.setImageBitmap(bmp);
}
});
storeBitmap(url, bmp);
}
}
}).start();
return;
}
private Bitmap getBitmap(String url) {
Bitmap bmp = bitmapStore.get(url);
if(null==bmp) {
byte[] data = getByteArrayFromUrl(url);
if(null!=data) {
bmp = scaledDecode(data, 0, data.length);
}
}
return bmp;
}
private void storeBitmap(String url, Bitmap bmp) {
if(null==bitmapStore.get(url)) {
bitmapStore.put(url, bmp);
}
}
private byte[] getByteArrayFromUrl(String strUrl) {
byte[] byteArray = new byte[1024];
byte[] ret = null;
int size;
HttpURLConnection con = null;
InputStream in = null;
ByteArrayOutputStream out = null;
try {
URL url = new URL(strUrl);
con = (HttpURLConnection) url.openConnection();
con.setUseCaches(true);
con.setRequestMethod("GET");
con.connect();
in = con.getInputStream();
out = new ByteArrayOutputStream();
while((size=in.read(byteArray))!=-1) {
out.write(byteArray, 0, size);
}
ret = out.toByteArray();
} catch (Exception e){
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
} finally {
try {
if (con != null)
con.disconnect();
if (in != null)
in.close();
if (out != null)
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return ret;
}
private Bitmap scaledDecode(byte[] data, int offset, int length) {
int viewWidth, viewHeight;
viewWidth = UrlImageView.this.getWidth();
viewHeight = UrlImageView.this.getHeight();
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, offset, length, opt);
int scaleW = opt.outWidth/viewWidth;
int scaleH = opt.outHeight/viewHeight;
int sampleSize = Math.max(scaleW, scaleH);
opt.inSampleSize = sampleSize;
opt.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeByteArray(data, offset, data.length, opt);
int tmpWidth = bmp.getWidth();
int tmpHeight = bmp.getHeight();
float scale = Math.min((float)viewWidth/tmpWidth, (float)viewHeight/tmpHeight);
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
bmp = Bitmap.createBitmap(bmp, 0, 0, tmpWidth, tmpHeight, matrix, true);
return bmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment