Activity 跳过 100 帧?花了太长时间才出现?
Activity skipping 100s of frames? Taking too long to appear?
Activity 显示时间过长。我不知道为什么,它的布局很简单,但每次都会跳过 500 帧或更多帧。我没有做任何繁重的工作,我只设置主布局文件太愚蠢了。
package zafus.pointme;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class CreateEventActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_create_event, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这里是layout.xml activity。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="zafus.pointme.CreateEventActivity">
<TextView android:id="@+id/create_event_fromal" android:text="Create new event and send invites to your friends and family." android:textSize="20sp" android:textColor="#0099cc" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/recepient"
android:layout_below="@+id/create_event_fromal"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:text="Send To: "
android:textColor="#0099cc"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/recepient_contact"
android:layout_width="fill_parent"
android:layout_below="@+id/create_event_fromal"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/recepient"
android:hint="Tap to add recepients"
android:layout_alignBaseline="@+id/recepient"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_below="@+id/recepient_contact"
android:layout_marginTop="10dp"
android:id="@+id/create_event_image"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/event_picture"
android:layout_width="100dp"
android:scaleType="fitXY"
android:src="@drawable/arrow_use"
android:layout_height="100dp" />
<TextView
android:textColor="#0099cc"
android:layout_width="100dp"
android:text="Featured Image"
android:layout_height="wrap_content" />
</LinearLayout>
<EditText
android:layout_width="fill_parent"
android:id="@+id/title"
android:hint="Event Title"
android:layout_height="wrap_content"
android:layout_below="@+id/recepient"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@+id/create_event_image"
android:layout_toRightOf="@+id/create_event_image" />
<TextView
android:id="@+id/when"
android:layout_width="wrap_content"
android:textColor="#0099cc"
android:text="When: "
android:layout_marginBottom="21dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/create_event_image"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/comment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/create_event_image"
android:layout_marginTop="4dp"
android:hint="Comment if any"
/>
<CalendarView
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginBottom="4dp"
android:layout_above="@+id/comment"
android:layout_toRightOf="@+id/when"
android:layout_toEndOf="@+id/when">
</CalendarView>
</RelativeLayout>
logcat 是这么说的:
07-17 13:15:18.001 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.001 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.003 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.003 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.004 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.004 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.088 637-637/zafus.pointme I/Choreographer﹕ Skipped 372 frames! The application may be doing too much work on its main thread.
07-17 13:15:26.544 637-652/zafus.pointme I/art﹕ Background partial concurrent mark sweep GC freed 212268(7MB) AllocSpace objects, 0(0B) LOS objects, 36% free, 28MB/44MB, paused 3.387ms total 136.810ms
07-17 13:15:41.899 637-647/zafus.pointme W/art﹕ Suspending all threads took: 19.104ms
07-17 13:15:41.912 637-652/zafus.pointme I/art﹕ Background sticky concurrent mark sweep GC freed 414866(14MB) AllocSpace objects, 0(0B) LOS objects, 21% free, 29MB/37MB, paused 2.258ms total 132.690ms
07-17 13:16:45.033 637-647/zafus.pointme W/art﹕ Suspending all threads took: 7.965ms
07-17 13:17:09.585 637-647/zafus.pointme W/art﹕ Suspending all threads took: 6.500ms
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.265 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.265 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.293 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.293 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.293 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.297 637-637/zafus.pointme I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@7dca64b time:14405839
加载 CalendarView
时似乎有一些奇怪的行为。我最近通过设置 android:calendarViewShown="false"
在 DatePicker
中处理了这个问题,这很好地加快了速度。也许你也可以试试这个。
编辑:SO 有几个关于 CalendarViews 性能的问题,例如 CalendarView takes much time for displaying
您可以使用 Android Studio 中的探查器找到此类速度问题的原因:
Activity 显示时间过长。我不知道为什么,它的布局很简单,但每次都会跳过 500 帧或更多帧。我没有做任何繁重的工作,我只设置主布局文件太愚蠢了。
package zafus.pointme;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class CreateEventActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_create_event, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这里是layout.xml activity。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="zafus.pointme.CreateEventActivity">
<TextView android:id="@+id/create_event_fromal" android:text="Create new event and send invites to your friends and family." android:textSize="20sp" android:textColor="#0099cc" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/recepient"
android:layout_below="@+id/create_event_fromal"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:text="Send To: "
android:textColor="#0099cc"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/recepient_contact"
android:layout_width="fill_parent"
android:layout_below="@+id/create_event_fromal"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/recepient"
android:hint="Tap to add recepients"
android:layout_alignBaseline="@+id/recepient"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_below="@+id/recepient_contact"
android:layout_marginTop="10dp"
android:id="@+id/create_event_image"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/event_picture"
android:layout_width="100dp"
android:scaleType="fitXY"
android:src="@drawable/arrow_use"
android:layout_height="100dp" />
<TextView
android:textColor="#0099cc"
android:layout_width="100dp"
android:text="Featured Image"
android:layout_height="wrap_content" />
</LinearLayout>
<EditText
android:layout_width="fill_parent"
android:id="@+id/title"
android:hint="Event Title"
android:layout_height="wrap_content"
android:layout_below="@+id/recepient"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@+id/create_event_image"
android:layout_toRightOf="@+id/create_event_image" />
<TextView
android:id="@+id/when"
android:layout_width="wrap_content"
android:textColor="#0099cc"
android:text="When: "
android:layout_marginBottom="21dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/create_event_image"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/comment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/create_event_image"
android:layout_marginTop="4dp"
android:hint="Comment if any"
/>
<CalendarView
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginBottom="4dp"
android:layout_above="@+id/comment"
android:layout_toRightOf="@+id/when"
android:layout_toEndOf="@+id/when">
</CalendarView>
</RelativeLayout>
logcat 是这么说的:
07-17 13:15:18.001 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.001 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.003 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.003 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.004 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.004 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:15:18.088 637-637/zafus.pointme I/Choreographer﹕ Skipped 372 frames! The application may be doing too much work on its main thread.
07-17 13:15:26.544 637-652/zafus.pointme I/art﹕ Background partial concurrent mark sweep GC freed 212268(7MB) AllocSpace objects, 0(0B) LOS objects, 36% free, 28MB/44MB, paused 3.387ms total 136.810ms
07-17 13:15:41.899 637-647/zafus.pointme W/art﹕ Suspending all threads took: 19.104ms
07-17 13:15:41.912 637-652/zafus.pointme I/art﹕ Background sticky concurrent mark sweep GC freed 414866(14MB) AllocSpace objects, 0(0B) LOS objects, 21% free, 29MB/37MB, paused 2.258ms total 132.690ms
07-17 13:16:45.033 637-647/zafus.pointme W/art﹕ Suspending all threads took: 7.965ms
07-17 13:17:09.585 637-647/zafus.pointme W/art﹕ Suspending all threads took: 6.500ms
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.262 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.265 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.265 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.293 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.293 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.293 637-637/zafus.pointme D/BubblePopupHelper﹕ isShowingBubblePopup : false
07-17 13:17:19.297 637-637/zafus.pointme I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@7dca64b time:14405839
加载 CalendarView
时似乎有一些奇怪的行为。我最近通过设置 android:calendarViewShown="false"
在 DatePicker
中处理了这个问题,这很好地加快了速度。也许你也可以试试这个。
编辑:SO 有几个关于 CalendarViews 性能的问题,例如 CalendarView takes much time for displaying
您可以使用 Android Studio 中的探查器找到此类速度问题的原因: