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

Commit

Permalink
Merge branch 'master' into ancirja/view_property_animator_extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ancirja authored Jun 11, 2018
2 parents 47881f8 + 89ee2e1 commit 06fdd90
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ package androidx.core.graphics {
method @RequiresApi(26) @ColorLong public static long toColorLong(int);
}

public final class ImageDecoderKt {
ctor public ImageDecoderKt();
method @RequiresApi(28) public static android.graphics.Bitmap decodeBitmap(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3<? super android.graphics.ImageDecoder,? super android.graphics.ImageDecoder.ImageInfo,? super android.graphics.ImageDecoder.Source,kotlin.Unit> action);
method @RequiresApi(28) public static android.graphics.drawable.Drawable decodeDrawable(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3<? super android.graphics.ImageDecoder,? super android.graphics.ImageDecoder.ImageInfo,? super android.graphics.ImageDecoder.Source,kotlin.Unit> action);
}

public final class MatrixKt {
ctor public MatrixKt();
method public static android.graphics.Matrix rotationMatrix(float degrees, float px = "0.0f", float py = "0.0f");
Expand Down
58 changes: 58 additions & 0 deletions src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt
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())
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/androidx/core/graphics/ImageDecoder.kt
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)
}
}

0 comments on commit 06fdd90

Please sign in to comment.