在this线程之后,我尝试使用以下代码创建一个变量状态栏:
private int[] loadingElementIDs;
private void initLoadingBar() {
final DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
final float screenWidthInDp = displayMetrics.widthPixels/displayMetrics.density;
final int elementAmount = (int) (Math.floor(screenWidthInDp * 0.5f / 30) * 5);
//set margins
LinearLayout container = (LinearLayout)findViewById(R.id.loading_outer);
...
container.requestLayout();
//declare length
loadingElementIDs = new int[elementAmount];
LayoutParams LLParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
LLParams.weight = 1f;
LinearLayout element;
for (int i=0; i<elementAmount; i++) {
int id = generateViewId(); //creates unique id
element = new LinearLayout(this);
element.setVisibility(View.INVISIBLE);
element.setLayoutParams(LLParams);
element.setBackgroundDrawable(getResources().getDrawable(R.drawable.loading_inner));
element.setId(id);
element.requestLayout();
container.addView(element);
loadingElementIDs[i] = id;
}
}
这对我来说很好,但是现在我想用asynctask来计算它并使元素可见(我的activity类中的代码):
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@SuppressWarnings("static-access")
@Override
protected Void doInBackground(Void... arg0) {
try {
int step = 0;
float totalSteps = 100f;
while (...) {
step++;
// ...................
//show status
setLoadingStatus( step / totalSteps);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}
public void setLoadingStatus(float percentage) {
int max = (int) Math.min( Math.floor(percentage * loadingElementIDs.length),
for (int d=0; d<max; d++ ) {
((LinearLayout)findViewById(loadingElementIDs[d])).setVisibility(View.VISIBLE);
LinearLayout el = (LinearLayout)this.findViewById(loadingElementIDs[d]);
el.setVisibility(LinearLayout.VISIBLE);
}
}
这不起作用。如果我从onCreate调用setLoadingStatus(20f);
它可以正常工作,但不能在AsyncTask中工作。当然,我会在活动onCreate()中启动initLoadingBar();
和new PrefetchData().execute();
。
答案 0 :(得分:1)
我不知道这是如何合并的,评论全部被标记,但请求的代码片段在下面用于使用处理程序进行管理:
在您的活动中定义处理程序:
Handler handler = new Handler(){
handleMessage(Message msg)
{
if (msg.what == STATUS)
{
//do something if it's a message form your AsyncTask
}
else
//other messages..
}
};
创建AsyncTask时,请将其作为处理程序。定义一个构造函数来接受它并保持对它的本地引用。
new PrefetchData(handler).execute(...);
然后在你的AsyncTask中:( STATUS将是一个常量设置作为消息代码..)
while (...) {
step++;
// ...................
//show status
handler.obtainMessage(STATUS, step / totalSteps).sendToTarget();
}
答案 1 :(得分:1)
使用UI线程更新UI组件。如果需要更新任务进度,可以使用publishProgress(xxx)和onProgressUpdate(xxx)。有关更多日期:http://developer.android.com/reference/android/os/AsyncTask.html
答案 2 :(得分:0)
您可以runOnUiThread
方法从任何thread
runOnUiThread(new Runnable() {
public void run() {
// some code #3 (Write your code here to run in UI thread)
}
}); // enter code here
答案 3 :(得分:0)
onProgressUpdate
解决了这个问题。而不是setLoadingStatus
我称之为:
private class PrefetchData extends AsyncTask<Void, Float, Void> {
....
protected void onProgressUpdate(Float... values) {
for (int d=0; d<Math.min( Math.floor(values[0] * loadingElementIDs.length), loadingElementIDs.length); d++ ) {
LinearLayout el = (LinearLayout)findViewById(loadingElementIDs[d]);
el.setVisibility(LinearLayout.VISIBLE);
}
}
}