提高android布局文件的性能

我正在尝试提高版式的性能。

但是我无法增加更多。

提高android布局文件的性能

正如您在此处看到的,DataBindingUtil.setContentView花费的时间太长。

这是完整行;

    mBinding = DataBindingUtil.setContentView(MediaPlayeractivity.this,R.layout.activity_media_player);

这是布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout 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:animateLayoutChanges="true">

    <data>
        <variable
            name="viewModel"
            type="br.com.gomus.androidapp.presentation.main.MediaPlayerViewModel"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        tools:mContext="br.com.gomus.androidapp.ui.main.MediaPlayeractivity">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/fragmentContainer"

            />


        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar"
            app:layout_constraintBottom_toTopOf="@id/bottomPlayer"
            />

        <include
            android:id="@+id/bottomPlayer"
            layout="@layout/bottom_player"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:viewModel="@{viewModel}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/fragmentContainer"


            />

    </android.support.constraint.ConstraintLayout>

</layout>

这是我在activity_media_player.xml末尾充气的 bottom_player.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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:animateLayoutChanges="true">

    <data>

        <variable
            name="viewModel"
            type="br.com.gomus.androidapp.presentation.main.MediaPlayerViewModel" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/transparent"
        android:visibility="@{viewModel.bottomPlayerVisibility}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <SeekBar
            android:id="@+id/playerSeekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:foreground="@drawable/song_selector"
            android:max="1000"
            android:nextFocusDown="@id/playPauseBottom"
            android:onProgressChanged="@{viewModel::onProgressChanged}"
            android:progress="@{viewModel.seekBarPosition}"
            android:progressBackgroundTint="@color/blue_grey"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:removePadding="@{true}" />


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/playerSeekBar">

            <TextView
                android:id="@+id/songTitle"
                style="@style/TitleTextBottomPlayer"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:gravity="center"
                android:maxLines="2"
                android:text="@{viewModel.bottomPlayerTitle}"
                app:layout_constraintEnd_toStartOf="@id/openmusicPlayer"
                app:layout_constraintStart_toEndOf="@id/playPauseBottom"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="Nothing's Gonna Change the Way You Feel about me now" />

            <TextView
                style="@style/SubTitleTextBottomPlayer"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:maxLines="1"
                android:gravity="center"
                android:text="@{viewModel.bottomPlayerArtist}"
                app:layout_constraintEnd_toStartOf="@id/openmusicPlayer"
                app:layout_constraintStart_toEndOf="@id/playPauseBottom"
                app:layout_constraintTop_toBottomOf="@id/songTitle"
                tools:text="Nothing's Gonna Change" />

            <ImageView
                android:id="@+id/playPauseBottom"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginBottom="15dp"
                android:layout_marginStart="10dp"
                android:background="@drawable/button_player_selector"
                android:focusable="true"
                android:nextFocusRight="@id/openmusicPlayer"
                android:nextFocusUp="@id/playerSeekBar"
                android:onClick="@{() -> viewModel.openPlayer()}"
                android:padding="8dp"
                android:src="@drawable/ic_back_playlist"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/openmusicPlayer"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginBottom="15dp"
                android:layout_marginEnd="10dp"
                android:background="@drawable/button_player_selector"
                android:focusable="true"
                android:nextFocusLeft="@id/playPauseBottom"
                android:nextFocusUp="@id/playerSeekBar"
                android:onClick="@{() -> viewModel.playPause()}"
                android:padding="8dp"
                android:src="@{viewModel.playing ? @drawable/ic_pause_pequeno : @drawable/ic_play_pequeno}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:src="@drawable/ic_play_pequeno" />


        </android.support.constraint.ConstraintLayout>


    </android.support.constraint.ConstraintLayout>

</layout>
zilan112 回答:提高android布局文件的性能

我的建议是使布局平坦,并将所有内容放到ConstraintLayout中。

另一件事是检查所有ImageView背景资源,以便它们不会很大,如果需要24dp宽度的图像,那么如果需要,请使用不大于24dp宽度的图像不同屏幕的大小不同,然后使用....-screen size

将不同的文件夹添加到您的资源中

android:animateLayoutChanges="true"行也应该在ConstraintLayout中向上一级,而不应该在<layout中。

本文链接:https://www.f2er.com/3124303.html

大家都在问