Skip to content

Instantly share code, notes, and snippets.

@JackCho
Created May 27, 2013 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackCho/5655309 to your computer and use it in GitHub Desktop.
Save JackCho/5655309 to your computer and use it in GitHub Desktop.
package com.sh.yeshine.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AsycnImageView extends ImageView {
private AsycnImageViewTask aivt;
private int bitmap_W;
private int bitmap_H;
private int MAX_W;
private int MAX_H;
private int MIN_W;
private int MIN_H;
private Context c;
public AsycnImageView(Context context) {
super(context);
this.c = context;
}
public AsycnImageView(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
this.c = arg0;
}
public AsycnImageView(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
this.c = arg0;
}
@Override
public void setImageBitmap(Bitmap bm) {
cancel();
super.setImageBitmap(bm);
/** 获取图片宽高 **/
bitmap_W = bm.getWidth();
bitmap_H = bm.getHeight();
MAX_W = bitmap_W * 3;
MAX_H = bitmap_H * 3;
MIN_W = bitmap_W / 2;
MIN_H = bitmap_H / 2;
}
@Override
public void setImageDrawable(Drawable drawable) {
cancel();
super.setImageDrawable(drawable);
}
@Override
public void setImageURI(Uri uri) {
cancel();
super.setImageURI(uri);
}
@Override
public void setImageResource(int resId) {
cancel();
super.setImageResource(resId);
}
@Override
public void setBackgroundDrawable(Drawable d) {
cancel();
super.setBackgroundDrawable(d);
}
@Override
public void setBackgroundResource(int resid) {
cancel();
super.setBackgroundResource(resid);
}
@Override
public void setBackgroundColor(int color) {
cancel();
super.setBackgroundColor(color);
}
public void setAsycnImageView(String url) {
try {
cancel();
aivt = new AsycnImageViewTask(c, this, url);
aivt.execute();
} catch (Exception e) {
} catch (Error e) {
}
}
public void setAsycnImageView(String url, int type) {
try {
cancel();
aivt = new AsycnImageViewTask(c, this, url, type);
aivt.execute();
} catch (Exception e) {
} catch (Error e) {
}
}
public void setMyMap(String url, BitmapCallBack callBack) {
try {
cancel();
aivt = new AsycnImageViewTask(c, callBack, url);
aivt.execute();
} catch (Exception e) {
} catch (Error e) {
}
}
private void cancel() {
if (aivt != null) {
aivt.cancel(true);
aivt = null;
}
}
public interface BitmapCallBack {
public void asycnIVset(Bitmap b);
}
}
package com.sh.yeshine.widget;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.Map;
import com.sh.yeshine.activity.R;
import com.sh.yeshine.temp.Temp;
import com.sh.yeshine.util.Download;
import com.sh.yeshine.widget.AsycnImageView.BitmapCallBack;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
public class AsycnImageViewTask extends AsyncTask<Object, Void, String> {
private AsycnImageView iv;
private String url;
private Bitmap bitmap;
private BitmapFactory.Options sDefaultOptions;
private int type = -1;
private DisplayMetrics dm;
private LoadingDialog dialog;
private Context c;
private BitmapCallBack call;
// 图阅详情使用
public AsycnImageViewTask(Context c, BitmapCallBack call, String url) {
this.call = call;
this.url = url;
WindowManager wm = (WindowManager) c
.getSystemService(Context.WINDOW_SERVICE);
dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
sDefaultOptions = new BitmapFactory.Options();
sDefaultOptions.inJustDecodeBounds = false;
sDefaultOptions.inSampleSize = 1;
}
public AsycnImageViewTask(Context c, AsycnImageView iv, String url, int type) {
super();
this.type = type;
this.iv = iv;
this.url = url;
WindowManager wm = (WindowManager) c
.getSystemService(Context.WINDOW_SERVICE);
dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
sDefaultOptions = new BitmapFactory.Options();
sDefaultOptions.inJustDecodeBounds = false;
sDefaultOptions.inSampleSize = 3;
}
public AsycnImageViewTask(Context c, AsycnImageView iv, String url) {
super();
this.c = c;
this.iv = iv;
this.url = url;
sDefaultOptions = new BitmapFactory.Options();
// sDefaultOptions.inJustDecodeBounds = true;
// sDefaultOptions.inDither = true;
// sDefaultOptions.inScaled = true;
// sDefaultOptions.inSampleSize = 5;
sDefaultOptions.inJustDecodeBounds = false;
sDefaultOptions.inSampleSize = 3;
}
@Override
protected String doInBackground(Object... params) {
if (Temp.imageCache.containsKey(url)) {
SoftReference<Bitmap> sr = Temp.imageCache.get(url);
if (sr!= null) {
bitmap = sr.get();
} else {
Temp.imageCache.remove(url);
bitmap = loadImageFromUrl(url);
}
} else {
bitmap = loadImageFromUrl(url);
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
if (bitmap != null) {
if (call != null) {
call.asycnIVset(bitmap);
} else{
iv.setImageBitmap(bitmap);
}
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = LoadingDialog.getInstance(c, R.style.dialog);
dialog.show();
}
public Bitmap loadImageFromUrl(String url) {
Bitmap b = Download.readBitmap(url,sDefaultOptions);
if (b == null) {
try {
// if (type != -1) {
// sDefaultOptions = new BitmapFactory.Options();
// sDefaultOptions.inJustDecodeBounds = false;
// sDefaultOptions.inDither = true;
// sDefaultOptions.inScaled = true;
// sDefaultOptions.inTargetDensity = 1;
// sDefaultOptions.outWidth = dm.widthPixels;
// sDefaultOptions.outHeight = bitmap.getHeight()
// * dm.widthPixels / bitmap.getWidth();
// }
b = BitmapFactory.decodeStream(new URL(url).openStream(), null,
sDefaultOptions);
Log.e("test", "b为空");
} catch (Exception e) {
e.printStackTrace();
}
}
Temp.imageCache.put(url, new SoftReference<Bitmap>(b));
return b;
}
}
package com.sh.yeshine.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.sh.yeshine.temp.Temp;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
/**
* 下载
*
* @author Administrator
*
*/
public class Download {
/**
*
* @param imageurl
* 下载链接
* @param sDefaultOptions
* @param name
* 图片名称
* @return
*/
public static Bitmap downloadBitmap(String imageurl, Options sDefaultOptions) {
File f = new File(Temp.rootFile, "/picture/");
if (!f.exists()) {
f.mkdirs();
}
String[] split = imageurl.split("/");
Bitmap bitmap = null;
try {
HttpGet hg = new HttpGet(imageurl);
HttpClient hc = new DefaultHttpClient();
HttpResponse hr = hc.execute(hg);
HttpEntity he = hr.getEntity();
InputStream is = he.getContent();
sDefaultOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bitmap = BitmapFactory.decodeStream(is,null,sDefaultOptions);
File fl = new File(f, split[split.length - 1]);
FileOutputStream fos = new FileOutputStream(fl);
bitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);
fos.flush();
fos.close();
is.close();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
public static File downloadDetail(String imageurl) {
File f = new File(Temp.rootFile, "/picture/");
if (!f.exists()) {
f.mkdirs();
}
File fl = null;
try {
String[] split = imageurl.split("/");
fl = new File(f, split[split.length - 1]);
HttpGet hg = new HttpGet(imageurl);
HttpClient hc = new DefaultHttpClient();
InputStream is = hc.execute(hg).getEntity().getContent();
FileOutputStream fos = new FileOutputStream(fl);
byte[] b = new byte[1024];
int read = is.read(b);
while (read != -1) {
fos.write(b, 0, read);
read = is.read(b);
}
fos.flush();
fos.close();
is.close();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fl;
}
/**
*
* @param sDefaultOptions
* @param name
* 图片名称
* @return
*/
public static Bitmap readBitmap(String imageurl, Options sDefaultOptions) {
File f = new File(Temp.rootFile, "/picture/");
if (!f.exists()) {
f.mkdirs();
}
String[] split = imageurl.split("/");
Bitmap bitmap = null;
File f1 = new File(f, split[split.length - 1]);
if (!f1.exists()) {
HttpURLConnection con = null;
try {
URL u = new URL(imageurl);
con = (HttpURLConnection) u.openConnection();
con.connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (f1.length() != con.getContentLength()) {
return downloadBitmap(imageurl,sDefaultOptions);
} else {
return bitmap;
}
}
try {
FileInputStream fis = new FileInputStream(f1);
sDefaultOptions.inPreferredConfig = Bitmap.Config.RGB_565;
// sDefaultOptions.inPurgeable = true;
// sDefaultOptions.inInputShareable = true;
sDefaultOptions.inSampleSize = 1;
bitmap = BitmapFactory.decodeStream(fis,null,sDefaultOptions);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment