优化抽屉,活动启动速度 [英] Optimizing drawer and activity launching speed
问题描述
我使用的是谷歌 DrawerLayout
。
当一个项目被点击,抽屉顺利关闭和活动
将陆续推出。谈到这些活动纳入片段
s是的没有的一个选项。因为这一点,推出一个活动,然后关闭抽屉也是不是一种选择。关闭抽屉和开展活动的同时会使收盘动画口吃。
When an item gets clicked, the drawer is smoothly closed and an Activity
will be launched. Turning these activities into Fragment
s is not an option. Because of this, launching an activity and then closing the drawer is also not an option. Closing the drawer and launching the activity at the same time will make the closing animation stutter.
鉴于我想顺先关闭,然后启动该活动,我有当用户点击抽屉项目,而当他们看到了活动,他们想去之间的延迟的问题。
Given that I want to smoothly close it first, and then launch the activity, I have a problem with the latency between when a user clicks on the drawer item, and when they see the activity they wanted to go to.
这是什么样的点击监听器为每个项目的样子。
This is what the click listener for each item looks like.
final View.OnClickListener mainItemClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
mViewToLaunch = v;
mDrawerLayout.closeDrawers();
}
};
我的活动也是DrawerListener,其 onDrawerClosed
的方法是这样的:
@Override
public synchronized void onDrawerClosed(final View view) {
if (mViewToLaunch != null) {
onDrawerItemSelection(mViewToLaunch);
mViewToLaunch = null;
}
}
onDrawerItemSelection
刚刚推出五大活动之一。
onDrawerItemSelection
just launches one of the five activities.
我什么都不做的的onPause
的 DrawerActivity
。
我插装这个,它需要平均为500-650ms从目前的onClick叫,到了此刻onDrawerClosed结束。
I am instrumenting this and it takes on average from 500-650ms from the moment onClick is called, to the moment onDrawerClosed ends.
有一个明显的滞后,一旦抽屉关闭时,相应的活动被启动之前。
There is a noticeable lag, once the drawer closes, before the corresponding activity is launched.
我认识一对夫妇的事情发生了:
I realize a couple of things are happening:
-
收盘动画发生,这是几毫秒的正确的有(比方说,300)。
The closing animation takes place, which is a couple of milliseconds right there (let's say 300).
此外,还有可能是视觉上的抽屉关闭和听众被解雇之间的一些延迟。我想弄清楚到底有多少这种情况正在发生<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.3_r2.1/android/support/v4/widget/DrawerLayout.java#DrawerLayout.0mListener">by看 DrawerLayout
源但还没有想通出来呢。
Then there's probably some latency between the drawer visually closing and its listener getting fired. I'm trying to figure out exactly how much of this is happening by looking at DrawerLayout
source but haven't figured it out yet.
然后有需要的启动的活动来执行它的启动生命周期方法达到所需的时间,并包括 onResume
。我还没有这个仪器,但是我估计大约200-300ms。
Then there's the amount of time it takes for the launched activity to perform its startup lifecycle methods up to, and including, onResume
. I have not instrumented this yet but I estimate about 200-300ms.
这似乎是一个问题,即下降了错误的道路将是非常昂贵的,所以我要确保我完全理解。
This seems like a problem where going down the wrong path would be quite costly so I want to make sure I fully understand it.
一个解决方法就是跳过结束动画,但我希望能保持它的身边。
One solution is just to skip the closing animation but I was hoping to keep it around.
如何才能减少我的过渡时间尽可能多地?
How can I decrease my transition time as much as possible?
推荐答案
所以,我似乎有一个合理的解决方案,解决了这个问题。
So I seem to have solved the problem with a reasonable solution.
感到延时的最大来源是当抽屉被关闭视觉之间的延迟,而当 onDrawerClosed
被调用。我通过发布的Runnable
私人处理程序
用于启动计划的活动,在一些特定的延迟解决了这一点。此延迟被选择为与抽屉关闭对应
The largest source of perceivable latency was the delay between when the drawer was visually closed, and when onDrawerClosed
was called. I solved this by posting a Runnable
to a private Handler
that launches the intended activity at some specified delay. This delay is chosen to correspond with the drawer closing.
我试图做启动 onDrawerSlide
后,80%的进度,但是这两个问题。首先是它结结巴巴。第二个是,如果你增加的百分比90%或95%的可能性,它不会被调用都由于动画的性质增加 - 你必须然后回落到 onDrawerClosed
,这违背了目的。
I tried to do the launching onDrawerSlide
after 80% progress, but this has two problems. The first was that it stuttered. The second was that if you increased the percentage to 90% or 95% the likelihood that it wouldn't get called at all due to the nature of the animation increased--and you then had to fall back to onDrawerClosed
, which defeats the purpose.
该解决方案具有口吃,特别在较旧的电话的可能性,但可能性可以简单地通过增加延迟足够高降低到0。我还以为是250ms的口吃和延迟之间取得合理的平衡。
This solution has the possibility to stutter, specially on older phones, but the likelihood can be reduced to 0 simply by increasing the delay high enough. I thought 250ms was a reasonable balance between stutter and latency.
的code这个样子的相关部分:
The relevant portions of the code look like this:
public class DrawerActivity extends SherlockFragmentActivity {
private final Handler mDrawerHandler = new Handler();
private void scheduleLaunchAndCloseDrawer(final View v) {
// Clears any previously posted runnables, for double clicks
mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(new Runnable() {
@Override
public void run() {
onDrawerItemSelection(v);
}
}, 250);
// The millisecond delay is arbitrary and was arrived at through trial and error
mDrawerLayout.closeDrawer();
}
}
这篇关于优化抽屉,活动启动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!