0

所以,我的 Android 项目中有一个缩放动画。单击包含图像视图的相对布局并且我希望保留更改时完成缩放。

现在,我知道anim.FillAfter = true;并且我已经设法保持动画的结束状态。还是有问题的。假设我有一个 400x600 像素的图像,我缩小到 200x300(x,y 不改变位置)。当我单击包含图像的相对布局时,图像会缩小。动画结束后,图像似乎处于 200x300 状态。但是,我仍然可以通过单击缩放留下的空白区域(右侧 200 像素和图像用于填充的底部 300 像素)来启动动画。我对此的最佳猜测是,在视觉上,变化正在发生并持续存在,但只是视觉上的。

代码方面,这就是它的样子:

用户界面构建器:

CustomGestureListener container = new CustomGestureListener (this); <- Custom relative layout integrating GestureDetector.IOnGestureListener, GestureDetector.IOnDoubleTapListener and ScaleGestureDetector.IOnScaleGestureListener
ImageView iv = new ImageView (this);
iv.SetImageDrawable (workingCopy);
iv.SetBackgroundColor (Android.Graphics.Color.Green);
iv.Clickable = false;
container.AddView (iv, new ViewGroup.LayoutParams (ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent));

GesturesExtension.Click(container).ActionEvent += delegate(GesturesExtension.State StateValue) {
            PTAnimationExtender.Scale(container, new System.Drawing.Size(container.Width, container.Height), new System.Drawing.Size((int)(container.Width / 2), (int)(container.Height / 2)), 2, delegate {
                AlertDialog.Builder ad = new AlertDialog.Builder(this);
                ad.SetMessage("View scaled");
                ad.Create().Show();
            });
        };

缩放:

public static void Scale (this View view, Size Start, Size End, double DurationInSec, Action Callback)
{
    ScaleAnimation sa = new ScaleAnimation (((float)Start.Width) / ((float)view.Width), ((float)End.Width) / ((float)view.Width), ((float)Start.Height) / ((float)view.Height), ((float)End.Height) / ((float)view.Height)); 
    sa.Duration = (long)(DurationInSec * 1000);
    sa.FillAfter = true;
    sa.Interpolator = new DecelerateInterpolator (2.5f);
    view.Animation = sa;
    view.Animation.AnimationEnd += delegate(object sender, Animation.AnimationEndEventArgs e) {
        if (Callback != null)
            Callback.Invoke ();
    };
    view.StartAnimation (view.Animation);
}

最后, OnClick 监听器CustomGestureListener

public class ClickClass
    {
        public delegate void Event (State StateValue);
        public event Event ActionEvent;

        public void CallEvent(State StateValue)
        {
            if (ActionEvent != null)
                ActionEvent (StateValue);
        }
    }
    ///<remarks>Detect a Click over your CustomGestureListener</remarks>
    public static ClickClass Click(this CustomGestureListener view)
    {
        ClickClass click = new ClickClass ();

        view.OnTapEvent += delegate(State state) {
            click.CallEvent (state);
        };

        return click;
    }

我尝试过的其他方法是替换该FillAfter选项。在view.Animation.AnimationEnd事件处理程序上,我对视图进行了重新缩放。这些线上的东西:

ViewGroup.LayoutParams lp = view.LayoutParameters;
lp.Width = End.Width;
lp.Height = End.Height;
view.LayoutParameters = lp;

最终得到了我想要的结果!(在视觉和功能上重新缩放后保持最终状态)。我在更改视图的布局参数时遇到的问题是,当发生这种情况时,视图会出现视觉故障。随着布局参数更改的执行,在几分之一秒内,整个视图会重新缩放为自身的较小版本,然后再采用正确的大小。

欢迎任何解决此问题的解决方案,无论是通过解决填充后的问题还是由视图的布局参数更改引起的视觉故障......或者我可能错过的任何其他事情

4

1 回答 1

0

因此,我最终找到了在更改布局参数时视觉闪烁的解决方法。如前所述,在动画端应用以下内容时,我的问题得到了解决:

ViewGroup.LayoutParams lp = view.LayoutParameters;
lp.Height = 350;
lp.Width = 150;
view.LayoutParameters = lp;

问题在于视觉闪烁。AnimationEnd但是视觉闪烁是由在动画实际结束之前被调用引起的。所以......这为我修复了它:https ://stackoverflow.com/a/5110476

于 2013-08-16T14:38:52.160 回答