首先到github网站下载SmartImageView,拷贝到工程
使用方式
public class SmartImageViewActivity extends BaseActivity {
private EditText mUrl;
private SmartImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_image);
mUrl = (EditText) findViewById(R.id.editText1);
mImageView = (SmartImageView) findViewById(R.id.smartimage);
}
public void loadImage(View view){
String url = mUrl.getText().toString().trim();
mImageView.setImageUrl(url, R.drawable.ic_launcher, R.drawable.ic_launcher);
}
}
SmartImageView源码
public class SmartImageView extends ImageView {
private static final int LOADING_THREADS = 4;
private static ExecutorService threadPool = Executors.newFixedThreadPool(LOADING_THREADS);
private SmartImageTask currentTask;
public SmartImageView(Context context) {
super(context);
}
public SmartImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SmartImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
// Helpers to set image by URL
public void setImageUrl(String url) {
setImage(new WebImage(url));
}
public void setImageUrl(String url, SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url), completeListener);
}
public void setImageUrl(String url, final Integer fallbackResource) {
setImage(new WebImage(url), fallbackResource);
}
public void setImageUrl(String url, final Integer fallbackResource, SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url), fallbackResource, completeListener);
}
public void setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource) {
setImage(new WebImage(url), fallbackResource, loadingResource);
}
public void setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource, SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url), fallbackResource, loadingResource, completeListener);
}
// Helpers to set image by contact address book id
public void setImageContact(long contactId) {
setImage(new ContactImage(contactId));
}
public void setImageContact(long contactId, final Integer fallbackResource) {
setImage(new ContactImage(contactId), fallbackResource);
}
public void setImageContact(long contactId, final Integer fallbackResource, final Integer loadingResource) {
setImage(new ContactImage(contactId), fallbackResource, fallbackResource);
}
// Set image using SmartImage object
public void setImage(final SmartImage image) {
setImage(image, null, null, null);
}
public void setImage(final SmartImage image, final SmartImageTask.OnCompleteListener completeListener) {
setImage(image, null, null, completeListener);
}
public void setImage(final SmartImage image, final Integer fallbackResource) {
setImage(image, fallbackResource, fallbackResource, null);
}
public void setImage(final SmartImage image, final Integer fallbackResource, SmartImageTask.OnCompleteListener completeListener) {
setImage(image, fallbackResource, fallbackResource, completeListener);
}
public void setImage(final SmartImage image, final Integer fallbackResource, final Integer loadingResource) {
setImage(image, fallbackResource, loadingResource, null);
}
public void setImage(final SmartImage image, final Integer fallbackResource, final Integer loadingResource, final SmartImageTask.OnCompleteListener completeListener) {
// Set a loading resource
if(loadingResource != null){
setImageResource(loadingResource);
}
// Cancel any existing tasks for this image view
if(currentTask != null) {
currentTask.cancel();
currentTask = null;
}
// Set up the new task
currentTask = new SmartImageTask(getContext(), image);
currentTask.setOnCompleteHandler(new SmartImageTask.OnCompleteHandler() {
@Override
public void onComplete(Bitmap bitmap) {
if(bitmap != null) {
setImageBitmap(bitmap);
} else {
// Set fallback resource
if(fallbackResource != null) {
setImageResource(fallbackResource);
}
}
if(completeListener != null){
completeListener.onComplete(bitmap);
}
}
});
// Run the task in a threadpool
threadPool.execute(currentTask);
}
public static void cancelAllTasks() {
threadPool.shutdownNow();
threadPool = Executors.newFixedThreadPool(LOADING_THREADS);
}
}