3

I have an animated *.svg file. I cannot find a way to play it in my android activity. I also need to know when the loop is over. how to achieve this?

dhir
  • 187
  • 2
  • 15
  • 1
    Possible duplicate of [Android and playing SVG animation](https://stackoverflow.com/questions/9115696/android-and-playing-svg-animation) – Rahul Sharma Oct 16 '18 at 06:02

1 Answers1

3

You could use https://shapeshifter.design/ in order to create an AnimatedVectorDrawable from plain SVG (see: https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable)

After creating the drawable and including it to the Assets Folder you can load the drawable and assing it to an ImageView, which is defined in your xml Layout.

See the following code:

final AnimatedVectorDrawableCompat animatedVectorDrawableCompat =
            AnimatedVectorDrawableCompat.create(getApplicationContext(),
                    R.drawable.animatedvector);

    ImageView imageView = findViewById(R.id.imageView);
    imageView.setImageDrawable(animatedVectorDrawableCompat);

    final Animatable animatable = (Animatable) imageView.getDrawable();
    animatable.start();

R.drawable.animatedvector is the AnimatedVectorDrawable you want to load, while R.id.imageView is the ImageView, which should show the AnimatedVectorDrawable.

Hope this helps ;-)

Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
  • 1
    I got issue: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.isStateful()' on a null object reference at androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat.isStateful(AnimatedVectorDrawableCompat.java:397) – Nanda Z Jan 08 '20 at 04:01