转自:http://blog.csdn.net/lfdfhl/article/details/12223345
稍微做了一点儿修改
/**
* 利用NetworkImageView显示网络图片
*/
private void showImageByNetworkImageView(){
String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
RequestQueue requestQueue = Volley.newRequestQueueInDisk(ctx, "/sdcard/demo/", null);
ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache()); mNetworkImageView.setTag("url");
mNetworkImageView.setImageUrl(imageUrl,imageLoader);
}
volley源文件:volley.zip
volley的jar文件:Volley.jar.zip
使用例子:
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley; public class MainActivity extends Activity { private Context ctx;
private RequestQueue mQueue;
private ImageLoader mImageLoader; private ImageView mImageView;
private NetworkImageView mNetworkImageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); this.ctx = this;
initView(); showImageByNetworkImageView();
} private void initView(){
mImageView=(ImageView) findViewById(R.id.imageView);
mNetworkImageView=(NetworkImageView)findViewById(R.id.networkImageView);
} /**
* 利用Volley获取JSON数据
*/
private void getJSONByVolley() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String JSONDataUrl = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";
final ProgressDialog progressDialog = ProgressDialog.show(this, "This is title", "...Loading..."); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
JSONDataUrl,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("response="+response);
if (progressDialog.isShowing()&&progressDialog!=null) {
progressDialog.dismiss();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
System.out.println("sorry,Error");
}
});
requestQueue.add(jsonObjectRequest);
} private void loadImageByVolley(){
String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
RequestQueue requestQueue = Volley.newRequestQueue(this); ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());
ImageListener listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher,R.drawable.ic_launcher);
imageLoader.get(imageUrl, listener);
} /**
* 利用NetworkImageView显示网络图片
*/
private void showImageByNetworkImageView(){
String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
RequestQueue requestQueue = Volley.newRequestQueueInDisk(ctx, "/sdcard/demo/", null);
ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache()); mNetworkImageView.setTag("url");
mNetworkImageView.setImageUrl(imageUrl,imageLoader);
} }
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.toolbox.ImageLoader.ImageCache; public class BitmapCache implements ImageCache {
private LruCache<String, Bitmap> mCache; public BitmapCache() {
int maxSize = * * ;
mCache = new LruCache<String, Bitmap>(maxSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
} @Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
} @Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="120dip"
android:layout_height="120dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
/> <ImageView
android:id="@+id/imageView"
android:layout_width="120dip"
android:layout_height="120dip"
android:layout_centerInParent="true"
/> </RelativeLayout>