转载注明出处,Lee http://blog.csdn.net/hnulwt/article/details/44462177
今天看到讨论说关于进度条走的慢的问题。实际问题是这样的:后台执行的程序速度会很快,但是在界面上感觉得到进度条是走的比较慢的。为此,做了一个小实验,代码上实验在1秒的时间 通过俩种方式增加进度条至100%(每隔10ms增加1%和 每隔250ms增加25%),验证哪种方式给人的感觉快,但是在demo做出来之后,确发现了新的问题,想在此与大家共同讨论。
问题
根据代码,我预想的结果应该是俩个进度条增加的速度相同,但是经过我的测试,点击 同时增加 按钮,跳跃增加那个进度条总是会比逐步增加的进度条要快。
界面如下(源码及布局文件在后面):
分析:
通过Handler机制更新Progress,在点击“同时增加”按钮后,分别post两个Runnable对象到消息队列,然后执行Runnable里面的run方法,在进度数值增加后 发送message到消息队列,最后handleMessage,更新进度条,然后再分别post Runnable对象。。。如此循环直到进度条满。
目前的结论
通过测试 “逐步增加”“跳跃增加”“同时增加”三个执行过程的TraceView,结果看不出有什么差别。看了相关的源码,觉得在处理过程中耗时操作就是this锁了吧。由于每隔10ms增加1%发送消息太频繁、界面更新也频繁,会导致逐步增加的速度偏慢。不知各位大神如何看这个问题,希望大家不吝赐教。
在贴源码之前先说一句,网上查到的一些利用Handler更新进度条的代码是有问题的。他在removeCallbacks之前又post了runnable,导致remove掉上一个runnable后又加入一个新的runnable,这样无限循环下去,占用资源。
Demo
MainActivity.java
package com.example.progress;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity implements OnClickListener
{
private static final int PERCENT_TOTAL = 100;
ProgressBar mProgressFluency, mProgressJumpy;
int percentFluency = 0;
int percentJumpy = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressFluency = (ProgressBar) findViewById(R.id.progressBar1);
mProgressJumpy = (ProgressBar) findViewById(R.id.progressBar2);
((Button) findViewById(R.id.button_fluency)).setOnClickListener(this);
((Button) findViewById(R.id.button_jumpy)).setOnClickListener(this);
((Button) findViewById(R.id.button_reset)).setOnClickListener(this);
((Button) findViewById(R.id.button_syn_increase)).setOnClickListener(this);
}
final Handler mHandler = new Handler() {
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case What.FLUENCY:
mProgressFluency.setProgress(msg.arg1);
mHandler.post(mUpdateProgressFluency);
break;
case What.JUMPY:
mProgressJumpy.setProgress(msg.arg1);
mHandler.post(mUpdateProgressJumpy);
break;
default:
break;
}
}
};
static class What
{
final static int FLUENCY = 1;
final static int JUMPY = 2;
}
final Runnable mUpdateProgressFluency = new Runnable() {
final int everyTimeAddF = 1;
final int delay = 1000 / (100 / everyTimeAddF);
@Override
public void run()
{
percentFluency += everyTimeAddF;
if (percentFluency > PERCENT_TOTAL)
{
mHandler.removeCallbacks(mUpdateProgressFluency);
}
else
{
Message msg = mHandler.obtainMessage();
msg.arg1 = percentFluency;
msg.what = What.FLUENCY;
mHandler.sendMessageDelayed(msg, delay);
}
}
};
final int everyTimeAdd = 25;
final int delay = 1000 / (100 / everyTimeAdd);
final Runnable mUpdateProgressJumpy = new Runnable() {
@Override
public void run()
{
percentJumpy += everyTimeAdd;
if (percentJumpy > PERCENT_TOTAL)
{
mHandler.removeCallbacks(mUpdateProgressJumpy);
}
else
{
Message msg = mHandler.obtainMessage();
msg.arg1 = percentJumpy;
msg.what = What.JUMPY;
mHandler.sendMessageDelayed(msg, delay);
}
}
};
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button_fluency:
percentFluency = 0;
mProgressFluency.setVisibility(View.VISIBLE);
mHandler.post(mUpdateProgressFluency);
break;
case R.id.button_jumpy:
percentJumpy = 0;
mProgressJumpy.setVisibility(View.VISIBLE);
mHandler.post(mUpdateProgressJumpy);
break;
case R.id.button_reset:
mHandler.removeCallbacks(mUpdateProgressFluency);
mProgressFluency.setProgress(0);
mHandler.removeCallbacks(mUpdateProgressJumpy);
mProgressJumpy.setProgress(0);
break;
case R.id.button_syn_increase:
percentFluency = 0;
mProgressFluency.setVisibility(View.VISIBLE);
mHandler.post(mUpdateProgressFluency);
percentJumpy = 0;
mProgressJumpy.setVisibility(View.VISIBLE);
mHandler.postDelayed(mUpdateProgressJumpy, delay);
break;
default:
break;
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.wps.progress.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/button_fluency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="逐步增加" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_jumpy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="跳跃增加" />
</LinearLayout>
<Button
android:id="@+id/button_syn_increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同时增加" />
<Button
android:id="@+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置" />
</LinearLayout>