Android片段转换自定义视图

时间:2015-05-04 11:22:51

标签: android android-fragments android-animation android-custom-view

我想为片段转换创建自定义动画。动画采用圆形,从特定大小增加半径,直到等于窗口高度。我可以使用 ScaleAnimation ,但之后我将失去可绘制的质量。有人有任何想法要做到这一点。提前谢谢。

1 个答案:

答案 0 :(得分:0)

确定。我会回答我自己的问题:)。要使用我的自定义过渡(增加其大小的彩色圆圈)为片段过渡设置动画,我已完成以下操作:   - 创建一个绘制圆圈的自定义视图,并增加增加半径的可能性;   - 将此视图放在我的 FragmentActivity xml布局中;   - 当点击目标按钮并且片段应该改变时,我称之为ValueAnimator来改变圆半径;

最终代码如下所示:

public class MainScreenActivity extends AppCompatActivity {

    private Button targetButton;
    private CircleView circleView;



    public void targetButtonClick(View view) {
        AnimationHandler handler = new AnimationHandler(view.getX(),view.getY());
        handler.animate();
    }

    private class AnimationHandler implements Animator.AnimatorListener, ValueAnimator.AnimatorUpdateListener {
        private static final int TRANSITION_ANIM_DURATION = 500;
        private final float centerX;
        private final float centerY;
        private ValueAnimator valueAnimator;

        public AnimationHandler(float x, float y) {
            this.centerX = x;
            this.centerY = y;
            init();
        }

        private void init() {
            Point point = new Point();
            getWindowManager().getDefaultDisplay().getSize(point);
            valueAnimator = ValueAnimator.ofFloat(0, point.y);
            valueAnimator.setDuration(TRANSITION_ANIM_DURATION);
            valueAnimator.addUpdateListener(this);
            valueAnimator.addListener(this);
        }

        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            circleView.setRadius((float) animation.getAnimatedValue());
        }

        public void animate() {
            circleView.setXCenter(centerX);
            circleView.setYCenter(centerY);
            int color = getResources().getColor(android.R.color.dark_red);
            circleView.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.trans_circle_width));
            valueAnimator.start();
        }
    }
}