我在滚动视图中有一个约束布局。在该约束布局中,我有一个支持地图片段。
我正在尝试找出导致我的应用缓慢呈现的原因。显示包含地图的片段(支持地图片段)大约需要1.5秒。我的意思是,当我将片段A替换为具有地图的片段B时,它会花费太长时间,在1-2秒后,片段B将出现在屏幕上。
这是我的xml
<ScrollView 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"
android:background="@android:color/white" android:id="@+id/scrollView_event_detail">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Fragments.Reusable.EventDetailFragment"
android:id="@+id/constraintLayout_event_detail_destination">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" tools:src="@tools:sample/backgrounds/scenic[12]"
android:id="@+id/imageView_event_detail_blurred_poster"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:scaleType="centerCrop" app:layout_constraintDimensionRatio="1:1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" tools:src="@tools:sample/backgrounds/scenic[12]"
android:id="@+id/imageView_event_detail_poster"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintDimensionRatio="1:1"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/textView_event_detail_event_title"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/imageView_event_detail_blurred_poster"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp" android:maxLines="2"
android:textSize="18sp" tools:text="Kajian Bulanan: Memahami Adab Para Penuntut Ilmu"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="@+id/line1_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_event_title"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<Button
android:text="Hadir"
android:layout_width="0dp"
android:layout_height="50dp" android:id="@+id/button_search_event_search"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/line1_view_event_detail"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
android:background="@drawable/rounded_button"
android:textColor="#ffffff"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="@+id/line2_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/button_search_event_search"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_date_time"
android:id="@+id/imageView_event_detail_icon_dateTime"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="@+id/line2_view_event_detail" android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_date_label"
app:layout_constraintTop_toTopOf="@+id/imageView_event_detail_icon_dateTime"
app:layout_constraintBottom_toBottomOf="@+id/imageView_event_detail_icon_dateTime"
app:layout_constraintStart_toEndOf="@+id/imageView_event_detail_icon_dateTime"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Selasa, 23 Desember 2018 - Rabu, 24 Desember 2018"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_time_label"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_date_label"
app:layout_constraintStart_toStartOf="@+id/textView_event_detail_date_label"
tools:text="17.00 - 21.00 WIB"
android:maxLines="1"
android:layout_marginTop="4dp" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_place"
android:id="@+id/imageView_event_detail_icon_address"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_time_label"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_venue_label"
app:layout_constraintTop_toTopOf="@+id/imageView_event_detail_icon_address"
app:layout_constraintBottom_toBottomOf="@+id/imageView_event_detail_icon_address"
app:layout_constraintStart_toEndOf="@+id/imageView_event_detail_icon_address"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Universitas Gadjah Mada"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_address_label"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_venue_label"
app:layout_constraintStart_toStartOf="@+id/textView_event_detail_venue_label"
tools:text="Jalan Grafika no.1. Sinduadi Sleman. Yogyakarta"
android:maxLines="1"
android:layout_marginTop="4dp" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_person"
android:id="@+id/imageView_event_detail_icon_speaker"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_address_label" android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_speaker_label"
app:layout_constraintTop_toTopOf="@+id/imageView_event_detail_icon_speaker"
app:layout_constraintBottom_toBottomOf="@+id/imageView_event_detail_icon_speaker"
app:layout_constraintStart_toEndOf="@+id/imageView_event_detail_icon_speaker"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Pembicara: Ustadz Khalid Basalamah"
android:maxLines="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_monetization"
android:id="@+id/imageView_event_detail_icon_price"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_speaker_label"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_price_label"
app:layout_constraintTop_toTopOf="@+id/imageView_event_detail_icon_price"
app:layout_constraintBottom_toBottomOf="@+id/imageView_event_detail_icon_price"
app:layout_constraintStart_toEndOf="@+id/imageView_event_detail_icon_price"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Rp 50.000"
android:maxLines="1"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="@+id/line3_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_price_label"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_description_snippet_label"
app:layout_constraintTop_toTopOf="@+id/line3_view_event_detail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Kajian akan membahas kitab Riyadush Shalihin Bab 3"
android:maxLines="2"/>
<TextView
android:text="Baca Selengkapnya"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView_button_read_more"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_description_snippet_label"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true" android:textColor="#2196F3"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="@+id/line4_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView_button_read_more"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="180dp"
android:id="@+id/map_event_detail"
tools:context=".Activities.MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/line4_view_event_detail"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:text="Peta Ke Lokasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView_button_map_more"
app:layout_constraintTop_toBottomOf="@+id/map_event_detail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true" android:textColor="#2196F3"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="@+id/line5_view_event_detail" android:background="#696C79"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/textView_button_map_more"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<TextView
android:text="Penyelenggara"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_organizer_tag"
app:layout_constraintTop_toBottomOf="@+id/line5_view_event_detail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"/>
<TextView
android:text="Masjid Nurul Iman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView_event_detail_organizer_label"
app:layout_constraintTop_toBottomOf="@+id/textView_event_detail_organizer_tag"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
我试图注释掉onCreateView中的所有代码。所以这个片段什么也没做。而且,当显示此片段时,我的应用程序渲染缓慢。
但是当我尝试从xml中删除支持地图片段时,我的意思是这部分:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="180dp"
android:id="@+id/map_event_detail"
tools:context=".Activities.MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/line4_view_event_detail"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
这将使我的应用程序渲染更快。那么为什么会发生这种情况,或者有其他解决方案吗?
我的意思是要在滚动视图内的约束布局中显示地图?
这是我的代码:
class EventDetailFragment : Fragment() {
lateinit var titleTextView: TextView
lateinit var fragmentView : View
lateinit var eventPosterImageView : ImageView
lateinit var eventBlurredPosterImageView: ImageView
lateinit var eventTitleTextView : TextView
lateinit var attendanceButton: Button
lateinit var dateTextView: TextView
lateinit var timeTextView : TextView
lateinit var venueTextView: TextView
lateinit var addressTextView: TextView
lateinit var speakerTextView: TextView
lateinit var priceTextView: TextView
lateinit var descriptionSnippetTextView: TextView
lateinit var readMoreTextViewButton : TextView
lateinit var mapMoreTextViewButton : TextView
lateinit var organizerTagTextView: TextView
lateinit var organizerLabelTextView: TextView
lateinit var mapFragment : SupportMapFragment
lateinit var googleMap: GoogleMap
lateinit var mContext : Context
lateinit var mActivity : FragmentActivity
lateinit var selectedEvent : Event
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
activity?.let { mActivity = it }
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentView = inflater.inflate(R.layout.fragment_event_detail, container, false)
// setUpViewsDeclaration()
// setUpSafeArg()
// setUpListeners()
// updateUI()
return fragmentView
}
private fun setUpViewsDeclaration() {
titleTextView = mActivity.findViewById(R.id.destination_label_text_view)
eventPosterImageView = fragmentView.findViewById(R.id.imageView_event_detail_poster)
eventBlurredPosterImageView = fragmentView.findViewById(R.id.imageView_event_detail_blurred_poster)
eventTitleTextView = fragmentView.findViewById(R.id.textView_event_detail_event_title)
attendanceButton = fragmentView.findViewById(R.id.button_search_event_search)
dateTextView = fragmentView.findViewById(R.id.textView_event_detail_date_label)
timeTextView = fragmentView.findViewById(R.id.textView_event_detail_time_label)
venueTextView = fragmentView.findViewById(R.id.textView_event_detail_venue_label)
addressTextView = fragmentView.findViewById(R.id.textView_event_detail_address_label)
speakerTextView = fragmentView.findViewById(R.id.textView_event_detail_speaker_label)
priceTextView = fragmentView.findViewById(R.id.textView_event_detail_price_label)
descriptionSnippetTextView = fragmentView.findViewById(R.id.textView_event_detail_description_snippet_label)
readMoreTextViewButton = fragmentView.findViewById(R.id.textView_button_read_more)
organizerTagTextView = fragmentView.findViewById(R.id.textView_event_detail_organizer_tag)
organizerLabelTextView = fragmentView.findViewById(R.id.textView_event_detail_organizer_label)
mapMoreTextViewButton = fragmentView.findViewById(R.id.textView_button_map_more)
}
private fun setUpSafeArg() {
arguments?.let {
val args = EventDetailFragmentArgs.fromBundle(it)
selectedEvent = args.selectedEvent
}
}
private fun setUpListeners() {
eventPosterImageView.setOnClickListener {
val photoViewNavigation = EventDetailFragmentDirections.actionGlobalPhotoViewFragment(selectedEvent.posterDownloadPath)
Navigation.findNavController(fragmentView).navigate(photoViewNavigation)
}
eventBlurredPosterImageView.setOnClickListener {
val photoViewNavigation = EventDetailFragmentDirections.actionGlobalPhotoViewFragment(selectedEvent.posterDownloadPath)
Navigation.findNavController(fragmentView).navigate(photoViewNavigation)
}
readMoreTextViewButton.setOnClickListener {
val nextDestination = EventDetailFragmentDirections.actionToDescriptionDetail(selectedEvent.description)
Navigation.findNavController(fragmentView).navigate(nextDestination)
}
mapMoreTextViewButton.setOnClickListener {
val latitudeFloat = selectedEvent.coordinate.latitude.toFloat()
val longitudeFloat = selectedEvent.coordinate.longitude.toFloat()
val mapDetailDestination = EventDetailFragmentDirections.actionToMapDetailLocation(latitudeFloat,longitudeFloat)
Navigation.findNavController(fragmentView).navigate(mapDetailDestination)
}
}
private fun updateUI() {
Picasso.get()
.load(selectedEvent.posterDownloadPath)
.into(eventPosterImageView)
Picasso.get()
.load(selectedEvent.posterDownloadPath)
.transform(BlurTransformation(mContext,25,3))
.into(eventBlurredPosterImageView)
eventTitleTextView.text = selectedEvent.title
val dateStartingDate = DateTimeService.changeDateToString("EEEE, d MMMM yyyy",selectedEvent.dateTimeStart)
val timeStartingDate = DateTimeService.changeDateToString("HH:mm",selectedEvent.dateTimeStart)
val dateEndingDate = DateTimeService.changeDateToString("EEEE, d MMMM yyyy",selectedEvent.dateTimeFinish)
val timeEndingDate = DateTimeService.changeDateToString("HH:mm zzz",selectedEvent.dateTimeFinish)
dateTextView.text = dateStartingDate
timeTextView.text = "$timeStartingDate - $timeEndingDate"
venueTextView.text = selectedEvent.venue
addressTextView.text = selectedEvent.address
speakerTextView.text = "Penceramah: ${selectedEvent.speaker}"
organizerLabelTextView.text = selectedEvent.creatorFullName
descriptionSnippetTextView.text = selectedEvent.description
setUpGoogleMap()
}
private fun setUpGoogleMap() {
mapFragment = childFragmentManager.findFragmentById(R.id.map_event_detail) as SupportMapFragment
mapFragment.getMapAsync {
googleMap = it
val eventLocation = LatLng(selectedEvent.coordinate.latitude,selectedEvent.coordinate.longitude)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(eventLocation,16f))
googleMap.addMarker(MarkerOptions().position(eventLocation).title("Lokasi Acara")).showInfoWindow()
googleMap.setOnMapClickListener {
val latitudeFloat = selectedEvent.coordinate.latitude.toFloat()
val longitudeFloat = selectedEvent.coordinate.longitude.toFloat()
val mapDetailDestination = EventDetailFragmentDirections.actionToMapDetailLocation(latitudeFloat,longitudeFloat)
Navigation.findNavController(fragmentView).navigate(mapDetailDestination)
}
}
}
}
答案 0 :(得分:1)
我建议使用MapView而不是SupportMapFragment,因为您可以更好地控制地图的生命周期。
我认为您无法替换该片段,因为SupportMapFragment将Map的生命周期与Activity集成在一起,并且您无法在Map完成加载之前替换它。
MapView将使您更好地控制地图,因为即使地图尚未加载,您也应该能够替换片段
查看此答案(比我更好地解释了差异):-the official installer
答案 1 :(得分:0)
您需要显示很多小部件和元素,我认为您正面临official documentation
中的垃圾UI渲染是从应用程序生成框架并将其显示在屏幕上的动作。为确保用户与您的应用的交互顺畅,您的应用应在16毫秒内渲染帧,以实现每秒60帧(为什么为60fps)。如果您的应用程序的UI呈现速度较慢,则系统将被迫跳过帧,并且用户会感觉到您的应用程序出现了卡顿现象。我们称之为垃圾。
我建议您仔细阅读文档,以更好地理解如何使UI平滑