This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ancirja/view_property_animator_extensions
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.core.graphics | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.ImageDecoder | ||
import org.junit.Assert.assertEquals | ||
import org.junit.BeforeClass | ||
import org.junit.Test | ||
import java.io.ByteArrayOutputStream | ||
import java.nio.ByteBuffer | ||
|
||
class ImageDecoderTest { | ||
|
||
@Test fun decodeBitmap() { | ||
val src = ImageDecoder.createSource(buffer) | ||
val decodedBitmap = src.decodeBitmap { _, _ -> setTargetSize(10, 10) } | ||
assertEquals(10, decodedBitmap.width) | ||
assertEquals(10, decodedBitmap.height) | ||
} | ||
|
||
@Test fun decodeDrawable() { | ||
val src = ImageDecoder.createSource(buffer) | ||
val decodedDrawable = src.decodeDrawable { _, _ -> setTargetSize(10, 10) } | ||
assertEquals(10, decodedDrawable.intrinsicWidth) | ||
assertEquals(10, decodedDrawable.intrinsicHeight) | ||
} | ||
|
||
companion object { | ||
private lateinit var buffer: ByteBuffer | ||
|
||
@BeforeClass | ||
@JvmStatic | ||
fun beforeClass() { | ||
val stream = ByteArrayOutputStream().apply { | ||
Bitmap | ||
.createBitmap(1, 1, Bitmap.Config.ARGB_8888) | ||
.compress(Bitmap.CompressFormat.JPEG, 100, this) | ||
} | ||
|
||
buffer = ByteBuffer.wrap(stream.toByteArray()) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.core.graphics | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.ImageDecoder | ||
import android.graphics.ImageDecoder.ImageInfo | ||
import android.graphics.ImageDecoder.Source | ||
import android.graphics.drawable.Drawable | ||
import androidx.annotation.RequiresApi | ||
|
||
/** | ||
* Create a Bitmap from a Source | ||
* | ||
* @see ImageDecoder.decodeBitmap | ||
*/ | ||
@RequiresApi(28) | ||
inline fun ImageDecoder.Source.decodeBitmap( | ||
crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit | ||
): Bitmap { | ||
return ImageDecoder.decodeBitmap(this) { decoder, info, source -> | ||
decoder.action(info, source) | ||
} | ||
} | ||
|
||
/** | ||
* Create a Drawable from a Source | ||
* | ||
* @see ImageDecoder.decodeDrawable | ||
*/ | ||
@RequiresApi(28) | ||
inline fun ImageDecoder.Source.decodeDrawable( | ||
crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit | ||
): Drawable { | ||
return ImageDecoder.decodeDrawable(this) { decoder, info, source -> | ||
decoder.action(info, source) | ||
} | ||
} |