当前位置: 动力学知识库 > 问答 > 编程问答 >

java - In Android ListView what is this the proper way to load rounded images along with text?

问题描述:

I am trying to create a ListView which contains a simple rounded image and textView along with it ,to achive rounded image i have created a function to make it round and since i get the image url from json object as httpurl i have used another function to get bitmap from it.. but looks something is seriously wrong because the ListView is taking too long to load and its even freezing when i scroll.

public class MyImageListAdapter extends ArrayAdapter {

private final Context context;

private final ArrayList<UserInfo> values;

String mother_name;

String mother_image;

public MyImageListAdapter(Activity atvt, Context context,

ArrayList<UserInfo> values) {

super(context, R.layout.my_list, values);

this.context = context;

this.values = values;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.image_name_fragment, parent,

false);

TextView textView = (TextView) rowView.findViewById(R.id.firstLine);

ImageView imageView = (ImageView) rowView

.findViewById(R.id.profileImage);

textView.setText(((UserInfo) values.get(position)).getUserName());

String motherImageURl = ((UserInfo) values.get(position))

.getPrifileImageUr();

mother_name = ((UserInfo) values.get(position)).getUserName();

mother_image = motherImageURl;

Bitmap bm = getBitmapFromHttpURL(motherImageURl);

Bitmap bm1 = getRoundedCornerBitmapWithBorderSmall(bm, 2);

imageView.setImageBitmap(bm1);

rowView.setPadding(5, 10, 5, 10);

return rowView;

}

public static Bitmap getBitmapFromHttpURL(String src) {

Bitmap bitmap = null;

AsyncTask<String, Void, Bitmap> ac = new AsyncTask<String, Void, Bitmap>() {

@Override

protected Bitmap doInBackground(String... params) {

try {

URL url = new URL(params[0]);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

connection.setDoInput(true);

connection.connect();

InputStream input = connection.getInputStream();

Bitmap myBitmap = BitmapFactory.decodeStream(input);

myBitmap = Bitmap.createScaledBitmap(myBitmap, 200, 200,

true);

return myBitmap;

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

};

try {

bitmap = ac.execute(src).get();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ExecutionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// bitmap = Bitmap.createScaledBitmap(bitmap,200,200, true);

return bitmap;

}

public static Bitmap getRoundedCornerBitmapWithBorderSmall(Bitmap bitmap,

int border) {

int w = bitmap.getWidth();

int h = bitmap.getHeight();

// if(w>50 || h>50){w=50;h=50;}

int a, b;

System.out.println("-------" + bitmap.getByteCount() + "--" + h + "---"

+ w);

int radius = Math.min(h / 2, w / 2);

a = Math.max(w, h);

Bitmap output = Bitmap.createBitmap(a, a, Config.ARGB_8888);

Paint p = new Paint();

p.setAntiAlias(true);

Canvas c = new Canvas(output);

c.drawARGB(0, 0, 0, 0);

p.setStyle(Style.FILL);

c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);

p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

c.drawBitmap(bitmap, 4, 4, p);

p.setXfermode(null);

p.setStyle(Style.STROKE);

p.setColor(Color.parseColor("#61445C"));

p.setStrokeWidth(border);

c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);

// output = Bitmap.createScaledBitmap(output, 200, 200, true);

// output=getResizedBitmap(output, 200,200) ;

// Bitmap yourBitmap;

output = Bitmap.createScaledBitmap(output, 100, 100, true);

return output;

}

}

网友答案:

You can use Picasso library for loading images directly from URL with single line of code.

For making image rounded corner you can refer my answer How to create round corner image using volley library android

And also one suggestion for your networking, use Volley library for it . It is for faster and better networking.

分享给朋友:
您可能感兴趣的文章:
随机阅读: