Created
December 24, 2018 07:49
-
-
Save qichuan/7b2b201c98c8c4b36d4ef7aca7deee57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* In order to stress the memory usage, | |
* this activity creates 100000 rows of TextView when user clicks on the start button | |
*/ | |
class HighMemoryUsageActivity : AppCompatActivity() { | |
val NO_OF_TEXTVIEWS_ADDED = 100000 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_high_memory_usage) | |
supportActionBar?.setDisplayHomeAsUpEnabled(true) | |
supportActionBar?.setTitle(R.string.activity_name_high_memory_usage) | |
btn_start.setOnClickListener { | |
addRowsOfTextView() | |
} | |
} | |
override fun onSupportNavigateUp(): Boolean { | |
onBackPressed() | |
return true | |
} | |
/** | |
* Add rows of text views to the root LinearLayout | |
*/ | |
private fun addRowsOfTextView() { | |
val linearLayout = findViewById<LinearLayout>(R.id.linear_layout) | |
val textViewParams = LinearLayout.LayoutParams( | |
LinearLayout.LayoutParams.MATCH_PARENT, | |
LinearLayout.LayoutParams.WRAP_CONTENT | |
) | |
val textViews = arrayOfNulls<TextView>(NO_OF_TEXTVIEWS_ADDED) | |
for (i in 0 until NO_OF_TEXTVIEWS_ADDED) { | |
textViews[i] = TextView(this) | |
textViews[i]?.layoutParams = textViewParams | |
textViews[i]?.text = i.toString() | |
textViews[i]?.setBackgroundColor(getRandomColor()) | |
linearLayout.addView(textViews[i]) | |
linearLayout.invalidate() | |
} | |
} | |
/** | |
* Creates a random color for background color of the text view. | |
*/ | |
private fun getRandomColor(): Int { | |
val r = Random() | |
val red = r.nextInt(255) | |
val green = r.nextInt(255) | |
val blue = r.nextInt(255) | |
return Color.rgb(red, green, blue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment