下面列出了怎么用android.view.TextureView的API类实例代码及写法,或者点击链接到github查看源代码。
public void init() {
this.mVideoWidth = 0;
this.mVideoHeight = 0;
setBackgroundColor(ViewCompat.MEASURED_STATE_MASK);
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
this.mCurrentState = 0;
this.mTargetState = 0;
TextureView renderUIView = new TextureView(getContext());
renderUIView.setLayoutParams(new LayoutParams(-2, -2, 17));
renderUIView.setSurfaceTextureListener(this.mSurfaceCallback);
this.mX8Camera9GridView = new X8Camera9GridView(getContext());
this.mX8Camera9GridView.setLayoutParams(new LayoutParams(-1, -1, 17));
this.mX8AiTrackContainterView = new X8AiTrackContainterView(getContext());
this.mX8AiTrackContainterView.setLayoutParams(new LayoutParams(-1, -1, 17));
this.blackView = new View(getContext());
this.blackView.setLayoutParams(new LayoutParams(-1, -1, 17));
this.blackView.setBackgroundColor(getContext().getResources().getColor(R.color.black));
addView(renderUIView);
addView(this.mX8AiTrackContainterView);
addView(this.blackView);
addView(this.mX8Camera9GridView);
showGridLine(GlobalConfig.getInstance().getGridLine());
}
private void initView() {
this.mPlayerView = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.x8_custom_video_view, this);
this.mPlayerView.setOnClickListener(this);
this.mLoadingBar = (ProgressBar) this.mPlayerView.findViewById(R.id.load_iv);
this.mVideoView = (TextureView) this.mPlayerView.findViewById(R.id.play_video_textureview);
this.mBtnPlayMax = (Button) this.mPlayerView.findViewById(R.id.btn_play_max);
this.mBtnPlayMax.setOnClickListener(this);
this.mVideoView.setOnClickListener(this);
this.mVideoView.setKeepScreenOn(true);
this.mVideoView.setSurfaceTextureListener(this);
this.mBottomPlayRl = (RelativeLayout) this.mPlayerView.findViewById(R.id.bottom_play_rl);
this.mMiniPlayBtn = (ImageButton) this.mBottomPlayRl.findViewById(R.id.play_btn);
this.mPlaySb = (SeekBar) this.mBottomPlayRl.findViewById(R.id.play_sb);
this.mPlaySb.setOnSeekBarChangeListener(this);
this.mMiniPlayBtn.setOnClickListener(this);
this.mCurrentTimeTv = (TextView) this.mBottomPlayRl.findViewById(R.id.time_current_tv);
this.mCurrentTimeTv.setText(setTimeFormatter(0));
this.mTotalTimeTv = (TextView) this.mBottomPlayRl.findViewById(R.id.total_time_tv);
this.mTotalTimeTv.setText(this.mTotalTime);
showBar(true);
this.mPlayerView.setOnClickListener(this);
LayoutParams params = new LayoutParams(this.mScreenWidth, this.mDestationHeight);
params.addRule(13);
this.mPlayerView.setLayoutParams(params);
FontUtil.changeFontLanTing(getResources().getAssets(), this.mTotalTimeTv, this.mCurrentTimeTv);
}
private static void applyTextureViewRotation(TextureView textureView, int textureViewRotation) {
float textureViewWidth = textureView.getWidth();
float textureViewHeight = textureView.getHeight();
if (textureViewWidth == 0 || textureViewHeight == 0 || textureViewRotation == 0) {
textureView.setTransform(null);
} else {
Matrix transformMatrix = new Matrix();
float pivotX = textureViewWidth / 2;
float pivotY = textureViewHeight / 2;
transformMatrix.postRotate(textureViewRotation, pivotX, pivotY);
// After rotation, scale the rotated texture to fit the TextureView size.
RectF originalTextureRect = new RectF(0, 0, textureViewWidth, textureViewHeight);
RectF rotatedTextureRect = new RectF();
transformMatrix.mapRect(rotatedTextureRect, originalTextureRect);
transformMatrix.postScale(
textureViewWidth / rotatedTextureRect.width(),
textureViewHeight / rotatedTextureRect.height(),
pivotX,
pivotY);
textureView.setTransform(transformMatrix);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_movie);
mTextureView = (TextureView) findViewById(R.id.movie_texture_view);
mTextureView.setSurfaceTextureListener(this);
// Populate file-selection spinner.
Spinner spinner = (Spinner) findViewById(R.id.playMovieFile_spinner);
// Need to create one of these fancy ArrayAdapter thingies, and specify the generic layout
// for the widget itself.
mMovieFiles = MiscUtils.getFiles(getFilesDir(), "*.mp4");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mMovieFiles);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner.
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
updateControls();
}
public void updateTextureViewPosition() {
View view = videoView.getAspectRatioView();
view.getLocationInWindow(position);
position[0] -= getLeftInset();
if (!videoView.isInline() && !animationInProgress) {
TextureView textureView = videoView.getTextureView();
textureView.setTranslationX(position[0]);
textureView.setTranslationY(position[1]);
View textureImageView = videoView.getTextureImageView();
if (textureImageView != null) {
textureImageView.setTranslationX(position[0]);
textureImageView.setTranslationY(position[1]);
}
}
View controlsView = videoView.getControlsView();
if (controlsView.getParent() == container) {
controlsView.setTranslationY(position[1]);
} else {
controlsView.setTranslationY(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_double_decode);
if (!sVideoRunning) {
sBlob[0] = new VideoBlob((TextureView) findViewById(R.id.double1_texture_view),
ContentManager.MOVIE_SLIDERS, 0);
sBlob[1] = new VideoBlob((TextureView) findViewById(R.id.double2_texture_view),
ContentManager.MOVIE_EIGHT_RECTS, 1);
sVideoRunning = true;
} else {
sBlob[0].recreateView((TextureView) findViewById(R.id.double1_texture_view));
sBlob[1].recreateView((TextureView) findViewById(R.id.double2_texture_view));
}
}
public void updateTextureViewPosition() {
View view = videoView.getAspectRatioView();
view.getLocationInWindow(position);
position[0] -= getLeftInset();
if (!videoView.isInline() && !animationInProgress) {
TextureView textureView = videoView.getTextureView();
textureView.setTranslationX(position[0]);
textureView.setTranslationY(position[1]);
View textureImageView = videoView.getTextureImageView();
if (textureImageView != null) {
textureImageView.setTranslationX(position[0]);
textureImageView.setTranslationY(position[1]);
}
}
View controlsView = videoView.getControlsView();
if (controlsView.getParent() == container) {
controlsView.setTranslationY(position[1]);
} else {
controlsView.setTranslationY(0);
}
}
public void updateTextureViewPosition() {
View view = videoView.getAspectRatioView();
view.getLocationInWindow(position);
position[0] -= getLeftInset();
if (!videoView.isInline() && !animationInProgress) {
TextureView textureView = videoView.getTextureView();
textureView.setTranslationX(position[0]);
textureView.setTranslationY(position[1]);
View textureImageView = videoView.getTextureImageView();
if (textureImageView != null) {
textureImageView.setTranslationX(position[0]);
textureImageView.setTranslationY(position[1]);
}
}
View controlsView = videoView.getControlsView();
if (controlsView.getParent() == container) {
controlsView.setTranslationY(position[1]);
} else {
controlsView.setTranslationY(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ffmpeg_preview);
cancelBtn = (Button) findViewById(R.id.play_cancel);
cancelBtn.setOnClickListener(this);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
surfaceView = (TextureView) findViewById(R.id.preview_video);
RelativeLayout preview_video_parent = (RelativeLayout)findViewById(R.id.preview_video_parent);
LayoutParams layoutParams = (LayoutParams) preview_video_parent
.getLayoutParams();
layoutParams.width = displaymetrics.widthPixels;
layoutParams.height = displaymetrics.widthPixels;
preview_video_parent.setLayoutParams(layoutParams);
surfaceView.setSurfaceTextureListener(this);
surfaceView.setOnClickListener(this);
path = getIntent().getStringExtra("path");
imagePlay = (ImageView) findViewById(R.id.previre_play);
imagePlay.setOnClickListener(this);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
public void setTextureView(TextureView textureView, AspectRatioFrameLayout aspectRatioFrameLayout, FrameLayout container, boolean set) {
if (textureView == null) {
return;
}
if (!set && currentTextureView == textureView) {
pipSwitchingState = 1;
currentTextureView = null;
currentAspectRatioFrameLayout = null;
currentTextureViewContainer = null;
return;
}
if (videoPlayer == null || textureView == currentTextureView) {
return;
}
isDrawingWasReady = aspectRatioFrameLayout != null && aspectRatioFrameLayout.isDrawingReady();
currentTextureView = textureView;
if (pipRoundVideoView != null) {
videoPlayer.setTextureView(pipRoundVideoView.getTextureView());
} else {
videoPlayer.setTextureView(currentTextureView);
}
currentAspectRatioFrameLayout = aspectRatioFrameLayout;
currentTextureViewContainer = container;
if (currentAspectRatioFrameLayoutReady && currentAspectRatioFrameLayout != null) {
if (currentAspectRatioFrameLayout != null) {
currentAspectRatioFrameLayout.setAspectRatio(currentAspectRatioFrameLayoutRatio, currentAspectRatioFrameLayoutRotation);
}
//if (currentTextureViewContainer.getVisibility() != View.VISIBLE) {
// currentTextureViewContainer.setVisibility(View.VISIBLE);
//}
}
}
private void initViews() {
mVideoView = (TextureView) findViewById(R.id.video_view);
mTimeView = (TextView) findViewById(R.id.video_time_view);
mProgressView = (SeekBar) findViewById(R.id.video_progress_view);
mControlBtn = (ImageButton) findViewById(R.id.video_control_btn);
mPlayTipView = (TextView) findViewById(R.id.play_tip_view);
mControlBtn.setOnClickListener(this);
mVideoView.setSurfaceTextureListener(mSurfaceTextureListener);
mProgressView.setOnSeekBarChangeListener(mSeekBarChangeListener);
}
@Override
public void clearVideoTextureView(TextureView textureView) {
verifyApplicationThread();
if (textureView != null && textureView == this.textureView) {
setVideoTextureView(null);
}
}
@Override
public void clearVideoTextureView(@Nullable TextureView textureView) {
verifyApplicationThread();
if (textureView != null && textureView == this.textureView) {
setVideoTextureView(null);
}
}
public void setTextureView(TextureView texture) {
if (textureView == texture) {
return;
}
textureView = texture;
if (player == null) {
return;
}
player.setVideoTextureView(textureView);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ffmpeg_preview);
cancelBtn = (Button) findViewById(R.id.play_cancel);
cancelBtn.setOnClickListener(this);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
surfaceView = (TextureView) findViewById(R.id.preview_video);
RelativeLayout previewVideoParent = (RelativeLayout)findViewById(R.id.preview_video_parent);
LayoutParams layoutParams = (LayoutParams) previewVideoParent
.getLayoutParams();
layoutParams.width = displaymetrics.widthPixels;
layoutParams.height = displaymetrics.widthPixels;
previewVideoParent.setLayoutParams(layoutParams);
surfaceView.setSurfaceTextureListener(this);
surfaceView.setOnClickListener(this);
path = getIntent().getStringExtra("path");
imagePlay = (ImageView) findViewById(R.id.previre_play);
imagePlay.setOnClickListener(this);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
private void setView(int id, TextureView view) {
ensureInitState();
if (view == null)
throw new NullPointerException("view is null");
final SurfaceHelper surfaceHelper = mSurfaceHelpers[id];
if (surfaceHelper != null)
surfaceHelper.release();
mSurfaceHelpers[id] = new SurfaceHelper(id, view);
}
@Override
public void clearVideoTextureView(TextureView textureView) {
verifyApplicationThread();
if (textureView != null && textureView == this.textureView) {
setVideoTextureView(null);
}
}
public void setPlayer(SimpleExoPlayer player) {
if (this.player == player) {
return;
}
if (this.player != null) {
this.player.removeListener(componentListener);
this.player.removeTextOutput(componentListener);
this.player.removeVideoListener(componentListener);
if (surfaceView instanceof TextureView) {
this.player.clearVideoTextureView((TextureView) surfaceView);
} else if (surfaceView instanceof SurfaceView) {
this.player.clearVideoSurfaceView((SurfaceView) surfaceView);
}
}
this.player = player;
if (useController) {
controller.setPlayer(player);
}
if (shutterView != null) {
shutterView.setVisibility(VISIBLE);
}
if (player != null) {
if (surfaceView instanceof TextureView) {
player.setVideoTextureView((TextureView) surfaceView);
} else if (surfaceView instanceof SurfaceView) {
player.setVideoSurfaceView((SurfaceView) surfaceView);
}
player.addVideoListener(componentListener);
player.addTextOutput(componentListener);
player.addListener(componentListener);
maybeShowController(false);
} else {
shutterView.setVisibility(VISIBLE);
controller.hide();
}
}
public void setupSurfaceTexureListener() {
if (mSurfaceTextureListener != null)
return;
mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
int width, int height) {
Log.d(TAG, "onSurfaceTextureAvailable() about to open the camera with width,height "
+ String.valueOf(width) + "," + String.valueOf(height));
mCamera.openCamera(width, height);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
int width, int height) {
Log.d(TAG, "onSurfaceTextureSizeChanged() width width,height "
+ String.valueOf(width) + "," + String.valueOf(height));
mCamera.configureTransform(width, height);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
Log.d(TAG, "onSurfaceTextureDestroyed() ");
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
Log.d(TAG, "onSurfaceTextureUpdated() ");
}
};
}
public boolean switchCameraInternal() {
if (camera == null) {
return false;
}
try {
camera.setPreviewCallback(null);
camera.release();
RxCameraConfig.Builder builder = new RxCameraConfig.Builder();
builder.from(getConfig());
if (getConfig().isFaceCamera) {
builder.useBackCamera();
} else {
builder.useFrontCamera();
}
this.cameraConfig = builder.build();
if (bindSurfaceView != null) {
SurfaceView oldSurfaceView = bindSurfaceView;
openCameraInternal();
bindSurfaceInternal(oldSurfaceView);
} else if (bindTextureView != null) {
TextureView oldTextureView = bindTextureView;
openCameraInternal();
bindTextureInternal(oldTextureView);
}
return startPreviewInternal();
} catch (Exception e) {
Log.e(TAG, "switchCamera error: " + e.getMessage());
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fold);
mImageView = (ImageView)findViewById(R.id.image_view);
mImageView.setPadding(ANTIALIAS_PADDING, ANTIALIAS_PADDING, ANTIALIAS_PADDING,
ANTIALIAS_PADDING);
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.image));
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
mAnchorSeekBar = (SeekBar)findViewById(R.id.anchor_seek_bar);
mFoldLayout = (FoldingLayout)findViewById(R.id.fold_view);
mFoldLayout.setBackgroundColor(Color.BLACK);
mFoldLayout.setFoldListener(mOnFoldListener);
mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
mAnchorSeekBar.setOnSeekBarChangeListener(mSeekBarChangeListener);
mScrollGestureDetector = new GestureDetector(this, new ScrollGestureDetector());
mItemSelectedListener = new ItemSelectedListener();
mDefaultPaint = new Paint();
mSepiaPaint = new Paint();
ColorMatrix m1 = new ColorMatrix();
ColorMatrix m2 = new ColorMatrix();
m1.setSaturation(0);
m2.setScale(1f, .95f, .82f, 1.0f);
m1.setConcat(m2, m1);
mSepiaPaint.setColorFilter(new ColorMatrixColorFilter(m1));
}
public static void waitForNUpdates(final TextureView textureView, int count) {
TextureView.SurfaceTextureListener previousListener = textureView.getSurfaceTextureListener();
final TextureViewAsserter textureViewAsserter = new TextureViewAsserter(previousListener);
textureView.setSurfaceTextureListener(textureViewAsserter);
TestUtils.synchronous().latchCount(count).timeout(15).run(new TestUtils.SynchronousBlock() {
@Override
public void run(final CountDownLatch latch) {
textureViewAsserter.waitForUpdates(latch);
}
});
textureView.setSurfaceTextureListener(previousListener);
}
/**
* get real width and height video
*/
private void getRealSize(MediaPlayer mp, TextureView mTextureView) {
if (mp == null || mTextureView == null) {
return;
}
//Get the dimensions of the video
int videoWidth = mp.getVideoWidth();
int videoHeight = mp.getVideoHeight();
Display display = G.fragmentActivity.getWindowManager().getDefaultDisplay();
int finalWith, finalHeight;
finalWith = display.getWidth();
finalHeight = (int) (((float) videoHeight / (float) videoWidth) * (float) display.getWidth());
if (finalHeight > display.getHeight()) {
finalWith = (int) (((float) finalWith / (float) finalHeight) * (float) display.getHeight());
finalHeight = display.getHeight();
}
ViewGroup.LayoutParams lp = mTextureView.getLayoutParams();
lp.width = finalWith;
lp.height = finalHeight;
mTextureView.setLayoutParams(lp);
}
private void init() {
inflate(getContext(), R.layout.view_playback, this);
setBackgroundColor(Color.BLACK);
mAnalytics = Fa.get();
TextureView mVideoTextureView = findViewById(R.id.video);
mVideoTextureView.setSurfaceTextureListener(this);
findViewById(R.id.close_button).setOnClickListener(this);
findViewById(R.id.layout_share).setOnClickListener(this);
findViewById(R.id.layout_save).setOnClickListener(this);
// set margin of bottom icons to be appropriate size for screen
View saveLayout = findViewById(R.id.layout_save);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) saveLayout
.getLayoutParams();
layoutParams.bottomMargin = getHeightOfNavigationBar();
saveLayout.setLayoutParams(layoutParams);
View shareLayout = findViewById(R.id.layout_share);
layoutParams = (ConstraintLayout.LayoutParams) shareLayout.getLayoutParams();
layoutParams.bottomMargin = getHeightOfNavigationBar();
shareLayout.setLayoutParams(layoutParams);
mAudioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
.build();
}
CameraImplV2(Activity context, TextureView prevView, boolean cameraFallback) throws CameraException {
super(context, prevView);
mCamManager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
if (mCamManager == null) {
throw new CameraException(mActivity.getString(R.string.couldNotObtainCameraService));
}
try {
findCameraFacing(CameraCharacteristics.LENS_FACING_FRONT);
if (mCameraId == null && cameraFallback) {
findCameraFacing(CameraCharacteristics.LENS_FACING_BACK);
}
} catch (CameraAccessException e) {
throw new CameraException(e);
}
if (mCameraId == null) {
if (cameraFallback) {
throw new CameraException(mActivity.getString(R.string.cameraMissing));
} else {
throw new CameraException(mActivity.getString(R.string.frontCameraMissing));
}
}
mPreviewThread = new HandlerThread("previewThread");
mPreviewThread.start();
mPreviewHandler = new Handler(mPreviewThread.getLooper());
}
/**
* bind a {@link TextureView} as the camera preview surface
* @param textureView
* @return
*/
public Observable<RxCamera> bindTexture(final TextureView textureView) {
return Observable.create(new Observable.OnSubscribe<RxCamera>() {
@Override
public void call(Subscriber<? super RxCamera> subscriber) {
boolean result = cameraInternal.bindTextureInternal(textureView);
if (result) {
subscriber.onNext(RxCamera.this);
subscriber.onCompleted();
} else {
subscriber.onError(cameraInternal.bindSurfaceFailedException());
}
}
});
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextureView = new TextureView(this);
setContentView(mTextureView);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = km.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
/**
* 开始播放
*
* @param request 播放的请求
*/
public void start(PlayRequest request) {
if (mCurrentState != STATE_IDLE) {
log("start on wrong state: " + mCurrentState);
return;
}
if (request.videoSource == null) {
log("null source is not allowed!");
return;
}
mTextureView = new TextureView(getContext());
// Instantiate and add TextureView for rendering
final LayoutParams textureLp =
new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
textureLp.gravity = Gravity.CENTER;
// 放在z轴最下面,防止盖住控制view
addView(mTextureView, 0, textureLp);
mTextureView.setSurfaceTextureListener(this);
log("Add texture view");
mSource = request.videoSource;
mInitialPosition = request.startPos;
mLoop = request.loop;
mLeftVolume = request.leftVolume;
mRightVolume = request.rightVolume;
prepare();
}
public void setTextureView(TextureView texture) {
if (textureView == texture) {
return;
}
textureView = texture;
if (player == null) {
return;
}
player.setVideoTextureView(textureView);
}
@Override
public void setVideoTextureView(TextureView textureView) {
if (this.textureView == textureView) {
return;
}
removeSurfaceCallbacks();
this.textureView = textureView;
needSetSurface = true;
if (textureView == null) {
setVideoSurfaceInternal(null, true);
maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
} else {
if (textureView.getSurfaceTextureListener() != null) {
Log.w(TAG, "Replacing existing SurfaceTextureListener.");
}
textureView.setSurfaceTextureListener(componentListener);
SurfaceTexture surfaceTexture = textureView.isAvailable() ? textureView.getSurfaceTexture()
: null;
if (surfaceTexture == null) {
setVideoSurfaceInternal(/* surface= */ null, /* ownsSurface= */ true);
maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
} else {
setVideoSurfaceInternal(new Surface(surfaceTexture), /* ownsSurface= */ true);
maybeNotifySurfaceSizeChanged(textureView.getWidth(), textureView.getHeight());
}
}
}