Android ViewStub延迟初始化加载布局View,Kotlin
activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="hello" /><ViewStubandroid:id="@+id/my_view_stub"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout="@layout/my_long_time_view" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="world" />
</LinearLayout>
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewStub
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContextclass MyActivity : AppCompatActivity() {companion object {const val TAG = "fly/MyActivity"}private var mViewStub: ViewStub? = nullprivate var mMyLongTimeView: MyLongTimeView? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)Log.d(TAG, "setContentView开始...")setContentView(R.layout.activity_my)Log.d(TAG, "setContentView结束")mViewStub = findViewById(R.id.my_view_stub)lifecycleScope.launch(Dispatchers.IO) {delay(2000)withContext(Dispatchers.Main) {showViewStub()}delay(2000)withContext(Dispatchers.Main) {hideViewStub()}}}private fun showViewStub() {val t = System.currentTimeMillis()Log.d(TAG, "ViewStub inflate")val inflatedView = mViewStub?.inflate()//mViewStub?.visibility = View.INVISIBLELog.d(TAG, "inflatedView 耗时=${System.currentTimeMillis() - t}")mMyLongTimeView = inflatedView?.findViewById(R.id.my_longt_time_view)!!mMyLongTimeView?.setImageResource(R.mipmap.image)}private fun hideViewStub() {mViewStub?.visibility = View.INVISIBLE}
}
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import androidx.appcompat.widget.AppCompatImageViewclass MyLongTimeView : AppCompatImageView {companion object {const val TAG = "fly/MyLongTimeView"}constructor(ctx: Context, attribute: AttributeSet) : super(ctx, attribute) {Log.d(TAG, "开始耗时...")Thread.sleep(5000)Log.d(TAG, "耗时结束")}
}
my_long_time_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.MyLongTimeViewandroid:id="@+id/my_longt_time_view"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="centerCrop" />
</LinearLayout>
启动后就可以看到TextView的hello,world显示出来,随后显示图片,再随后隐藏图片。
Android GPU渲染屏幕绘制显示基础概念(1)-CSDN博客文章浏览阅读2.3k次,点赞33次,收藏30次。CPU返回后,会直接将GraphicBuffer提交给SurfaceFlinger,告诉SurfaceFlinger进行合成,但是这个时候GPU可能并未完成之前的图像渲染,这时候就牵扯到一个同步,Android中,用的是Fence机制,SurfaceFlinger合成前会查询Fence,如果GPU渲染没有结束,则等待GPU渲染结束,GPU结束后,会通知SurfaceFlinger进行合成,SF合成后,提交显示,最终完成图像的渲染显示。而对SF来说,只要有合成任务,它就得再去申请VSYNC-sf。_android gpu渲染https://blog.csdn.net/zhangphil/article/details/138585120