下面列出了android.app.PictureInPictureParams#android.util.Rational 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
/**
* 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;
}
/**
* 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;
}
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);
}
}
@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;
}
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;
}
/** 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());
}
/** 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());
}
@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);
}
}
private void updateViewInfo() {
if (mImageCapture != null) {
mImageCapture.setCropAspectRatio(new Rational(getWidth(), getHeight()));
mImageCapture.setTargetRotation(getDisplaySurfaceRotation());
}
if (mVideoCapture != null && MediaConstraints.isVideoTranscodeAvailable()) {
mVideoCapture.setTargetRotation(getDisplaySurfaceRotation());
}
}
@Override
protected void onUserLeaveHint() {
if (deviceSupportsPipMode()) {
PictureInPictureParams params = new PictureInPictureParams.Builder()
.setAspectRatio(new Rational(16, 9))
.build();
setPictureInPictureParams(params);
//noinspection deprecation
enterPictureInPictureMode();
}
}
@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);
}
}
/** {@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);
}
}
/** {@hide} */
PictureInPictureParams(Rational aspectRatio, List<RemoteAction> actions,
Rect sourceRectHint) {
mAspectRatio = aspectRatio;
mUserActions = actions;
mSourceRectHint = sourceRectHint;
}
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);
}
}
/**
* 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);
}
/**
* 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);
}
}
/**
* 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);
}
@Override
public float getExposureCompensationStep()
{
if (!isExposureCompensationSupported())
{
return -1.0f;
}
Rational compensationStep = mCameraCharacteristics.get(
CameraCharacteristics.CONTROL_AE_COMPENSATION_STEP);
return (float) compensationStep.getNumerator() / compensationStep.getDenominator();
}
/** 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());
}
/** 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());
}
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());
}
}
@Override
public void onUserLeaveHint() {
if (!isInPictureInPictureMode()) {
Rational aspectRatio = new Rational(videoView.getWidth(), videoView.getHeight());
pictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
enterPictureInPictureMode(pictureInPictureParamsBuilder.build());
}
}
@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);
}
}
@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);
}
}
/** @hide */
public Rational getAspectRatioRational() {
return mAspectRatio;
}
private PictureInPictureArgs(Rational aspectRatio, List<RemoteAction> actions,
Rect sourceRectHint) {
mAspectRatio = aspectRatio;
mUserActions = actions;
mSourceRectHint = sourceRectHint;
}
/**
* @hide
*/
@Deprecated
public void setAspectRatio(float aspectRatio) {
// Temporary workaround
mAspectRatio = new Rational((int) (aspectRatio * 1000000000), 1000000000);
}
/** {@hide} */
public Rational getAspectRatioRational() {
return mAspectRatio;
}
private void marshalPrimitive(Rational value, ByteBuffer buffer) {
buffer.putInt(value.getNumerator());
buffer.putInt(value.getDenominator());
}