下面列出了怎么用android.util.Property的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Retrieves the value stored in {@link android.content.SharedPreferences} for a
* given {@link android.util.Property} associated with a VolumePanel.
* @return The given value, {@code defVal} if none was set, or null is the
* value could not be retrieved.
* @throws ClassCastException If a type error occurred between SP and Property.
*/
@SuppressWarnings("unchecked")
public <T, E> E getProperty(Class<T> clazz, Property<T, E> property, E defVal)
throws ClassCastException {
Class<E> type = property.getType();
String name = getName(clazz, property);
// Handle all types supported by SharedPreferences.
if (type.equals(Integer.TYPE) || type.equals(Integer.class))
return (E) Integer.valueOf(mPreferences.getInt(name, (Integer) defVal));
else if (type.equals(String.class) || type.equals(CharSequence.class))
return (E) mPreferences.getString(name, ((defVal == null) ? (String) defVal : defVal.toString()));
else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
return (E) Boolean.valueOf(mPreferences.getBoolean(name, (Boolean) defVal));
else if (type.equals(Long.TYPE) || type.equals(Long.class))
return (E) Long.valueOf(mPreferences.getLong(name, (Long) defVal));
else if (type.equals(Float.TYPE) || type.equals(Float.class))
return (E) Float.valueOf(mPreferences.getFloat(name, (Float) defVal));
else if (type.getClass().isAssignableFrom(Set.class))
return (E) mPreferences.getStringSet(name, (Set<String>) defVal);
return defVal;
}
/**
* Constructs an animator for the specified property on a group of views.
* See {@link ObjectAnimator#ofFloat(Object, String, float...)} for
* implementation details.
*
* @param property The property being animated.
* @param value The value to which that property should animate.
* @param views The target views to animate.
* @return An animator for all the specified views.
*/
private static Animator groupAnimatorOfFloat(
Property<View, Float> property, float value, View... views) {
AnimatorSet animSet = new AnimatorSet();
AnimatorSet.Builder builder = null;
for (int i = views.length - 1; i >= 0; i--) {
final Animator anim = ObjectAnimator.ofFloat(views[i], property, value);
if (builder == null) {
builder = animSet.play(anim);
} else {
builder.with(anim);
}
}
return animSet;
}
/**
* Moves the workspace UI in the provided direction.
* @param direction the direction to move the workspace
* @param translation the amount of shift.
* @param alpha the alpha for the workspace page
*/
private void setWorkspaceTranslationAndAlpha(Direction direction, float translation, float alpha) {
Property<View, Float> property = direction.viewProperty;
mPageAlpha[direction.ordinal()] = alpha;
float finalAlpha = mPageAlpha[0] * mPageAlpha[1];
View currentChild = getChildAt(getCurrentPage());
if (currentChild != null) {
property.set(currentChild, translation);
currentChild.setAlpha(finalAlpha);
}
// When the animation finishes, reset all pages, just in case we missed a page.
if (Float.compare(translation, 0) == 0) {
for (int i = getChildCount() - 1; i >= 0; i--) {
View child = getChildAt(i);
property.set(child, translation);
child.setAlpha(finalAlpha);
}
}
}
private Animator createAnimation(final View view, Property<View, Float> property,
float start, float end, float terminalValue, TimeInterpolator interpolator,
int finalVisibility) {
float[] startPosition = (float[]) view.getTag(R.id.lb_slide_transition_value);
if (startPosition != null) {
start = View.TRANSLATION_Y == property ? startPosition[1] : startPosition[0];
view.setTag(R.id.lb_slide_transition_value, null);
}
final ObjectAnimator anim = ObjectAnimator.ofFloat(view, property, start, end);
SlideAnimatorListener listener = new SlideAnimatorListener(view, property, terminalValue, end,
finalVisibility);
anim.addListener(listener);
anim.addPauseListener(listener);
anim.setInterpolator(interpolator);
return anim;
}
protected void onProgress(float progress) {
Set<Property<View, Float>> set = hashMap.keySet();
for (Property<View, Float> property : set) {
Pair<Float, Float> value = hashMap.get(property);
property.set(mView, value.first + (value.second * progress));
}
}
public InteractionAnimation build() {
return new InteractionAnimation(this.mEndProgress) {
@Override
public void onProgress(float progress) {
Set<Property> set = hashMap.keySet();
for (Property property : set) {
Holder value = hashMap.get(property);
property.set(mDrawable, value.typeEvaluator.evaluate(progress, value.fromValue, value.toValue));
}
}
};
}
public InteractionAnimation build() {
return new InteractionAnimation(mEndProgress) {
@Override
public void onProgress(float progress) {
Set<Property<View, Float>> set = hashMap.keySet();
for (Property<View, Float> property : set) {
Pair<Float, Float> value = hashMap.get(property);
property.set(mView, value.first + (value.second * progress));
}
}
};
}
@Override
protected void onProgress(float progress) {
super.onProgress(progress);
Set<Property> set = hashMap.keySet();
for (Property property : set) {
Holder value = hashMap.get(property);
property.set(mView, value.typeEvaluator.evaluate(progress, value.fromValue, value.toValue));
}
}
private Property<View, Float> getTranslateProperty() {
if (mOrientation == LinearLayoutManager.VERTICAL) {
return View.TRANSLATION_Y;
} else {
return View.TRANSLATION_X;
}
}
/**
* Apply the specified {@code value} across the {@code array} of views using the {@code property}.
*/
@UiThread
public static <T extends View, V> void set(@NonNull T[] array,
@NonNull Property<? super T, V> setter, @Nullable V value) {
//noinspection ForLoopReplaceableByForEach
for (int i = 0, count = array.length; i < count; i++) {
setter.set(array[i], value);
}
}
public IntPropertyValuesHolder(Property property, Keyframes.IntKeyframes keyframes) {
super(property);
mValueType = int.class;
mKeyframes = keyframes;
mIntKeyframes = keyframes;
if (property instanceof IntProperty) {
mIntProperty = (IntProperty) mProperty;
}
}
public IntPropertyValuesHolder(Property property, int... values) {
super(property);
setIntValues(values);
if (property instanceof IntProperty) {
mIntProperty = (IntProperty) mProperty;
}
}
@Override
public void setProperty(Property property) {
if (property instanceof IntProperty) {
mIntProperty = (IntProperty) property;
} else {
super.setProperty(property);
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static ObjectAnimator animateMarkerToICS(final MapView map, Marker marker, GeoPoint finalPosition, final GeoPointInterpolator GeoPointInterpolator) {
TypeEvaluator<GeoPoint> typeEvaluator = new TypeEvaluator<GeoPoint>() {
@Override
public GeoPoint evaluate(float fraction, GeoPoint startValue, GeoPoint endValue) {
return GeoPointInterpolator.interpolate(fraction, startValue, endValue);
}
};
Property<Marker, GeoPoint> property = Property.of(Marker.class, GeoPoint.class, "position");
ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
animator.setDuration(3000);
animator.start();
return animator;
}
public FloatPropertyValuesHolder(Property property, float... values) {
super(property);
setFloatValues(values);
if (property instanceof FloatProperty) {
mFloatProperty = (FloatProperty) mProperty;
}
}
@Override
public void setProperty(Property property) {
if (property instanceof FloatProperty) {
mFloatProperty = (FloatProperty) property;
} else {
super.setProperty(property);
}
}
public static ObjectAnimator ofFloat(View target, Property<View, Float> property,
float... values) {
ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values);
cancelOnDestroyActivity(anim);
new FirstFrameAnimatorHelper(anim, target);
return anim;
}
/**
* Moves the Hotseat UI in the provided direction.
* @param direction the direction to move the workspace
* @param translation the amount of shift.
* @param alpha the alpha for the hotseat page
*/
public void setHotseatTranslationAndAlpha(Direction direction, float translation, float alpha) {
Property<View, Float> property = direction.viewProperty;
// Skip the page indicator movement in the vertical bar layout
if (direction != Direction.Y || !mLauncher.getDeviceProfile().isVerticalBarLayout()) {
property.set(mPageIndicator, translation);
}
property.set(mLauncher.getHotseat(), translation);
setHotseatAlphaAtIndex(alpha, direction.ordinal());
}
private void initHolders(){
for (Map.Entry<Property<View, Float>, Float> entry : transformMap.entrySet()) {
holders.add(PropertyValuesHolder.ofFloat(entry.getKey(), entry.getValue()));
}
if (!TextUtils.isEmpty(opacity)) {
holders.add(PropertyValuesHolder.ofFloat(View.ALPHA, WXUtils.fastGetFloat(opacity, 3)));
}
}
private AnimatorSet createAnimator(View from, View to, NavigationType navType, Direction direction) {
Property<View, Float> axis;
int fromTranslation;
int toTranslation;
int sign = direction.sign();
switch (navType) {
case GO:
axis = View.TRANSLATION_X;
fromTranslation = sign * -from.getWidth();
toTranslation = sign * to.getWidth();
break;
case SHOW:
axis = View.TRANSLATION_Y;
fromTranslation = direction == FORWARD ? 0 : from.getHeight();
toTranslation = direction == BACKWARD ? 0 : to.getHeight();
break;
default:
axis = View.TRANSLATION_X;
fromTranslation = 0;
toTranslation = 0;
break;
}
AnimatorSet set = new AnimatorSet();
if (from != null) {
set.play(ObjectAnimator.ofFloat(from, axis, 0, fromTranslation));
}
set.play(ObjectAnimator.ofFloat(to, axis, toTranslation, 0));
return set;
}
/**
* The preferred constructor to use when animating properties. If you use this constructor, you
* don't need to worry about the logic to apply the changes. This is taken care of by using the
* Setter provided by `property`.
*/
public AdditiveAnimation(T target, Property<T, Float> property, float startValue, float targetValue) {
mTarget = target;
mProperty = property;
mTargetValue = targetValue;
mStartValue = startValue;
setTag(property.getName());
}
public AdditiveAnimation(T target, Property<T, Float> property, float startValue, Path path, PathEvaluator.PathMode pathMode, PathEvaluator sharedEvaluator) {
mTarget = target;
mProperty = property;
mStartValue = startValue;
mPath = path;
mSharedPathEvaluator = sharedEvaluator;
mPathMode = pathMode;
mTargetValue = evaluateAt(1f);
setTag(property.getName());
}
public SlideAnimatorListener(View view, Property<View, Float> prop,
float terminalValue, float endValue, int finalVisibility) {
mProp = prop;
mView = view;
mTerminalValue = terminalValue;
mEndValue = endValue;
mFinalVisibility = finalVisibility;
view.setVisibility(View.VISIBLE);
}
/**
* TODO: documentation of byValueCanBeUsedByParentAnimators
*/
protected final T animatePropertyBy(final Property<V, Float> property, final float by, final boolean byValueCanBeUsedByParentAnimators) {
initValueAnimatorIfNeeded();
float currentTarget = getTargetPropertyValue(property);
if (getQueuedPropertyValue(property.getName()) != null) {
currentTarget = getQueuedPropertyValue(property.getName());
}
AdditiveAnimation animation = createAnimation(property, currentTarget + by);
initValueAnimatorIfNeeded();
mRunningAnimationsManager.addAnimation(mAnimationAccumulator, animation);
if (byValueCanBeUsedByParentAnimators) {
runIfParentIsInSameAnimationGroup(() -> mParent.animatePropertyBy(property, by, true));
}
return self();
}
protected final T animatePropertiesAlongPath(Property<V, Float> xProperty, Property<V, Float> yProperty, Property<V, Float> rotationProperty, Path path) {
PathEvaluator sharedEvaluator = new PathEvaluator();
if (xProperty != null) {
animate(xProperty, path, PathEvaluator.PathMode.X, sharedEvaluator);
}
if (yProperty != null) {
animate(yProperty, path, PathEvaluator.PathMode.Y, sharedEvaluator);
}
if (rotationProperty != null) {
animate(rotationProperty, path, PathEvaluator.PathMode.ROTATION, sharedEvaluator);
}
return self();
}
Float getActualPropertyValue(Property<T, Float> property) {
Float lastTarget = getLastTargetValue(property.getName());
if (lastTarget == null) {
lastTarget = property.get(mAnimationTarget);
}
return lastTarget;
}
@NonNull
public static <T> ObjectAnimator ofFloat(@Nullable T target,
@NonNull Property<T, Float> xProperty,
@NonNull Property<T, Float> yProperty,
@NonNull Path path) {
return ObjectAnimator.ofFloat(target, xProperty, yProperty, path);
}
@NonNull
public static <T> ObjectAnimator ofInt(@Nullable T target,
@NonNull Property<T, Integer> xProperty,
@NonNull Property<T, Integer> yProperty,
@NonNull Path path) {
return ObjectAnimator.ofInt(target, xProperty, yProperty, path);
}
private <T extends View> T createView(Class<T> cls, Map<String, String> props) {
try {
T v = cls.getConstructor(Context.class).newInstance(mParent.getContext());
// copy parsed uri params into the view properties
for (Map.Entry<String, String> p : props.entrySet()) {
Property<T, String> property = Property.of(cls, String.class, p.getKey());
property.set(v, p.getValue());
}
return v;
} catch (NoSuchMethodException|InstantiationException|
IllegalAccessException|InvocationTargetException e) {
throw new RuntimeException(e);
}
}
@NonNull
public static <T> ObjectAnimator ofArgb(@Nullable T target,
@NonNull Property<T, Integer> property, int... values) {
ObjectAnimator animator = ObjectAnimator.ofInt(target, property, values);
animator.setEvaluator(new ArgbEvaluator());
return animator;
}