Last active
July 15, 2016 08:12
-
-
Save josemigallas/1665a4dd98d5837ea68c463265126f89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CroppableTextureView extends TextureView implements TextureView.SurfaceTextureListener { | |
private final int videoRes; | |
private MediaPlayer player; | |
private int videoHeight; | |
private int videoWidth; | |
public CroppableTextureView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CroppableTextureView, 0, 0); | |
try { | |
videoRes = ta.getResourceId(R.styleable.CroppableTextureView_rawRes, 0); | |
} finally { | |
ta.recycle(); | |
} | |
init(); | |
} | |
private void init() { | |
if (videoRes == 0) { | |
throw new RuntimeException("Must specify a video raw resource"); | |
} | |
setSurfaceTextureListener(this); | |
} | |
private void calculateVideoSize() { | |
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever(); | |
AssetFileDescriptor fileDescriptor = getResources().openRawResourceFd(videoRes); | |
metaRetriever.setDataSource( | |
fileDescriptor.getFileDescriptor(), | |
fileDescriptor.getStartOffset(), | |
fileDescriptor.getLength() | |
); | |
String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); | |
String width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); | |
videoHeight = Integer.parseInt(height); | |
videoWidth = Integer.parseInt(width); | |
} | |
@Override | |
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { | |
calculateVideoSize(); | |
centerCrop(width, height, videoWidth, videoHeight); | |
player = MediaPlayer.create(getContext(), videoRes); | |
player.setSurface(new Surface(surfaceTexture)); | |
player.setLooping(true); | |
player.start(); | |
} | |
@Override | |
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { | |
} | |
@Override | |
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { | |
if (player != null) { | |
player.stop(); | |
player.release(); | |
player = null; | |
} | |
return true; | |
} | |
@Override | |
public void onSurfaceTextureUpdated(SurfaceTexture surface) { | |
} | |
public void centerCrop(int screenWidth, int screenHeight, int videoWidth, int videoHeight) { | |
Matrix matrix = new Matrix(); | |
float screenRatio = (float) screenWidth / screenHeight; | |
float videoRatio = (float) videoWidth / videoHeight; | |
if (screenRatio < videoRatio) { | |
float heightScale = (float) screenHeight / videoHeight; | |
float newWidth = videoWidth * heightScale; | |
float widthScale = newWidth / screenWidth; | |
float offsetX = -(newWidth - screenWidth) / 2; | |
getTransform(matrix); | |
matrix.setScale(widthScale, 1); | |
matrix.postTranslate(offsetX, 0); | |
} else { | |
float widthScale = (float) screenWidth / videoWidth; | |
float newHeight = videoHeight * widthScale; | |
float heightScale = newHeight / screenHeight; | |
float offsetY = -(newHeight - screenHeight) / 2; | |
getTransform(matrix); | |
matrix.setScale(1, heightScale); | |
matrix.postTranslate(0, offsetY); | |
} | |
setTransform(matrix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment