@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);
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();
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);
ctr++;
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
上面的代码是动态设计。那里有一个图像,我把图像放在我的Android屏幕上,我从我的数据库使用Web服务。问题是图像闪烁,而且太大了。我认为它是闪烁的,因为我在一个线程中创建一个线程,但我仍然对如何解决它感到困惑。我的异步任务是如何获得图像。
public ArrayList<Properties> attendanceUse(String result) {
ArrayList<Properties> attendanceusers = new ArrayList<Properties>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Properties user = new Properties();
user.setPic(json_data.getString("student_ImgUrl"));
attendanceusers.add(user);
//attendanceusers.get(index)
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return attendanceusers;
}
上面的代码是我如何通过我的htdocs中的php查询得到我的图像。 student_imgurl是我从mysql数据库中选择的列以获取url。
private void update()
{
final Handler handler = new Handler();
Timer timer = new Timer();
String splithis;
splithis=mySpinner.getSelectedItem().toString();
splited = splithis.split(" ");
course = splited[0];
room = splited[1];
sections = splited[2];
TimerTask task = new TimerTask() {
@Override
public void run() {
data = bw.getAttendanceFromDB(term, course,sections,day,room);
handler.post(new Runnable() {
public void run() {
try {
//asyntask class ito
ArrayList<Properties> attendanceusers = attendanceUse(data);
addAttendance(attendanceusers);
} catch (Exception e) {
// error, do something
}
}//run
});//handler mo
}//runmo
};//timer
timer.schedule(task, 0,1000); //timer
}
这是我的更新无效。它在计时器上,以便图像实时更新。
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
data ="";
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
update();
}
});
}
}).start();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
return;
}
});
上面的代码是我在Oncreate中的代码。我有一个微调器/下拉菜单,他们将选择课程室部分,然后当他们选择一个项目时,将显示该课程 - 房间部分的学生的图像。我想要的是停止我的UI闪烁和调整imageview的大小。
我的想法是,通过用void update()
类替换你的AsyncTask<Void, Properties, Void>
方法来从数据库中提取参与者数据并同时在其doInBackground()
方法中下载配置文件图像可以做得更好。每个与会者加载后,您可以通过publishProgress(Properties)
和onProgressUpdate(Properties[])
方法更新您的UI。这应该会导致更新的UI更新。
请记住,如果您同时运行多个AsyncTasks,则应在AsyncTask.THREAD_POOL_EXECUTOR
上执行它们以允许它们并行运行(默认执行程序为AsyncTask.SERIAL_EXECUTOR
)
至于图像尺寸问题,可能是您没有在视图上设置正确的ImageView.ScaleType
以使其正确适合。我希望它应该是ImageView.ScaleType.CENTER_INSIDE
。