【发布时间】:2018-05-23 11:02:48
【问题描述】:
@SuppressWarnings({ "rawtypes" })
public void addAttendance(ArrayList<Properties> attendanceusers) {
//tl.removeView(tr);
tl.removeAllViews();
//addHeaderAttendance();
ctr=0;
for (Iterator i = attendanceusers.iterator(); i.hasNext();) {
Properties p = (Properties) i.next();
property_list.add(p);
/** Create a TableRow dynamically **/
tr = new TableRow(this);
picurl=p.getPic();
profile = new ImageView(this);
profile.setPadding(20,50,20,50);
/* URL newurl = new URL(picurl);
bmp = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
profile.setImageBitmap(bmp);*/
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(picurl).getContent());
profile.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
profile.setOnClickListener(this);
//myButton.setPadding(5, 5, 5, 5);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
Ll.setPadding(0, 0, 20, 0);
Ll.addView(profile,params);
tr.addView((View)Ll);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
我用于填充图像的代码不起作用,因为我的主要方法正在线程上工作。你能建议一个我可以放在 a 方法上的代码吗?这篇文章不是堆栈上任何其他帖子的重复。 我在我的另一个正在工作的类上尝试了这段代码,但是如果主方法(我使用的是一个线程)它会闪烁并闪烁:
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
InputStream in = new URL(picurl).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
// log error
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (bmp != null)
profile.setImageBitmap(bmp);
}
}.execute();
但是,如果我把它放在我粘贴的第一个代码中。 ui 会不断闪烁,因为它是线程内部的线程。我的图像太大,我无法正确调整它的大小,你能帮我吗?我会承诺给那些可以帮助我的人打勾加分,对于那些努力的人我也会给分
【问题讨论】:
-
你可以使用 glide 库来做这个!它更容易!和android文档推荐!