android.animation.PropertyValuesHolder#ofObject ( )源码实例Demo

下面列出了android.animation.PropertyValuesHolder#ofObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: scene   文件: AbsoluteChangeBounds.java
@Override
public Animator createAnimator(final ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
    if (startValues == null || endValues == null) {
        return null;
    }

    final View view = endValues.view;
    sceneRoot.getLocationInWindow(tempLocation);
    int startX = (Integer) startValues.values.get(PROPNAME_WINDOW_X) - tempLocation[0];
    int startY = (Integer) startValues.values.get(PROPNAME_WINDOW_Y) - tempLocation[1];
    int endX = (Integer) endValues.values.get(PROPNAME_WINDOW_X) - tempLocation[0];
    int endY = (Integer) endValues.values.get(PROPNAME_WINDOW_Y) - tempLocation[1];
    // TODO: also handle size changes: check bounds and animate size changes
    if (startX != endX || startY != endY) {
        final int width = view.getWidth();
        final int height = view.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        final BitmapDrawable drawable = new BitmapDrawable(bitmap);
        view.setAlpha(0);
        drawable.setBounds(startX, startY, startX + width, startY + height);
        sceneRoot.getOverlay().add(drawable);
        Path topLeftPath = getPathMotion().getPath(startX, startY, endX, endY);
        PropertyValuesHolder origin = PropertyValuesHolder.ofObject(
                DRAWABLE_ORIGIN_PROPERTY, null, topLeftPath);
        ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(drawable, origin);
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                sceneRoot.getOverlay().remove(drawable);
                view.setAlpha(1);
            }
        });
        return anim;
    }
    return null;
}
 
/**
 * ObjectAnimator: You can also create multiple animations by PropertyValuesHolder.
 * <p>
 * ValueAnimator has the same method, but we don't use it that way. #ValueAnimator.ofPropertyValuesHolder()#
 *
 * @return
 */
public Animator getObjectAnimatorByPropertyValuesHolder() {
    PropertyValuesHolder bgColorAnimator = PropertyValuesHolder.ofObject("backgroundColor",
            new ArgbEvaluator(),
            0xff009688, 0xff795548);
    PropertyValuesHolder rotationXAnimator = PropertyValuesHolder.ofFloat("rotationX",
            0f, 360f);
    ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mPuppet, bgColorAnimator, rotationXAnimator);
    objectAnimator.setDuration(3000);
    objectAnimator.setRepeatCount(1);
    objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
    return objectAnimator;
}
 
@Override
public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {
        case R.id.btn_start_animation_a:

            PropertyValuesHolder colorHolder = PropertyValuesHolder.ofInt("backgroundColor",
                    0xffffffff, 0xffff00ff, 0xffffff00, 0xffffffff);
            PropertyValuesHolder rotationHolder = PropertyValuesHolder.ofFloat("rotation", 180, -90, 90, 0);

            ObjectAnimator
                    .ofPropertyValuesHolder(tvTargetA, colorHolder, rotationHolder)
                    .setDuration(3000)
                    .start();
            break;

        case R.id.btn_start_animation_b:
            PropertyValuesHolder valuesHolder = PropertyValuesHolder
                    .ofObject("char", new CharacterEvaluate(), 'A', 'Z');

            ObjectAnimator
                    .ofPropertyValuesHolder(tvTargetB, valuesHolder)
                    .setDuration(4000)
                    .start();
            break;
        case R.id.btn_start_animation_c:
            ObjectAnimator.ofPropertyValuesHolder(ivTargetC,
                    getRotationValuesHolder(),
                    getScaleXValuesHolder(), getScaleYValuesHolder())
                    .setDuration(800)
                    .start();
            break;
    }
}
 
@TargetApi(LOLLIPOP)
private PropertyValuesHolder getPathValuesHolder(Run run, int dy, int dx) {
    PropertyValuesHolder propertyValuesHolder;
    if (IS_LOLLIPOP_OR_ABOVE) {
        PathMotion pathMotion = new PathMotion() {
            @Override
            public Path getPath(float startX, float startY, float endX, float endY) {
                return ReflowTextAnimatorHelper.getPath(startX, startY, endX, endY);
            }
        };
        propertyValuesHolder = PropertyValuesHolder.ofObject(SwitchDrawable.TOP_LEFT, null,
                pathMotion.getPath(
                        run.getStart().left,
                        run.getStart().top,
                        run.getEnd().left - dx,
                        run.getEnd().top - dy));
    } else {
        PointF startPoint = new PointF(run.getStart().left, run.getStart().top);
        PointF endPoint = new PointF(run.getEnd().left - dx, run.getEnd().top - dy);
        propertyValuesHolder = PropertyValuesHolder.ofObject(SwitchDrawable.TOP_LEFT, new TypeEvaluator<PointF>() {
            private final PointF point = new PointF();

            @Override
            public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
                float x = startValue.x + (endValue.x - startValue.x) * fraction;
                float y = startValue.y + (endValue.y - startValue.y) * fraction;

                point.set(x, y);

                return point;
            }
        }, startPoint, endPoint);
    }

    return propertyValuesHolder;
}
 
/**
 * ObjectAnimator: You can also create multiple animations by PropertyValuesHolder.
 * <p>
 * ValueAnimator has the same method, but we don't use it that way. #ValueAnimator.ofPropertyValuesHolder()#
 *
 * @return
 */
public Animator getObjectAnimatorByPropertyValuesHolder() {
    PropertyValuesHolder bgColorAnimator = PropertyValuesHolder.ofObject("backgroundColor",
            new ArgbEvaluator(),
            0xff009688, 0xff795548);
    PropertyValuesHolder rotationXAnimator = PropertyValuesHolder.ofFloat("rotationX",
            0f, 360f);
    ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mPuppet, bgColorAnimator, rotationXAnimator);
    objectAnimator.setDuration(3000);
    objectAnimator.setRepeatCount(1);
    objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
    return objectAnimator;
}