圆角头像
1:activity:
public class MainActivity extends Activity {
private ImageView mImageView;
private String url = "http://www.jcpeixun.com/app_client_api/userinfo.aspx?uid=450894";
private String imgUrl;
private RequestQueue mRequestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRequestQueue = Volley.newRequestQueue(this);
init(url);
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x001) {
try {
Log.e("msg.obj ", msg.obj + "");
JSONObject object = (JSONObject) msg.obj;
imgUrl = object.getString("img");
Log.e("imgUrl ", imgUrl);
getImage(imgUrl);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
public void volleyImg(String imgUrl) {
ImageRequest imgRequest = new ImageRequest(imgUrl,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap arg0) {
mImageView.setImageBitmap(arg0);
}
}, 300, 200, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
}
});
mRequestQueue.add(imgRequest);
}
public void getImage(String imgUrl){
final LruCache<String, Bitmap> mImageCache = new LruCache<String, Bitmap>(
20);
ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
@Override
public void putBitmap(String key, Bitmap value) {
mImageCache.put(key, value);
}
@Override
public Bitmap getBitmap(String key) {
return mImageCache.get(key);
}
};
ImageLoader mImageLoader = new ImageLoader(mRequestQueue, imageCache);
ImageLoader.ImageListener listener = ImageLoader
.getImageListener(mImageView, android.R.drawable.ic_menu_rotate,
android.R.drawable.ic_delete);
mImageLoader.get(imgUrl, listener);
}
public void init(String url) {
mImageView = (ImageView) findViewById(R.id.iv);
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject object) {
Message message = mHandler.obtainMessage(0x001);
message.obj = object;
mHandler.sendMessage(message);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
mRequestQueue.add(jr);
}
@Override
protected void onPause() {
super.onPause();
mRequestQueue.cancelAll(this);
}
}
2:绑定RoundedImageView的Library:
3:activity_main布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<test.com.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
app:riv_border_color="#888888"
app:riv_border_width="2dip"
app:riv_corner_radius="40dip" />
</LinearLayout>
4:效果图:
![这里写图片描述](https://img-blog.csdn.net/20151126233008172)