我有一个片段,其中显示了33个以上的文本视图和一个折线图。屏幕旋转非常慢,Android Studio这样说:“ I / Choreographer:已跳过49帧!该应用程序可能在其主线程上做过多的工作。”丢帧的数量随着每一次新的旋转而增加。如果数据显示在活动本身中(即不在片段中),则不会发生这种情况。即使我注释掉处理数据的每一行代码并仅保留片段的最小框架,问题仍然存在。 我不知道我在做什么错,但是由于即使没有处理任何数据,问题仍然存在,我猜这是由布局引起的吗?
活动:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ButterKnife.bind(this);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SensorDataFragment dataFragment = new SensorDataFragment();
fragmentTransaction.add(R.id.fragment_container, dataFragment).commit();
片段:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.sensor_data_fragment, container, false);
return rootView;
}
活动布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/device_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appbar_layout"
android:visibility="gone"
tools:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</androidx.core.widget.NestedScrollView>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlinkyActivity"
tools:ignore="ContentDescription">
<LinearLayout
android:id="@+id/progress_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/connection_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="16sp"
tools:text="@string/state_connecting"/>
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:indeterminate="true"/>
</LinearLayout>
<include
android:id="@+id/not_supported"
layout="@layout/info_not_supported"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
tools:visibility="visible"/>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
片段布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:id="@+id/spect_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:background="@android:color/white"
app:cardElevation="1dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/spect_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/spect_tool_bar"
android:layout_width="0dp"
android:layout_height="?actionBarSize"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:logo="@drawable/outline_opacity_24"
app:title="@string/title_spectrum"
app:titleMarginStart="@dimen/toolbar_title_margin" />
<TextView
android:id="@+id/spect_summary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:text="@string/spectrum_summary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spect_tool_bar" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/spect_chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingBottom="0dp"
app:layout_constraintTop_toBottomOf="@+id/spect_summary" />
<ImageView
android:id="@+id/menu_numberformat"
android:contentDescription="@string/menu_number_format_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:onClick="showNumberFormatPopup"
app:srcCompat="@drawable/outline_more_horiz_24"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spect_chart" />
<TextView
android:id="@+id/sensor_label_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingBottom="5dp"
android:text="@string/sensor_label_1"
app:layout_constraintLeft_toRightOf="@+id/f1_label"
app:layout_constraintTop_toBottomOf="@+id/spect_chart" />
<TextView
android:id="@+id/sensor_label_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingBottom="0dp"
android:text="@string/sensor_label_2"
app:layout_constraintLeft_toRightOf="@+id/sensor_label_1"
app:layout_constraintTop_toBottomOf="@+id/spect_chart" />
<TextView
android:id="@+id/sensor_label_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingBottom="0dp"
android:text="@string/sensor_label_3"
app:layout_constraintLeft_toRightOf="@+id/sensor_label_2"
app:layout_constraintTop_toBottomOf="@+id/spect_chart" />
<TextView
android:id="@+id/f1_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingEnd="32dp"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingBottom="0dp"
android:text="@string/f1_data_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sensor_label_1" />
<TextView
android:id="@+id/s1f1_data"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingBottom="0dp"
android:text="@string/spectrum_unknown"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/sensor_label_1"
app:layout_constraintEnd_toEndOf="@+id/sensor_label_1"
app:layout_constraintLeft_toLeftOf="@+id/sensor_label_1"
app:layout_constraintTop_toBottomOf="@+id/sensor_label_1" />
// a lot of more text views
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
可能是由布局引起的吗?我的意思是,如果没有数据处理,还有什么会导致问题?非常感谢您的帮助!
答案 0 :(得分:1)
编舞者只能检测到跳帧。它无法说明为什么会发生这种情况。
但是我认为问题是由于片段中处理了大量数据
最好的方法是在与主UI线程分开的线程中进行所有处理,以访问您的数据然后绘制
重复这种具有相同属性的视图(例如“ TextView”)的行为,最好不要使用RecyclerView并将数据列表传递给Recycler适配器
答案 1 :(得分:0)
Android系统每秒渲染60帧。这意味着每16 ms屏幕重绘一次。 (1000/60 = 16.6)
由于我们可以了解有效的布局层次结构,因此不要创建很多嵌套的视图组。 Read more。
也考虑使用ConstraintLayout进行相对定位。