Java Code Examples for android.view.TextureView#setRotation()

The following examples show how to use android.view.TextureView#setRotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextureViewImplementation.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Corrects the preview to match the UI orientation and completely fill the PreviewView.
 *
 * <p>
 * The camera produces a preview that depends on its sensor orientation and that has a
 * specific resolution. In order to display it correctly, this preview must be rotated to
 * match the UI orientation, and must be scaled up/down to fit inside the view that's
 * displaying it. This method takes care of doing so while keeping the preview centered.
 * </p>
 *
 * @param container   The {@link PreviewView}'s root layout, which wraps the preview.
 * @param textureView The {@link android.view.TextureView} that displays the preview, its size
 *                    must match the camera sensor output size.
 * @param bufferSize  The camera sensor output size.
 */
private void correctPreviewForCenterCrop(@NonNull final View container,
        @NonNull final TextureView textureView, @NonNull final Size bufferSize) {
    // Scale TextureView to fill PreviewView while respecting sensor output size aspect ratio
    final Pair<Float, Float> scale = ScaleTypeTransform.getFillScaleWithBufferAspectRatio(container, textureView,
            bufferSize);
    textureView.setScaleX(scale.first);
    textureView.setScaleY(scale.second);

    // Center TextureView inside PreviewView
    final Point newOrigin = ScaleTypeTransform.getOriginOfCenteredView(container, textureView);
    textureView.setX(newOrigin.x);
    textureView.setY(newOrigin.y);

    // Rotate TextureView to correct preview orientation
    final int rotation = ScaleTypeTransform.getRotationDegrees(textureView);
    textureView.setRotation(-rotation);
}