Играть в GIF в классе пользовательского просмотра - PullRequest
0 голосов
/ 18 января 2019

У меня есть собственный класс, который расширяет класс View. Как я могу нарисовать GIF позади других вещей, которые я рисую Canvas в методе onDraw?

Есть похожий вопрос, но Movie класс устарел:

Как играть в GIF на андроид

Ответы [ 2 ]

0 голосов
/ 18 января 2019

Попробуйте, загрузив GIF , используя Glide в вашем onDraw() методе:

Редактировать: на основе обсуждения с @ filipst о загрузке его на холст, добавлении кода в onResourceReady() метод

@Override
protected void onDraw(Canvas canvas) {
    ...
    Glide.with(this.getContext())  // 'this' here is your custom view reference 
        .asGif() // We will define this to tell Glide about it's GIF format to load explicitly
        .load(R.raw.gif_test) // or even put it into drawable R.drawable.git_test
        .into(new SimpleTarget<GifDrawable>() { 
            @Override 
            public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) { 
                resource.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); // Import to set bounds of canvas to load resource otherwise won't load
                resource.draw(canvas); 
                resource.start();
                //or 
                resource.startFromFirstFrame();
            } 
    });
    ...
}
0 голосов
/ 18 января 2019

вы можете использовать Лотти здесь Библиотека

Из XML

<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:lottie_rawRes="@raw/hello_world"
        // or
        app:lottie_fileName="hello_world.json"

        // Loop indefinitely
        app:lottie_loop="true"
        // Start playing as soon as the animation is loaded
        app:lottie_autoPlay="true" />

Программный

@BindView(R.id.animation_view)
LottieAnimationView animation_view;

 animation_view.setImageAssetsFolder("images/");
        animation_view.setAnimation(R.raw.lightning_animation);
        animation_view.useHardwareAcceleration(true);
        animation_view.enableMergePathsForKitKatAndAbove(true);
        animation_view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        animation_view.playAnimation();

Это легкая и простая библиотека для использования анимации, как GIF.

...