保持动画状态在无效之间



我试图在无效后保存视图状态,我非常不成功。

问题是我有一个图像,用户可以缩放(在右侧或左侧)。在他缩放后,应用程序将在一些位置上绘制点,在用户点击其中一些点后,我希望它改变(这是一张带有可选择城市的地图)。

问题是,点击点后,我需要调用invalidate()调用onDraw()再次改变点,但无效后地图得到其原始状态,所以它的"未缩放"再次。请告诉我,如何通过invalidate方法保存应用于视图的动画状态?

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (zoomed) {
        Paint p //init Paint
        for (Location l : locations) {
            if (l.selected) {
                canvas.drawCircle(l.calculatedPixels.X, l.calculatedPixels.Y, pxFromDp(getContext(), 10), p);
            } else {
                canvas.drawCircle(l.calculatedPixels.X, l.calculatedPixels.Y, pxFromDp(getContext(), 5), p);
                p.setStyle(Paint.Style.STROKE);
                canvas.drawCircle(l.calculatedPixels.X, l.calculatedPixels.Y, pxFromDp(getContext(), 10), p);
            }
//NOTHING OF THIS WORKS CORRECTLY
        if (scaleByTownTouch && getScaleX() != scaleTo) {
            //canvas.restore();
            setScaleX(scaleTo);
            setScaleY(scaleTo);
            setPivotX(pivotX);
            setPivotY(pivotY);
            /*ScaleAnimation scaleAnimation = new ScaleAnimation(scaleFrom, scaleTo, scaleFrom, scaleTo, pivotX, pivotY);
            scaleAnimation.setDuration(0);
            scaleAnimation.setFillAfter(true);
            this.startAnimation(scaleAnimation);*/
            scaleByTownTouch = false;
        }
    }
}
@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (zoomed) {
            for (Location l : locations) {
                if (l.calculatedPixels.isTouchNearPoint(event.getX(), event.getY(), 50, scaleTo)) {
                    l.selected = !l.selected;
                    scaleByTownTouch = true;
                    //THIS UNZOOMS WHOLE VIEW
                    invalidate();
                    return true;
                }
            }
            zoom(new Point());
        } else {
            float xTouch = event.getX();
            float yTouch = event.getY();
            Point touch = new Point();
            touch.X = xTouch / measuredX * 100;
            touch.Y = 100 - (yTouch / measuredY * 100);
            zoom(touch);
        }
    }
    return false;
}
public void zoom(Point touch) {
    if (!scaleByTownTouch) {
        if (!zoomed) {
            if (!touch.isBelowLine()) {
                pivotX = measuredX;
                pivotY = measuredY;
                scaleTo = 1.7f;
            } else {
                pivotX = 0;
                pivotY = 0;
                scaleTo = 1.5f;
            }
            scaleFrom = 1;
        } else {
            scaleFrom = scaleTo;
            scaleTo = 1;
        }
        final MapImageView miv = this;
        ScaleAnimation scaleAnimation = new ScaleAnimation(scaleFrom, scaleTo, scaleFrom, scaleTo, pivotX, pivotY);
        scaleAnimation.setDuration(1000);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                zoomed = !zoomed;
                miv.invalidate();
            }
        });
        this.startAnimation(scaleAnimation);
    }
}

您应该尝试使用Animator而不是Animation。动画只改变渲染,而动画器改变对象的属性,这些属性将在无效后保留。

看看ValueAnimator或ObjectAnimator

相关内容

  • 没有找到相关文章

最新更新