Makalele
Makalele

Reputation: 7521

Run alpha animation on Video View android

I've successfully added VideoView on my layout and I'm able to play it too. I have a nice animating logo on top of videoview, when certain button is clicked. In the same time I want to fade out the video. But running alpha animation on it immedietly turn it black. I found that videoview is not behaving like an ordinary view, because it's surface view. I tried putting it inside frame layout, but it didn't work. Animation looks like this:

AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
alphaAnimation.setFillAfter(true);

videoView.suspend(); //pause video
videoView.startAnimation(alphaAnimation);

So how can I fade out video?

Upvotes: 2

Views: 5988

Answers (3)

Akhil
Akhil

Reputation: 329

If you are using videoView it is not suitable for running aplha animation. For this you can use Exoplayer.In Exoplayer use the surface_type attribute as texture_view in xml fle and run the animation.

Eg:

 <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:surface_type="texture_view"
            android:animateLayoutChanges="true"/>

And your Kotlin code like this

val shortAnimationDuration = 1000
            videoView.apply {
                alpha = 0f
                animate()
                    .alpha(1f)
                    .setDuration(shortAnimationDuration.toLong())
                    .setListener(object :AnimatorListenerAdapter(){
                        override fun onAnimationEnd(animation: Animator?) {
                           //You can add what you want to do after completing animation
                        }
                    })
            }

This is a Fade in animation. If you want Fade out you just swap the alpha values.

Upvotes: 3

maatik5
maatik5

Reputation: 277

You can make Fade Out on VideoView:

final int duration = 400;
final int colorFrom = Color.parseColor("#10000000");
final int colorTo = Color.parseColor("#000000");
ColorDrawable[] color = {new ColorDrawable(colorFrom), new ColorDrawable(colorTo)};
TransitionDrawable transition = new TransitionDrawable(color);
videoview.setBackground(transition);
transition.startTransition(duration);

Upvotes: 4

JiTHiN
JiTHiN

Reputation: 6588

You cannot animate VideoView. Why don't you try using TextureView, which behaves just like a normal View. You can find how to play video in TextureView from this answer in SO.

Upvotes: 4

Related Questions