Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Extension property to get/set bitmap from ImageView #579

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/androidTest/java/androidx/core/widget/ImageViewTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package androidx.core.widget

import android.graphics.Bitmap
import org.junit.Before
import android.support.test.InstrumentationRegistry
import android.widget.ImageView
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

class ImageViewTest {

private lateinit var fakeBitmap: Bitmap
private val context = InstrumentationRegistry.getContext()

@Before
fun setup() {
val dim = 100
fakeBitmap = Bitmap.createBitmap(dim, dim, Bitmap.Config.ARGB_8888)
}

@Test
fun setBitmapTest() {
val imageView = ImageView(context)
imageView.bitmap = fakeBitmap
assertTrue(fakeBitmap.sameAs(imageView.bitmap))
}

@Test
fun emptyBitmapTest() {
val imageView = ImageView(context)
assertNull(imageView.bitmap)
}

}
28 changes: 28 additions & 0 deletions src/main/java/androidx/core/widget/ImageView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package androidx.core.widget

import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.widget.ImageView


/**
* Returns Bitmap if set to imageview if not than it will return null
*
* ```
* val myBitmap:Bitmap? = imageView.bitmap
* ```
*
* Setting a bitmap to this property using [ImageView.setImageBitmap]
*
* ```
* imageView.bitmap = myBitmap
* ```
*/
inline var ImageView.bitmap: Bitmap?
get() {
val drawable = drawable as? BitmapDrawable
return drawable?.bitmap
}
set(value) {
setImageBitmap(value)
}