拖放期间在DragShadowBuilder画布中缩放动画

时间:2014-10-08 19:40:25

标签: android

如何创建自定义DragShadowBuilder来为画布中的视图缩放设置动画,而不是仅缩放到设定的大小?

例如,我有一个用于拖放的ImageView,当我按下ImageView时,图像需要在用于拖放的画布中增长,当您将其拖动到手指时,您会看到放下区域。

我可以使用以下CustomDragShadowBuilder找到的here类来将图像缩放到设定的大小,但有没有办法为缩放设置动画?

public static class CustomDragShadowBuilder extends View.DragShadowBuilder {

    private static final int SCALING_FACTOR = 4;

    public CustomDragShadowBuilder(View view) {
        super(view);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
        View v = getView();
        final int width = v.getWidth() * SCALING_FACTOR;
        final int height = v.getHeight() * SCALING_FACTOR;
        shadowSize.set(width, height);
        shadowTouchPoint.set(width / 2, height / 2);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        canvas.scale(SCALING_FACTOR, SCALING_FACTOR);
        getView().draw(canvas);
    }

}

1 个答案:

答案 0 :(得分:0)

我找到的最佳解决方案是在开始拖动之前为比例设置动画,如下所示:

    final View yourView;
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(yourView, "scaleX",4f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(yourView, "scaleY",4f);
    AnimatorSet animSetXY = new AnimatorSet();
    animSetXY.playTogether(scaleX, scaleY);
    animSetXY.setDuration(duration);
    final View.DragShadowBuilder shadow = new View.DragShadowBuilder(this);
    animSetXY.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            v.startDrag(dragData,  // the data to be dragged
                    new CustomDragShadowBuilder(yourView) ,  // the drag shadow builder
                    null,
                    0          // flags (not currently used, set to 0)
            );
        }
    });
    animSetXY.start();