How to animate TextureView?

2019-09-19 03:00发布

问题:

I am trying to fade in a TextureView but for some reason its not animating. It just simply pops in the video, no fade at all and i dont really know why that is because after some research i have found that TextureView can be animated normally.

Here is my code, i hope you guys can give me a pointer in the right direction.

PS, i have left out all irrelevant code that does not concern itself with the textureview and the animation.

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener
{
    private static final String TAG = MainActivity.class.getSimpleName();

    private MediaPlayer mediaPlayer1;
    private ArrayList<Uri> videoUris = new ArrayList<>();
    private int current_video_index = 0;

    private TextureView textureView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textureView = (TextureView) findViewById(R.id.textureView);

        mediaPlayer1 = new MediaPlayer();
        mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer1.setOnPreparedListener(this);
        mediaPlayer1.setOnCompletionListener(this);

        initVideoUris();
        initNewVideo();
    }

    private void startVideo(final Uri uri)
    {
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener()
        {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
            {
                try {
                    mediaPlayer1.setDataSource(MainActivity.this, uri);
                    mediaPlayer1.setSurface(new Surface(surface));
                    mediaPlayer1.setOnCompletionListener(MainActivity.this);
                    mediaPlayer1.setOnPreparedListener(MainActivity.this);
                    mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);

                    mediaPlayer1.prepareAsync();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
            {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
            {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface)
            {

            }
        });
    }

    @Override
    public void onPrepared(MediaPlayer mp)
    {
        mp.start();
        fadeInView(textureView);
    }

    @Override
    public void onCompletion(MediaPlayer mp)
    {
        if (!moreVideosAvailable())
        {
            finish();
        }
    }

    private boolean moreVideosAvailable()
    {
        return current_video_index < videoUris.size();
    }

    private void fadeInView(View view)
    {
        view.setAlpha(0f);
        view.setVisibility(View.VISIBLE);
        view.animate().alpha(1f).setDuration(2000).setListener(null).start();
    }
}

回答1:

I solved it by making a videoPlayerFragment that uses TextureView as the surface for displaying the video. Then simply animate the whole fragment instead of the textureView.