Skip to content

Instantly share code, notes, and snippets.

@floriangbh
Created September 10, 2020 06:53
Show Gist options
  • Save floriangbh/dc452ee09c2ca4cc99d0bdd8811aaaf2 to your computer and use it in GitHub Desktop.
Save floriangbh/dc452ee09c2ca4cc99d0bdd8811aaaf2 to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onResume() {
super.onResume()
val customView = CustomView(applicationContext)
customView.measure(View.MeasureSpec.makeMeasureSpec(1280, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(720, View.MeasureSpec.EXACTLY))
customView.layout(0, 0, customView.measuredWidth, customView.measuredHeight)
loadBitmapFromView(customView).let { bitmap ->
this.imageView.setImageBitmap(bitmap)
}
}
fun loadBitmapFromView(view: View): Bitmap? {
val bitmap: Bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.layout(view.left, view.top, view.right, view.bottom)
view.draw(canvas)
return bitmap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment