Skip to content

Instantly share code, notes, and snippets.

@LeonidIvankin
Created July 18, 2022 05:34
Show Gist options
  • Save LeonidIvankin/9a6316c0d9e3be6f60076c858e4c1b02 to your computer and use it in GitHub Desktop.
Save LeonidIvankin/9a6316c0d9e3be6f60076c858e4c1b02 to your computer and use it in GitHub Desktop.
package com.leonidivankin.draftandroid.articles.benchmark
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import androidx.core.view.children
import com.leonidivankin.draftandroid.R
class HybridWidthActivity : AppCompatActivity() {
var time = 0L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = "hybrid: xml+compose, width"
val colors = listOf(
0xFFF44336,
0xFFE91E63,
0xFF9C27B0,
0xFF673AB7,
0xFF3F51B5,
0xFF2196F3,
0xFF03A9F4,
0xFF00BCD4,
0xFF009688,
0xFF4CAF50,
0xFF8BC34A,
0xFFCDDC39,
0xFFFFEB3B,
0xFFFFC107,
0xFFFF9800,
0xFFFF5722
)
time = System.currentTimeMillis()
setContentView(R.layout.activity_hybrid_width)
findViewById<LinearLayout>(R.id.hybridWidthLinearLayout).children.forEachIndexed { index, view ->
(view as ComposeView).setContent { MyBox(colors[index % colors.size]) }
}
}
override fun onResume() {
super.onResume()
Log.d("Benchmark", "HybridWidthActivity onResume: ${System.currentTimeMillis() - time}")
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
Log.d(
"Benchmark",
"HybridWidthActivity onWindowFocusChanged: ${System.currentTimeMillis() - time}"
)
}
}
}
@Composable
private fun MyBox(color: Long) {
Box(
Modifier
.fillMaxWidth()
.height(8.dp)
.background(color = Color(color))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment