android.util.Rational Java Examples

The following examples show how to use android.util.Rational. 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 Project: journaldev   Author: journaldev   File: MainActivity.java    License: MIT License 7 votes vote down vote up
private Preview setPreview() {

        Rational aspectRatio = new Rational(textureView.getWidth(), textureView.getHeight());
        Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen


        PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();
        Preview preview = new Preview(pConfig);

        preview.setOnPreviewOutputUpdateListener(
                new Preview.OnPreviewOutputUpdateListener() {
                    @Override
                    public void onUpdated(Preview.PreviewOutput output) {
                        ViewGroup parent = (ViewGroup) textureView.getParent();
                        parent.removeView(textureView);
                        parent.addView(textureView, 0);

                        textureView.setSurfaceTexture(output.getSurfaceTexture());
                        updateTransform();
                    }
                });

        return preview;
    }
 
Example #2
Source Project: android_9.0.0_r45   Author: lulululbj   File: ColorSpaceTransform.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a new immutable {@link ColorSpaceTransform} instance from a {@link Rational} array.
 *
 * <p>The elements must be stored in a row-major order.</p>
 *
 * @param elements An array of {@code 9} elements
 *
 * @throws IllegalArgumentException
 *            if the count of {@code elements} is not {@code 9}
 * @throws NullPointerException
 *            if {@code elements} or any sub-element is {@code null}
 */
public ColorSpaceTransform(Rational[] elements) {

    checkNotNull(elements, "elements must not be null");
    if (elements.length != COUNT) {
        throw new IllegalArgumentException("elements must be " + COUNT + " length");
    }

    mElements = new int[COUNT_INT];

    for (int i = 0; i < elements.length; ++i) {
        checkNotNull(elements, "element[" + i + "] must not be null");
        mElements[i * RATIONAL_SIZE + OFFSET_NUMERATOR] = elements[i].getNumerator();
        mElements[i * RATIONAL_SIZE + OFFSET_DENOMINATOR] = elements[i].getDenominator();
    }
}
 
Example #3
Source Project: android_9.0.0_r45   Author: lulululbj   File: ColorSpaceTransform.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check if this {@link ColorSpaceTransform} is equal to another {@link ColorSpaceTransform}.
 *
 * <p>Two color space transforms are equal if and only if all of their elements are
 * {@link Object#equals equal}.</p>
 *
 * @return {@code true} if the objects were equal, {@code false} otherwise
 */
@Override
public boolean equals(final Object obj) {
    if (obj == null) {
        return false;
    }
    if (this == obj) {
        return true;
    }
    if (obj instanceof ColorSpaceTransform) {
        final ColorSpaceTransform other = (ColorSpaceTransform) obj;
        for (int i = 0, j = 0; i < COUNT; ++i, j += RATIONAL_SIZE) {
            int numerator = mElements[j + OFFSET_NUMERATOR];
            int denominator = mElements[j + OFFSET_DENOMINATOR];
            int numeratorOther = other.mElements[j + OFFSET_NUMERATOR];
            int denominatorOther = other.mElements[j + OFFSET_DENOMINATOR];
            Rational r = new Rational(numerator, denominator);
            Rational rOther = new Rational(numeratorOther, denominatorOther);
            if (!r.equals(rOther)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
 
Example #4
Source Project: android_9.0.0_r45   Author: lulululbj   File: MarshalHelpers.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether or not {@code klass} is one of the metadata-primitive classes.
 *
 * <p>The following types (whether boxed or unboxed) are considered primitive:
 * <ul>
 * <li>byte
 * <li>int
 * <li>float
 * <li>double
 * <li>Rational
 * </ul>
 * </p>
 *
 * <p>This doesn't strictly follow the java understanding of primitive since
 * boxed objects are included, Rational is included, and other types such as char and
 * short are not included.</p>
 *
 * @param klass a {@link Class} instance; using {@code null} will return {@code false}
 * @return {@code true} if primitive, {@code false} otherwise
 */
public static <T> boolean isPrimitiveClass(Class<T> klass) {
    if (klass == null) {
        return false;
    }

    if (klass == byte.class || klass == Byte.class) {
        return true;
    } else if (klass == int.class || klass == Integer.class) {
        return true;
    } else if (klass == float.class || klass == Float.class) {
        return true;
    } else if (klass == long.class || klass == Long.class) {
        return true;
    } else if (klass == double.class || klass == Double.class) {
        return true;
    } else if (klass == Rational.class) {
        return true;
    }

    return false;
}
 
Example #5
Source Project: android_9.0.0_r45   Author: lulululbj   File: MarshalQueryablePrimitive.java    License: Apache License 2.0 6 votes vote down vote up
private Object unmarshalObject(ByteBuffer buffer) {
    switch (mNativeType) {
        case TYPE_INT32:
            return buffer.getInt();
        case TYPE_FLOAT:
            return buffer.getFloat();
        case TYPE_INT64:
            return buffer.getLong();
        case TYPE_RATIONAL:
            int numerator = buffer.getInt();
            int denominator = buffer.getInt();
            return new Rational(numerator, denominator);
        case TYPE_DOUBLE:
            return buffer.getDouble();
        case TYPE_BYTE:
            return buffer.get(); // getByte
        default:
            throw new UnsupportedOperationException(
                    "Can't unmarshal native type " + mNativeType);
    }
}
 
Example #6
Source Project: android_9.0.0_r45   Author: lulululbj   File: MarshalQueryablePrimitive.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean isTypeMappingSupported(TypeReference<T> managedType, int nativeType) {
    if (managedType.getType() instanceof Class<?>) {
        Class<?> klass = (Class<?>)managedType.getType();

        if (klass == byte.class || klass == Byte.class) {
            return nativeType == TYPE_BYTE;
        } else if (klass == int.class || klass == Integer.class) {
            return nativeType == TYPE_INT32;
        } else if (klass == float.class || klass == Float.class) {
            return nativeType == TYPE_FLOAT;
        } else if (klass == long.class || klass == Long.class) {
            return nativeType == TYPE_INT64;
        } else if (klass == double.class || klass == Double.class) {
            return nativeType == TYPE_DOUBLE;
        } else if (klass == Rational.class) {
            return nativeType == TYPE_RATIONAL;
        }
    }
    return false;
}
 
Example #7
Source Project: media-samples   Author: android   File: MediaSessionPlaybackActivity.java    License: Apache License 2.0 5 votes vote down vote up
/** Enters Picture-in-Picture mode. */
void minimize() {
    if (mMovieView == null) {
        return;
    }
    // Hide the controls in picture-in-picture mode.
    mMovieView.hideControls();
    // Calculate the aspect ratio of the PiP screen.
    Rational aspectRatio = new Rational(mMovieView.getWidth(), mMovieView.getHeight());
    mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
    enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
 
Example #8
Source Project: media-samples   Author: android   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
/** Enters Picture-in-Picture mode. */
void minimize() {
    if (mMovieView == null) {
        return;
    }
    // Hide the controls in picture-in-picture mode.
    mMovieView.hideControls();
    // Calculate the aspect ratio of the PiP screen.
    Rational aspectRatio = new Rational(mMovieView.getWidth(), mMovieView.getHeight());
    mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
    enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
 
Example #9
Source Project: mollyim-android   Author: mollyim   File: CameraXUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(21)
public static @NonNull Size buildResolutionForRatio(int longDimension, @NonNull Rational ratio, boolean isPortrait) {
  int shortDimension = longDimension * ratio.getDenominator() / ratio.getNumerator();

  if (isPortrait) {
    return new Size(shortDimension, longDimension);
  } else {
    return new Size(longDimension, shortDimension);
  }
}
 
Example #10
Source Project: mollyim-android   Author: mollyim   File: CameraXModule.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void updateViewInfo() {
  if (mImageCapture != null) {
    mImageCapture.setCropAspectRatio(new Rational(getWidth(), getHeight()));
    mImageCapture.setTargetRotation(getDisplaySurfaceRotation());
  }

  if (mVideoCapture != null && MediaConstraints.isVideoTranscodeAvailable()) {
    mVideoCapture.setTargetRotation(getDisplaySurfaceRotation());
  }
}
 
Example #11
Source Project: mollyim-android   Author: mollyim   File: WebRtcCallActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onUserLeaveHint() {
  if (deviceSupportsPipMode()) {
    PictureInPictureParams params = new PictureInPictureParams.Builder()
                                                              .setAspectRatio(new Rational(16, 9))
                                                              .build();
    setPictureInPictureParams(params);

    //noinspection deprecation
    enterPictureInPictureMode();
  }
}
 
Example #12
Source Project: GotoBrowser   Author: antest1   File: BrowserActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onUserLeaveHint() {
    boolean pipEnabled = sharedPref.getBoolean(PREF_PIP_MODE, false);
    if (supportsPiPMode() && pipEnabled) {
        PictureInPictureParams params = new PictureInPictureParams.Builder()
                .setAspectRatio(new Rational(1200, 720)).build();
        enterPictureInPictureMode(params);
    }
}
 
Example #13
Source Project: android_9.0.0_r45   Author: lulululbj   File: PictureInPictureParams.java    License: Apache License 2.0 5 votes vote down vote up
/** {@hide} */
PictureInPictureParams(Parcel in) {
    if (in.readInt() != 0) {
        mAspectRatio = new Rational(in.readInt(), in.readInt());
    }
    if (in.readInt() != 0) {
        mUserActions = new ArrayList<>();
        in.readParcelableList(mUserActions, RemoteAction.class.getClassLoader());
    }
    if (in.readInt() != 0) {
        mSourceRectHint = Rect.CREATOR.createFromParcel(in);
    }
}
 
Example #14
Source Project: android_9.0.0_r45   Author: lulululbj   File: PictureInPictureParams.java    License: Apache License 2.0 5 votes vote down vote up
/** {@hide} */
PictureInPictureParams(Rational aspectRatio, List<RemoteAction> actions,
        Rect sourceRectHint) {
    mAspectRatio = aspectRatio;
    mUserActions = actions;
    mSourceRectHint = sourceRectHint;
}
 
Example #15
Source Project: android_9.0.0_r45   Author: lulululbj   File: PictureInPictureArgs.java    License: Apache License 2.0 5 votes vote down vote up
private PictureInPictureArgs(Parcel in) {
    if (in.readInt() != 0) {
        mAspectRatio = new Rational(in.readInt(), in.readInt());
    }
    if (in.readInt() != 0) {
        mUserActions = new ArrayList<>();
        in.readParcelableList(mUserActions, RemoteAction.class.getClassLoader());
    }
    if (in.readInt() != 0) {
        mSourceRectHint = Rect.CREATOR.createFromParcel(in);
    }
}
 
Example #16
Source Project: android_9.0.0_r45   Author: lulululbj   File: ColorSpaceTransform.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get an element of this matrix by its row and column.
 *
 * <p>The rows must be within the range [0, 3),
 * and the column must be within the range [0, 3).</p>
 *
 * @return element (non-{@code null})
 *
 * @throws IllegalArgumentException if column or row was out of range
 */
public Rational getElement(int column, int row) {
    if (column < 0 || column >= COLUMNS) {
        throw new IllegalArgumentException("column out of range");
    } else if (row < 0 || row >= ROWS) {
        throw new IllegalArgumentException("row out of range");
    }

    int numerator = mElements[(row * COLUMNS + column) * RATIONAL_SIZE + OFFSET_NUMERATOR];
    int denominator = mElements[(row * COLUMNS + column) * RATIONAL_SIZE + OFFSET_DENOMINATOR];

    return new Rational(numerator, denominator);
}
 
Example #17
Source Project: android_9.0.0_r45   Author: lulululbj   File: ColorSpaceTransform.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Copy the {@link Rational} elements in row-major order from this matrix into the destination.
 *
 * @param destination
 *          an array big enough to hold at least {@code 9} elements after the
 *          {@code offset}
 * @param offset
 *          a non-negative offset into the array
 * @throws NullPointerException
 *          If {@code destination} was {@code null}
 * @throws ArrayIndexOutOfBoundsException
 *          If there's not enough room to write the elements at the specified destination and
 *          offset.
 */
public void copyElements(Rational[] destination, int offset) {
    checkArgumentNonnegative(offset, "offset must not be negative");
    checkNotNull(destination, "destination must not be null");
    if (destination.length - offset < COUNT) {
        throw new ArrayIndexOutOfBoundsException("destination too small to fit elements");
    }

    for (int i = 0, j = 0; i < COUNT; ++i, j += RATIONAL_SIZE) {
        int numerator = mElements[j + OFFSET_NUMERATOR];
        int denominator = mElements[j + OFFSET_DENOMINATOR];

        destination[i + offset] = new Rational(numerator, denominator);
    }
}
 
Example #18
Source Project: android_9.0.0_r45   Author: lulululbj   File: ParamsUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link Rational} value by approximating the float value as a rational.
 *
 * <p>Floating points too large to be represented as an integer will be converted to
 * to {@link Integer#MAX_VALUE}; floating points too small to be represented as an integer
 * will be converted to {@link Integer#MIN_VALUE}.</p>
 *
 * @param value a floating point value
 * @return the rational representation of the float
 */
public static Rational createRational(float value) {
    if (Float.isNaN(value)) {
        return Rational.NaN;
    } else if (value == Float.POSITIVE_INFINITY) {
        return Rational.POSITIVE_INFINITY;
    } else if (value == Float.NEGATIVE_INFINITY) {
        return Rational.NEGATIVE_INFINITY;
    } else if (value == 0.0f) {
        return Rational.ZERO;
    }

    // normal finite value: approximate it

    /*
     * Start out trying to approximate with denominator = 1million,
     * but if the numerator doesn't fit into an Int then keep making the denominator
     * smaller until it does.
     */
    int den = RATIONAL_DENOMINATOR;
    float numF;
    do {
        numF = value * den;

        if ((numF > Integer.MIN_VALUE && numF < Integer.MAX_VALUE) || (den == 1)) {
            break;
        }

        den /= 10;
    } while (true);

    /*
     *  By float -> int narrowing conversion in JLS 5.1.3, this will automatically become
     *  MIN_VALUE or MAX_VALUE if numF is too small/large to be represented by an integer
     */
    int num = (int) numF;

    return new Rational(num, den);
 }
 
Example #19
Source Project: Camera2   Author: Yuloran   File: OneCameraCharacteristicsImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public float getExposureCompensationStep()
{
    if (!isExposureCompensationSupported())
    {
        return -1.0f;
    }
    Rational compensationStep = mCameraCharacteristics.get(
            CameraCharacteristics.CONTROL_AE_COMPENSATION_STEP);
    return (float) compensationStep.getNumerator() / compensationStep.getDenominator();
}
 
Example #20
Source Project: android-PictureInPicture   Author: googlearchive   File: MediaSessionPlaybackActivity.java    License: Apache License 2.0 5 votes vote down vote up
/** Enters Picture-in-Picture mode. */
void minimize() {
    if (mMovieView == null) {
        return;
    }
    // Hide the controls in picture-in-picture mode.
    mMovieView.hideControls();
    // Calculate the aspect ratio of the PiP screen.
    Rational aspectRatio = new Rational(mMovieView.getWidth(), mMovieView.getHeight());
    mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
    enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
 
Example #21
Source Project: android-PictureInPicture   Author: googlearchive   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
/** Enters Picture-in-Picture mode. */
void minimize() {
    if (mMovieView == null) {
        return;
    }
    // Hide the controls in picture-in-picture mode.
    mMovieView.hideControls();
    // Calculate the aspect ratio of the PiP screen.
    Rational aspectRatio = new Rational(mMovieView.getWidth(), mMovieView.getHeight());
    mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
    enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
 
Example #22
Source Project: journaldev   Author: journaldev   File: PiPActivity.java    License: MIT License 5 votes vote down vote up
private void startPictureInPictureFeature() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            Rational aspectRatio = new Rational(videoView.getWidth(), videoView.getHeight());
            pictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
            enterPictureInPictureMode(pictureInPictureParamsBuilder.build());
        }
    }
 
Example #23
Source Project: journaldev   Author: journaldev   File: PiPActivity.java    License: MIT License 5 votes vote down vote up
@Override
public void onUserLeaveHint() {
    if (!isInPictureInPictureMode()) {
        Rational aspectRatio = new Rational(videoView.getWidth(), videoView.getHeight());
        pictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
        enterPictureInPictureMode(pictureInPictureParamsBuilder.build());
    }
}
 
Example #24
Source Project: Pix-Art-Messenger   Author: kriztan   File: RtpSessionActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
private void startPictureInPicture() {
    try {
        enterPictureInPictureMode(
                new PictureInPictureParams.Builder()
                        .setAspectRatio(new Rational(10, 16))
                        .build()
        );
    } catch (final IllegalStateException e) {
        //this sometimes happens on Samsung phones (possibly when Knox is enabled)
        Log.w(Config.LOGTAG, "unable to enter picture in picture mode", e);
    }
}
 
Example #25
Source Project: Conversations   Author: iNPUTmice   File: RtpSessionActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
private void startPictureInPicture() {
    try {
        enterPictureInPictureMode(
                new PictureInPictureParams.Builder()
                        .setAspectRatio(new Rational(10, 16))
                        .build()
        );
    } catch (final IllegalStateException e) {
        //this sometimes happens on Samsung phones (possibly when Knox is enabled)
        Log.w(Config.LOGTAG, "unable to enter picture in picture mode", e);
    }
}
 
Example #26
Source Project: android_9.0.0_r45   Author: lulululbj   File: PictureInPictureParams.java    License: Apache License 2.0 4 votes vote down vote up
/** @hide */
public Rational getAspectRatioRational() {
    return mAspectRatio;
}
 
Example #27
Source Project: android_9.0.0_r45   Author: lulululbj   File: PictureInPictureArgs.java    License: Apache License 2.0 4 votes vote down vote up
private PictureInPictureArgs(Rational aspectRatio, List<RemoteAction> actions,
        Rect sourceRectHint) {
    mAspectRatio = aspectRatio;
    mUserActions = actions;
    mSourceRectHint = sourceRectHint;
}
 
Example #28
Source Project: android_9.0.0_r45   Author: lulululbj   File: PictureInPictureArgs.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @hide
 */
@Deprecated
public void setAspectRatio(float aspectRatio) {
    // Temporary workaround
    mAspectRatio = new Rational((int) (aspectRatio * 1000000000), 1000000000);
}
 
Example #29
Source Project: android_9.0.0_r45   Author: lulululbj   File: PictureInPictureArgs.java    License: Apache License 2.0 4 votes vote down vote up
/** {@hide} */
public Rational getAspectRatioRational() {
    return mAspectRatio;
}
 
Example #30
Source Project: android_9.0.0_r45   Author: lulululbj   File: MarshalQueryablePrimitive.java    License: Apache License 2.0 4 votes vote down vote up
private void marshalPrimitive(Rational value, ByteBuffer buffer) {
    buffer.putInt(value.getNumerator());
    buffer.putInt(value.getDenominator());
}