下面列出了怎么用android.util.SizeF的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the value associated with the given key, or null if
* no mapping of the desired type exists for the given key or a null
* value is explicitly associated with the key.
*
* @param key a String, or null
* @return a Size value, or null
*/
@Nullable
public SizeF getSizeF(@Nullable String key) {
unparcel();
final Object o = mMap.get(key);
try {
return (SizeF) o;
} catch (ClassCastException e) {
typeWarning(key, o, "SizeF", e);
return null;
}
}
@Override
public SizeF unmarshal(ByteBuffer buffer) {
float width = buffer.getFloat();
float height = buffer.getFloat();
return new SizeF(width, height);
}
@Test
public void testSerializeSizeF() {
SizeF expectedValue = new SizeF(9, 5);
mCoder.serialize(bundle(), randomKey(), expectedValue);
assertEquals(expectedValue, bundle().getSizeF(randomKey()));
}
@Config(minSdk = Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Test
public void testCoderApi21NotNullAbove21() throws CoderNotFoundException {
assertCoderNotNull(Size.class);
assertCoderNotNull(SizeF.class);
}
@Config(maxSdk = Build.VERSION_CODES.KITKAT)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Test(expected = CoderNotFoundException.class)
public void testCoderApi21ThrowsBelowApi21() throws CoderNotFoundException {
assertCoderNotNull(Size.class);
assertCoderNotNull(SizeF.class);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Test public void sizeFIsCorrectlyParcelled() {
TypeAdapter<SizeF> adapter = StaticAdapters.SIZE_F_ADAPTER;
SizeF expected = new SizeF(10.f, 10.f);
SizeF result = writeThenRead(adapter, expected);
assertThat(result).isEqualTo(expected);
}
@Override
public void onCaptureCompleted(CameraCaptureSession session,
CaptureRequest request,
TotalCaptureResult result) {
Long timestamp = result.get(CaptureResult.SENSOR_TIMESTAMP);
Long number = result.getFrameNumber();
Long exposureTimeNs = result.get(CaptureResult.SENSOR_EXPOSURE_TIME);
Long frmDurationNs = result.get(CaptureResult.SENSOR_FRAME_DURATION);
Long frmReadoutNs = result.get(CaptureResult.SENSOR_ROLLING_SHUTTER_SKEW);
Integer iso = result.get(CaptureResult.SENSOR_SENSITIVITY);
if (expoStats.size() > kMaxExpoSamples) {
expoStats.subList(0, kMaxExpoSamples / 2).clear();
}
expoStats.add(new NumExpoIso(number, exposureTimeNs, iso));
Float fl = result.get(CaptureResult.LENS_FOCAL_LENGTH);
Float fd = result.get(CaptureResult.LENS_FOCUS_DISTANCE);
Integer afMode = result.get(CaptureResult.CONTROL_AF_MODE);
Rect rect = result.get(CaptureResult.SCALER_CROP_REGION);
mFocalLengthHelper.setmFocalLength(fl);
mFocalLengthHelper.setmFocusDistance(fd);
mFocalLengthHelper.setmCropRegion(rect);
SizeF sz_focal_length = mFocalLengthHelper.getFocalLengthPixel();
String delimiter = ",";
StringBuilder sb = new StringBuilder();
sb.append(timestamp);
sb.append(delimiter + sz_focal_length.getWidth());
sb.append(delimiter + sz_focal_length.getHeight());
sb.append(delimiter + number);
sb.append(delimiter + exposureTimeNs);
sb.append(delimiter + frmDurationNs);
sb.append(delimiter + frmReadoutNs);
sb.append(delimiter + iso);
sb.append(delimiter + fl);
sb.append(delimiter + fd);
sb.append(delimiter + afMode);
String frame_info = sb.toString();
if (mRecordingMetadata) {
try {
mFrameMetadataWriter.write(frame_info + "\n");
} catch (IOException err) {
System.err.println("Error writing captureResult: " + err.getMessage());
}
}
((CameraCaptureActivity) mActivity).updateCaptureResultPanel(
sz_focal_length.getWidth(), exposureTimeNs, afMode);
}
public BcmRouterIntent putSizeF(String key, SizeF value) {
bundle.putSizeF(key, value);
return this;
}
/**
* Flatten a SizeF into the parcel at the current dataPosition(),
* growing dataCapacity() if needed.
*/
public final void writeSizeF(SizeF val) {
writeFloat(val.getWidth());
writeFloat(val.getHeight());
}
/**
* Read a SizeF from the parcel at the current dataPosition().
*/
public final SizeF readSizeF() {
final float width = readFloat();
final float height = readFloat();
return new SizeF(width, height);
}
private static void mapSensor(CameraMetadataNative m, Parameters p) {
// Use the largest jpeg size (by area) for both active array and pixel array
Size largestJpegSize = getLargestSupportedJpegSizeByArea(p);
/*
* sensor.info.activeArraySize
*/
{
Rect activeArrayRect = ParamsUtils.createRect(largestJpegSize);
m.set(SENSOR_INFO_ACTIVE_ARRAY_SIZE, activeArrayRect);
}
/*
* sensor.availableTestPatternModes
*/
{
// Only "OFF" test pattern mode is available
m.set(SENSOR_AVAILABLE_TEST_PATTERN_MODES, new int[] { SENSOR_TEST_PATTERN_MODE_OFF });
}
/*
* sensor.info.pixelArraySize
*/
m.set(SENSOR_INFO_PIXEL_ARRAY_SIZE, largestJpegSize);
/*
* sensor.info.physicalSize
*/
{
/*
* Assume focal length is at infinity focus and that the lens is rectilinear.
*/
float focalLength = p.getFocalLength(); // in mm
double angleHor = p.getHorizontalViewAngle() * Math.PI / 180; // to radians
double angleVer = p.getVerticalViewAngle() * Math.PI / 180; // to radians
float height = (float)Math.abs(2 * focalLength * Math.tan(angleVer / 2));
float width = (float)Math.abs(2 * focalLength * Math.tan(angleHor / 2));
m.set(SENSOR_INFO_PHYSICAL_SIZE, new SizeF(width, height)); // in mm
}
/*
* sensor.info.timestampSource
*/
{
m.set(SENSOR_INFO_TIMESTAMP_SOURCE, SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN);
}
}
protected MarshalerSizeF(TypeReference<SizeF> typeReference, int nativeType) {
super(MarshalQueryableSizeF.this, typeReference, nativeType);
}
@Override
public void marshal(SizeF value, ByteBuffer buffer) {
buffer.putFloat(value.getWidth());
buffer.putFloat(value.getHeight());
}
@Override
public Marshaler<SizeF> createMarshaler(
TypeReference<SizeF> managedType, int nativeType) {
return new MarshalerSizeF(managedType, nativeType);
}
@Override
public boolean isTypeMappingSupported(TypeReference<SizeF> managedType, int nativeType) {
return nativeType == TYPE_FLOAT && (SizeF.class.equals(managedType.getType()));
}
/**
* Get the default {@link StateCoder} from a class.
* If the class is equal to the coder class or the coder class extends/implements the class
* passed as parameter, the class is supported.
* In the other cases, you have to implement your own {@link StateCoder}.
*
* @param cls class that can be serialized/deserialized with a {@link StateCoder}
* @return coder related to the class passed as parameter
* @throws CoderNotFoundException if the coder wasn't found
*/
@NonNull
public static StateCoder getBasicCoderForClass(@NonNull Class<?> cls) throws CoderNotFoundException {
if (boolean.class == cls || Boolean.class == cls)
return new BooleanCoder();
if (boolean[].class == cls)
return new BooleanArrayCoder();
if (byte.class == cls || Byte.class == cls)
return new ByteCoder();
if (byte[].class == cls)
return new ByteArrayCoder();
if (char.class == cls || Character.class == cls)
return new CharCoder();
if (char[].class == cls)
return new CharArrayCoder();
if (CharSequence.class == cls)
return new CharSequenceCoder();
if (CharSequence[].class == cls)
return new CharSequenceArrayCoder();
if (double.class == cls || Double.class == cls)
return new DoubleCoder();
if (double[].class == cls)
return new DoubleArrayCoder();
if (float.class == cls || Float.class == cls)
return new FloatCoder();
if (float[].class == cls)
return new FloatArrayCoder();
if (IBinder.class == cls)
return new IBinderCoder();
if (int.class == cls || Integer.class == cls)
return new IntCoder();
if (int[].class == cls)
return new IntArrayCoder();
if (long.class == cls || Long.class == cls)
return new LongCoder();
if (long[].class == cls)
return new LongArrayCoder();
if (Parcelable.class == cls)
return new ParcelableCoder();
if (Parcelable[].class == cls)
return new ParcelableArrayCoder();
if (Serializable.class == cls)
return new SerializableCoder();
if (short.class == cls || Short.class == cls)
return new ShortCoder();
if (short[].class == cls)
return new ShortArrayCoder();
if (String.class == cls)
return new StringCoder();
if (String[].class == cls)
return new StringArrayCoder();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Size.class == cls)
return new SizeCoder();
if (SizeF.class == cls)
return new SizeFCoder();
}
return getBasicCoderForInheritedClass(cls);
}
@Test
public void testDeserializeSizeF() {
SizeF expectedValue = new SizeF(9, 5);
bundle().putSizeF(randomKey(), expectedValue);
assertEquals(expectedValue, mCoder.deserialize(bundle(), randomKey()));
}
public static void putValueToBundle(
@NonNull Bundle bundle, @NonNull String key, @NonNull Object value) {
if (value instanceof String) {
bundle.putString(key, (String) value);
} else if (value instanceof Integer) {
bundle.putInt(key, (int) value);
} else if (value instanceof Boolean) {
bundle.putBoolean(key, (boolean) value);
} else if (value instanceof Long) {
bundle.putLong(key, (long) value);
} else if (value instanceof Short) {
bundle.putShort(key, (short) value);
} else if (value instanceof Double) {
bundle.putDouble(key, (double) value);
} else if (value instanceof Float) {
bundle.putFloat(key, (float) value);
} else if (value instanceof Character) {
bundle.putChar(key, (char) value);
} else if (value instanceof Byte) {
bundle.putByte(key, (byte) value);
} else if (value instanceof CharSequence) {
bundle.putCharSequence(key, (CharSequence) value);
} else if (value instanceof Bundle) {
bundle.putBundle(key, (Bundle) value);
} else if (value instanceof Parcelable) {
bundle.putParcelable(key, (Parcelable) value);
} else if (value instanceof String[]) {
bundle.putStringArray(key, (String[]) value);
} else if (value instanceof int[]) {
bundle.putIntArray(key, (int[]) value);
} else if (value instanceof boolean[]) {
bundle.putBooleanArray(key, (boolean[]) value);
} else if (value instanceof long[]) {
bundle.putLongArray(key, (long[]) value);
} else if (value instanceof short[]) {
bundle.putShortArray(key, (short[]) value);
} else if (value instanceof double[]) {
bundle.putDoubleArray(key, (double[]) value);
} else if (value instanceof float[]) {
bundle.putFloatArray(key, (float[]) value);
} else if (value instanceof char[]) {
bundle.putCharArray(key, (char[]) value);
} else if (value instanceof byte[]) {
bundle.putByteArray(key, (byte[]) value);
} else if (value instanceof CharSequence[]) {
bundle.putCharSequenceArray(key, (CharSequence[]) value);
} else if (value instanceof Parcelable[]) {
bundle.putParcelableArray(key, (Parcelable[]) value);
} else if (value instanceof ArrayList) {
bundle.putIntegerArrayList(key, (ArrayList<Integer>) value);
} else if (value instanceof SparseArray) {
bundle.putSparseParcelableArray(key, (SparseArray<? extends Parcelable>) value);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (value instanceof IBinder) {
bundle.putBinder(key, (IBinder) value);
return;
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (value instanceof Size) {
bundle.putSize(key, (Size) value);
return;
} else if (value instanceof SizeF) {
bundle.putSizeF(key, (SizeF) value);
return;
}
}
if (value instanceof Serializable) {
bundle.putSerializable(key, (Serializable) value);
return;
}
throw new RuntimeException(String.format(Locale.getDefault(),
"Arguments extra %s has wrong type %s.", key, value.getClass().getName()));
}
}
public BundleHelper put(@NonNull String key, @Nullable SizeF value){
bundle.putSizeF(key, value);
return this;
}
public void putSizeF(java.lang.String key, SizeF value){
throw new RuntimeException("Stub!");
}
public SizeF getSizeF(java.lang.String key){
throw new RuntimeException("Stub!");
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@NonNull @Override public SizeF readFromParcel(@NonNull Parcel source) {
return source.readSizeF();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override public void writeToParcel(@NonNull SizeF value, @NonNull Parcel dest, int flags) {
dest.writeSizeF(value);
}
/**
* Returns the component-wise zoom ratio (each greater or equal than {@code 1.0});
* largest values means more zoom.
*
* @param activeArraySize active array size of the sensor (e.g. max jpeg size)
* @param cropSize size of the crop/zoom
*
* @return {@link SizeF} with width/height being the component-wise zoom ratio
*
* @throws NullPointerException if any of the args were {@code null}
* @throws IllegalArgumentException if any component of {@code cropSize} was {@code 0}
*/
private static SizeF getZoomRatio(Size activeArraySize, Size cropSize) {
checkNotNull(activeArraySize, "activeArraySize must not be null");
checkNotNull(cropSize, "cropSize must not be null");
checkArgumentPositive(cropSize.getWidth(), "cropSize.width must be positive");
checkArgumentPositive(cropSize.getHeight(), "cropSize.height must be positive");
float zoomRatioWidth = activeArraySize.getWidth() * 1.0f / cropSize.getWidth();
float zoomRatioHeight = activeArraySize.getHeight() * 1.0f / cropSize.getHeight();
return new SizeF(zoomRatioWidth, zoomRatioHeight);
}
/**
* Inserts a SizeF value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* @param key a String, or null
* @param value a SizeF object, or null
*/
public void putSizeF(@Nullable String key, @Nullable SizeF value) {
unparcel();
mMap.put(key, value);
}
/**
* Write a field's value into the saved state {@link Bundle}.
*
* @param state {@link Bundle} used to save the state
* @param key key retrieved from {@code fieldDeclaringClass#fieldName}
* @param fieldValue value of field
*/
@Override
public void serialize(@NonNull Bundle state, @NonNull String key, @NonNull SizeF fieldValue) {
state.putSizeF(key, fieldValue);
}
/**
* Inserts a SizeF value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* @param key a String, or null
* @param value a SizeF object, or null
*/
@TargetApi(21)
public Bundler putSizeF(String key, SizeF value) {
bundle.putSizeF(key, value);
return this;
}
/**
* Inserts a SizeF value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* @param key a String, or null
* @param value a SizeF object, or null
*/
@TargetApi(21)
public Bundler putSizeF(String key, SizeF value) {
bundle.putSizeF(key, value);
return this;
}
/**
* Inserts a SizeF value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* @param key a String, or null
* @param value a SizeF object, or null
*/
@TargetApi(21)
public Bundler putSizeF(String key, SizeF value) {
bundle.putSizeF(key, value);
return this;
}
/**
* Inserts a SizeF value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* @param key a String, or null
* @param value a SizeF object, or null
*/
@TargetApi(21)
public Bundler putSizeF(String key, SizeF value) {
bundle.putSizeF(key, value);
return this;
}