2013-08-16 43 views
0

所以,我的Android項目中有一個縮放動畫。縮放是在單擊包含圖像視圖的相對佈局並完成更改時完成的。動畫保留更改,功能明智

現在,我知道約anim.FillAfter = true;,我已經設法保持動畫的最後狀態。它仍然存在問題。假設我有一個400x600像素的圖像,可以縮小到200x300(x,y不更改位置)。當我點擊包含圖像的相對佈局時,圖像會縮小。動畫結束後,圖像看起來處於200x300狀態。不過,我仍然可以通過單擊縮放左側的空白區域(即圖像用於填充的右側200像素和底部300像素底部)來啓動動畫。我最好的猜測是,在視覺上,這些變化正在發生並持續下去,但只是在視覺上。

代碼明智的,這個它是什麼樣子:

UI建設者:

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); 
} 

最後,在CustomGestureListener的OnClick聽衆:

​​

我試過的其他東西是取代FillAfter選項。在view.Animation.AnimationEnd事件處理程序中,我對視圖進行了重新縮放。這些線上的東西:

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

哪個結果有我想要的結果! (在視覺和功能上重新縮放之後保持最後狀態)。我改變視圖的佈局參數的問題是,當發生這種情況時,視圖有一個視覺問題。隨着佈局參數更改的執行,幾分之一秒,整個視圖重新縮放到其自身的較小版本版本,然後才採用正確的大小。

爲解決此問題的任何解決方案表示歡迎,無論是搶斷後的填充或視圖的的LayoutParams變化引起的視覺干擾......或其他任何東西,我可以錯過

回答

0

所以,我最終在更改佈局參數時發現視覺閃爍的工作週期。

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

問題有,這是一個視覺輕拂:如前所述,應用在動畫結束時之後我的問題得到解決。但是,在動畫實際結束之前調用AnimationEnd引起了視覺輕彈。所以...這爲我修好了:https://stackoverflow.com/a/5110476