gpt4 book ai didi

java - 在 Android ListView 中,加载圆形图像和文本的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-01 12:38:56 25 4
gpt4 key购买 nike

我正在尝试创建一个ListView,其中包含一个简单的圆形图像和textView,为了实现圆形图像,我创建了一个函数来使其圆形,因为我从json对象获取图像url作为我使用的httpurl另一个从中获取位图的函数..但看起来有些严重错误,因为 ListView 加载时间太长,甚至在我滚动时卡住。

公共(public)类 MyImageListAdapter 扩展了 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;

}

}

最佳答案

您可以使用Picasso用于使用单行代码直接从 URL 加载图像的库。

要使图像圆角可以引用我的答案 How to create round corner image using volley library android

还有一个对你的网络的建议,使用 Volley 库。这是为了更快更好的网络。

关于java - 在 Android ListView 中,加载圆形图像和文本的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25306852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com