Skip to content

Commit

Permalink
Add an API to determine whether or not bitmaps should be rescaled
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Peal committed Jan 9, 2022
1 parent bbc80cc commit 8e13d96
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
Expand Up @@ -19,13 +19,15 @@ import com.airbnb.lottie.value.ScaleXY
* This takes a vararg of individual dynamic properties which should be created with [rememberLottieDynamicProperty].
*
* @see rememberLottieDynamicProperty
* @see LottieDrawable.setRescaleBitmaps
*/
@Composable
fun rememberLottieDynamicProperties(
rescaleBitmaps: Boolean = false,
vararg properties: LottieDynamicProperty<*>,
): LottieDynamicProperties {
return remember(properties) {
LottieDynamicProperties(properties.toList())
return remember(rescaleBitmaps, properties) {
LottieDynamicProperties(rescaleBitmaps, properties.toList())
}
}

Expand Down Expand Up @@ -91,6 +93,7 @@ class LottieDynamicProperty<T> internal constructor(
* @see rememberLottieDynamicProperties
*/
class LottieDynamicProperties internal constructor(
private val rescaleBitmaps: Boolean,
private val intProperties: List<LottieDynamicProperty<Int>>,
private val pointFProperties: List<LottieDynamicProperty<PointF>>,
private val floatProperties: List<LottieDynamicProperty<Float>>,
Expand All @@ -102,7 +105,8 @@ class LottieDynamicProperties internal constructor(
private val bitmapProperties: List<LottieDynamicProperty<Bitmap>>,
) {
@Suppress("UNCHECKED_CAST")
constructor(properties: List<LottieDynamicProperty<*>>) : this(
constructor(rescaleBitmaps: Boolean, properties: List<LottieDynamicProperty<*>>) : this(
rescaleBitmaps,
properties.filter { it.property is Int } as List<LottieDynamicProperty<Int>>,
properties.filter { it.property is PointF } as List<LottieDynamicProperty<PointF>>,
properties.filter { it.property is Float } as List<LottieDynamicProperty<Float>>,
Expand All @@ -114,6 +118,7 @@ class LottieDynamicProperties internal constructor(
)

internal fun addTo(drawable: LottieDrawable) {
drawable.rescaleBitmaps = rescaleBitmaps
intProperties.forEach { p ->
drawable.addValueCallback(p.keyPath, p.property, p.callback.toValueCallback())
}
Expand Down
20 changes: 20 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Expand Up @@ -893,6 +893,26 @@ public String getImageAssetsFolder() {
return lottieDrawable.getImageAssetsFolder();
}

/**
* When true, dynamically set bitmaps will be drawn at the size of the original bitmap.
* When false, dynamically set bitmaps will be drawn at 0,0 at the original bitmap but at whatever size the dynamic bitmap is.
*
* Defaults to false.
*/
public void setRescaleBitmaps(boolean rescaleBitmaps) {
lottieDrawable.setRescaleBitmaps(rescaleBitmaps);
}

/**
* When true, dynamically set bitmaps will be drawn at the size of the original bitmap.
* When false, dynamically set bitmaps will be drawn at 0,0 at the original bitmap but at whatever size the dynamic bitmap is.
*
* Defaults to false.
*/
public boolean getRescaleBitmaps() {
return lottieDrawable.getRescaleBitmaps();
}

/**
* Allows you to modify or clear a bitmap that was loaded for an image either automatically
* through {@link #setImageAssetsFolder(String)} or with an {@link ImageAssetDelegate}.
Expand Down
21 changes: 21 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Expand Up @@ -99,6 +99,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
@Nullable
TextDelegate textDelegate;
private boolean enableMergePaths;
private boolean rescaleBitmaps;
@Nullable
private CompositionLayer compositionLayer;
private int alpha = 255;
Expand Down Expand Up @@ -219,6 +220,26 @@ public String getImageAssetsFolder() {
return imageAssetsFolder;
}

/**
* When true, dynamically set bitmaps will be drawn at the size of the original bitmap.
* When false, dynamically set bitmaps will be drawn at 0,0 at the original bitmap but at whatever size the dynamic bitmap is.
*
* Defaults to false.
*/
public void setRescaleBitmaps(boolean rescaleBitmaps) {
this.rescaleBitmaps = rescaleBitmaps;
}

/**
* When true, dynamically set bitmaps will be drawn at the size of the original bitmap.
* When false, dynamically set bitmaps will be drawn at 0,0 at the original bitmap but at whatever size the dynamic bitmap is.
*
* Defaults to false.
*/
public boolean getRescaleBitmaps() {
return rescaleBitmaps;
}

/**
* Create a composition with {@link LottieCompositionFactory}
*
Expand Down
Expand Up @@ -48,7 +48,12 @@ public class ImageLayer extends BaseLayer {
canvas.save();
canvas.concat(parentMatrix);
src.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
dst.set(0, 0, (int) (lottieImageAsset.getWidth() * density), (int) (lottieImageAsset.getHeight() * density));
if (lottieDrawable.getRescaleBitmaps()) {
dst.set(0, 0, (int) (lottieImageAsset.getWidth() * density), (int) (lottieImageAsset.getHeight() * density));
} else {
dst.set(0, 0, (int) (bitmap.getWidth() * density), (int) (bitmap.getHeight() * density));
}

canvas.drawBitmap(bitmap, src, dst, paint);
canvas.restore();
}
Expand All @@ -65,8 +70,9 @@ public class ImageLayer extends BaseLayer {
private Bitmap getBitmap() {
if (imageAnimation != null) {
Bitmap callbackBitmap = imageAnimation.getValue();
if (callbackBitmap != null)
if (callbackBitmap != null) {
return callbackBitmap;
}
}
String refId = layerModel.getRefId();
return lottieDrawable.getBitmapForId(refId);
Expand Down
Expand Up @@ -33,6 +33,8 @@ class FilmStripView @JvmOverloads constructor(

fun setImageAssetDelegate(delegate: ImageAssetDelegate?) {
animationViews.forEach { it.setImageAssetDelegate(delegate) }
// Enable bitmap rescaling for the first 4 views so both APIs get test coverage.
animationViews.forEachIndexed { i, av -> av.rescaleBitmaps = i <= 4 }
}

fun setFontAssetDelegate(delegate: FontAssetDelegate?) {
Expand Down

0 comments on commit 8e13d96

Please sign in to comment.