网络获取图片及设置圆角

package com.kj.adapter;


import java.util.List;


import com.google.android.gms.internal.el;
import com.kj.entity.Topic;
import com.kj.huanxin.MessageAdapter.ViewHolder;
import com.kj.seeyou3.R;
import com.kj.util.CharArray;
import com.kj.util.Constants;
import com.kj.util.ImageHelper;
import com.kj.util.howTimeAgo;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
 * 
 * @file_name: MoodAdapter.java
 * @function:心情适配器
 * @author: moshangpiaoxue
 * @date: 2015-10-16
 * @time: 上午11:36:24
 */
public class MoodAdapter extends BaseAdapter {
List<Topic> topicLists;
Context mContext;
BitmapUtils bitmapUtils;
public MoodAdapter(List<Topic> topicLists, Context mContext) {
super();
this.topicLists = topicLists;
this.mContext = mContext;
bitmapUtils =new BitmapUtils(mContext);
}


@Override
public int getCount() {
if (topicLists==null) {
return 0;
}
return topicLists.size();
}


@Override
public Topic getItem(int position) {
return topicLists.get(position);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder =null;
if (convertView==null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_mood_itme, null);
ViewUtils.inject(holder, convertView);


convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}


Topic topic =topicLists.get(position);
holder.tv_Name.setText(topic.getNick());
// holder.tv_Say.setTextColor()
holder.tv_Say.setText(""+topic.getTxt());
holder.tv_dizhi.setText(topic.getCity());


String time = topic.getTime();
holder.tv_Time.setText(howTimeAgo.getStandardDate(time));
holder.tv_dianzan.setText(topic.getPraise());
holder.tv_shaoucang.setText(topic.getPlnum());
holder.tv_Tag.setText(CharArray.flag[Integer.parseInt(topic.getFlag())]);
if (topic.getSex().equals("0")) {
holder.iv_Sex.setImageResource(R.drawable.nv1);
}else if (topic.getSex().equals("1")){
holder.iv_Sex.setImageResource(R.drawable.nan1);
}
// bitmapUtils.display(holder.iv_TouXiang, Constants.PIC_URL+topic.getIcon());
// bitmapUtils.display(holder.iv_PIC, Constants.PIC_URL+topic.getSrc());
Picasso.with(mContext).load(Constants.PIC_URL+topic.getSrc()).into(holder.iv_PIC);
Picasso.with(mContext).load(Constants.PIC_URL+topic.getIcon()).transform(new CircleTransform()).into(holder.iv_TouXiang);
// new MyTask(holder).execute(Constants.PIC_URL+topic.getIcon());
return convertView;
}
public class CircleTransform implements Transformation {  
@Override  
public Bitmap transform(Bitmap source) {  
int size = Math.min(source.getWidth(), source.getHeight());  


int x = (source.getWidth() - size) / 2;  
int y = (source.getHeight() - size) / 2;  


Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);  
if (squaredBitmap != source) {  
source.recycle();  
}  


Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());  


Canvas canvas = new Canvas(bitmap);  
Paint paint = new Paint();  
BitmapShader shader = new BitmapShader(squaredBitmap,  
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);  
paint.setShader(shader);  
paint.setAntiAlias(true);  


float r = size / 2f;  
canvas.drawCircle(r, r, r, paint);  


squaredBitmap.recycle();  
return bitmap;  
}  


@Override  
public String key() {  
return "circle";  
}  
}  
// 以String类型的参数,Void表示没有进度信息,Bitmap表示异步任务返回一个位图
public class MyTask extends AsyncTask<String, Void, Bitmap> {
ViewHolder viewholder;
public MyTask(ViewHolder viewholder){
this.viewholder = viewholder;
}


// 表示任务执行之前的操作


//主要是完成耗时操作
@Override
protected Bitmap doInBackground(String... params) {
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(params[0]);
Bitmap bitmap=null;
try {
//从网络上下载图片
HttpResponse httpResponse =httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity httpEntity = httpResponse.getEntity();
byte[] data=EntityUtils.toByteArray(httpEntity);
bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}


//完成更新UI操作
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result!=null) {
//设置ImageView的显示图片
viewholder.iv_TouXiang.setImageBitmap(ImageHelper.toRoundBitmap(result));
}else {
viewholder.iv_TouXiang.setImageResource(R.drawable.head);
}




}


}
class ViewHolder{
@ViewInject(R.id.iv_TouXiang)ImageView iv_TouXiang;
@ViewInject(R.id.iv_PIC)ImageView iv_PIC;
@ViewInject(R.id.iv_Sex)ImageView iv_Sex;


@ViewInject(R.id.tv_Name)TextView tv_Name;
@ViewInject(R.id.tv_Tag)TextView tv_Tag;
@ViewInject(R.id.tv_Time)TextView tv_Time;
@ViewInject(R.id.tv_Say)TextView tv_Say;
@ViewInject(R.id.tv_dizhi)TextView tv_dizhi;
@ViewInject(R.id.tv_dianzan)TextView tv_dianzan;
@ViewInject(R.id.tv_shaoucang)TextView tv_shaoucang;


}
}

















































你可能感兴趣的:(网络获取图片及设置圆角)