7

I have to use lottie animation for splash screen in android. I have used match parent for width and height. But it is showing me some padding at the edges of the screen as in the attached image. If i use scaletype as fitXY then it image is stretched on some devices. How can I use lottie animation file for all device resolutions or do i need to ask our designer for changes in file?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/retention_bg_welcome_color">

<com.airbnb.lottie.LottieAnimationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    app:lottie_fileName="test_lottie.json"
    android:adjustViewBounds="true"
    app:lottie_loop="true"
    app:lottie_speed="0.8" />
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

TylerH
  • 20,799
  • 66
  • 75
  • 101
user2050075
  • 189
  • 3
  • 14
  • why did you put android:adjustViewBounds="true" ? – Lena Bru Sep 06 '20 at 06:48
  • please try on the lottie view instead of layout_width="match_parent" to put layout_width="0dp" and the same for layout_height. It should force the constraint layout to calculate the correct size – Lena Bru Sep 06 '20 at 06:50
  • @LenaBru It was like that earlier. Both height/width was 0dp. I changed these just to check. It was mentioned somewhere to add android:adjustViewBounds="true". Still padding at the edges – user2050075 Sep 06 '20 at 07:48
  • and you're positive the padding comes from the lottie view and not the parent of the lottie view? or the parent of the parent ? – Lena Bru Sep 06 '20 at 07:53
  • yeah i am sure as i tried it with putting some dummy view and added color to it. I am thinking if we need some changes in json file. Is single json file will support all screen resolutions? – user2050075 Sep 06 '20 at 08:00
  • could you possibly upload the lottie json? I will try it in my dev env, and see if there is anything can be done – Lena Bru Sep 07 '20 at 06:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221076/discussion-between-user2050075-and-lena-bru). – user2050075 Sep 07 '20 at 06:21

4 Answers4

13

android:scaleType="centerCrop" fixes the issue for me:

The full XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:gravity="center_horizontal">

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/animation_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:duplicateParentState="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    app:lottie_fileName="test.json"
    app:lottie_loop="false"
    android:scaleType="centerCrop"
    tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Carsten
  • 361
  • 3
  • 14
0
  /**
   * Disable the extraScale mode in {@link #draw(Canvas)} function when scaleType is FitXY. It doesn't affect the rendering with other scaleTypes.
   *
   * <p>When there are 2 animation layout side by side, the default extra scale mode might leave 1 pixel not drawn between 2 animation, and
   * disabling the extraScale mode can fix this problem</p>
   *
   * <b>Attention:</b> Disable the extra scale mode can downgrade the performance and may lead to larger memory footprint. Please only disable this
   * mode when using animation with a reasonable dimension (smaller than screen size).
   *
   * @see LottieDrawable#drawWithNewAspectRatio(Canvas)
   */
  public void disableExtraScaleModeInFitXY() {
    lottieDrawable.disableExtraScaleModeInFitXY();
  }
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
-1

use this in your activity

requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
-1

Add android:scaleType Property to com.airbnb.lottie.LottieAnimationView

<com.airbnb.lottie.LottieAnimationView
  android:id="@+id/lottieView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:scaleType="centerCrop" // fitXY or centerCrop will resolve the aspect issue
  app:lottie_autoPlay="true"
  app:lottie_loop="false"
  app:lottie_rawRes="@raw/splash" />
imKrishh
  • 762
  • 1
  • 6
  • 11