生活随笔
收集整理的這篇文章主要介紹了
CenterCrop的Video View
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先,原生的VideoView是繼承SurfaceView加上媒體控制器,不能直接處理。而對(duì)TextureView centerCrop的原理很簡單,如下:
在onVideoSizeChanged方法
if (videoWidth
== 0 || videoHeight
== 0) {return;}float sx
= (float) getWidth() / (float) videoWidth
;float sy
= (float) getHeight() / (float) videoHeight
;float maxScale
= Math
.max(sx
, sy
);sx
= maxScale
/ sx
;sy
= maxScale
/ sy
;Matrix matrix
= new Matrix();matrix
.setScale(sx
, sy
, getWidth() / 2f, getHeight() / 2f);if (matrix
!= null
) {setTransform(matrix
);}
更多的類型可以參考
https://github.com/codeguyFred/Android-ScalableVideoView
在自定義View的入口加上setSurfaceTextureListener監(jiān)聽
但是有些手機(jī)onSurfaceTextureAvailable回調(diào)會(huì)不夠及時(shí)。
如果mMediaPlayer.setSurface(mSurface);中的mSurface是null的你會(huì)出現(xiàn)黑屏的情況。
完整代碼如下
public class ScaleVideoView extends TextureView implements TextureView.SurfaceTextureListener,MediaPlayer
.OnVideoSizeChangedListener
{protected MediaPlayer mMediaPlayer
;private Surface mSurface
= null
;public ScaleVideoView(Context context
) {this(context
, null
);}public ScaleVideoView(Context context
, AttributeSet attrs
) {this(context
, attrs
, 0);}public ScaleVideoView(Context context
, AttributeSet attrs
, int defStyle
) {super(context
, attrs
, defStyle
);setSurfaceTextureListener(this);}@Overridepublic void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture
, int width
, int height
) {mSurface
= new Surface(surfaceTexture
);if (mMediaPlayer
!= null
) {mMediaPlayer
.setSurface(mSurface
);}}@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surface
, int width
, int height
) {}@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface
) {if (mSurface
!= null
) {mSurface
.release();mSurface
= null
;}return true;}@Overridepublic void onSurfaceTextureUpdated(SurfaceTexture surface
) {}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (mMediaPlayer
== null
) {return;}if (isPlaying()) {stop();}release();}@Overridepublic void onVideoSizeChanged(MediaPlayer mp
, int width
, int height
) {scaleVideoSize(width
, height
);}private void scaleVideoSize(int videoWidth
, int videoHeight
) {if (videoWidth
== 0 || videoHeight
== 0) {return;}float sx
= (float) getWidth() / (float) videoWidth
;float sy
= (float) getHeight() / (float) videoHeight
;float maxScale
= Math
.max(sx
, sy
);sx
= maxScale
/ sx
;sy
= maxScale
/ sy
;Matrix matrix
= new Matrix();matrix
.setScale(sx
, sy
, getWidth() / 2f, getHeight() / 2f);if (matrix
!= null
) {setTransform(matrix
);}}private void initializeMediaPlayer() {if (mMediaPlayer
== null
) {mMediaPlayer
= new MediaPlayer();} else {reset();}mMediaPlayer
.setOnVideoSizeChangedListener(this);mMediaPlayer
.setSurface(mSurface
);}public void setRawData(@RawRes int id
) throws IOException
{AssetFileDescriptor afd
= getResources().openRawResourceFd(id
);setDataSource(afd
);}public void setAssetData(@NonNull String assetName
) throws IOException
{AssetManager manager
= getContext().getAssets();AssetFileDescriptor afd
= manager
.openFd(assetName
);setDataSource(afd
);}private void setDataSource(@NonNull AssetFileDescriptor afd
) throws IOException
{setDataSource(afd
.getFileDescriptor(), afd
.getStartOffset(), afd
.getLength());afd
.close();}public void setDataSource(@NonNull String path
) throws IOException
{initializeMediaPlayer();mMediaPlayer
.setDataSource(path
);}public void setDataSource(@NonNull Context context
, @NonNull Uri uri
,@Nullable Map
<String, String> headers
) throws IOException
{initializeMediaPlayer();mMediaPlayer
.setDataSource(context
, uri
, headers
);}public void setDataSource(@NonNull Context context
, @NonNull Uri uri
) throws IOException
{initializeMediaPlayer();mMediaPlayer
.setDataSource(context
, uri
);}public void setDataSource(@NonNull FileDescriptor fd
, long offset
, long length
)throws IOException
{initializeMediaPlayer();mMediaPlayer
.setDataSource(fd
, offset
, length
);}public void setDataSource(@NonNull FileDescriptor fd
) throws IOException
{initializeMediaPlayer();mMediaPlayer
.setDataSource(fd
);}public void prepare(@Nullable MediaPlayer
.OnPreparedListener listener
)throws IOException
, IllegalStateException
{mMediaPlayer
.setOnPreparedListener(listener
);mMediaPlayer
.prepare();}public void prepareAsync(@Nullable MediaPlayer
.OnPreparedListener listener
)throws IllegalStateException
{mMediaPlayer
.setOnPreparedListener(listener
);mMediaPlayer
.prepareAsync();}public void prepare() throws IOException
, IllegalStateException
{prepare(null
);}public void prepareAsync() throws IllegalStateException
{prepareAsync(null
);}public void setOnErrorListener(@Nullable MediaPlayer
.OnErrorListener listener
) {mMediaPlayer
.setOnErrorListener(listener
);}public void setOnCompletionListener(@Nullable MediaPlayer
.OnCompletionListener listener
) {mMediaPlayer
.setOnCompletionListener(listener
);}public void setOnInfoListener(@Nullable MediaPlayer
.OnInfoListener listener
) {mMediaPlayer
.setOnInfoListener(listener
);}public int getCurrentPosition() {return mMediaPlayer
.getCurrentPosition();}public int getDuration() {return mMediaPlayer
.getDuration();}public int getVideoHeight() {return mMediaPlayer
.getVideoHeight();}public int getVideoWidth() {return mMediaPlayer
.getVideoWidth();}public boolean isLooping() {return mMediaPlayer
.isLooping();}public void setLooping(boolean looping
) {mMediaPlayer
.setLooping(looping
);}public boolean isPlaying() {return mMediaPlayer
.isPlaying();}public void pause() {mMediaPlayer
.pause();}public void seekTo(int msec
) {mMediaPlayer
.seekTo(msec
);}public void setVolume(float leftVolume
, float rightVolume
) {mMediaPlayer
.setVolume(leftVolume
, rightVolume
);}public void start() {if (mMediaPlayer
!= null
) {mMediaPlayer
.start();}}public void stop() {if (mMediaPlayer
!= null
) {mMediaPlayer
.stop();}}public void reset() {if (mMediaPlayer
!= null
) {mMediaPlayer
.reset();}}public void release() {if (mMediaPlayer
!= null
) {reset();mMediaPlayer
.release();mMediaPlayer
= null
;}}
}
使用方法
vBgVideo
.post(() -> playBgVideo());
private void playBgVideo() {try {vBgVideo
.setRawData(R
.raw
.login_bg_video
);vBgVideo
.setVolume(0, 0);vBgVideo
.setLooping(true);vBgVideo
.prepare(mp
-> vBgVideo
.start());} catch (IOException e
) {}}
總結(jié)
以上是生活随笔為你收集整理的CenterCrop的Video View的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。