2017-06-21 33 views
0

我畫了一條線。我在視圖中添加了AngleProperty。使用那個角度屬性我需要爲那個角度設置動畫。如何在xamarin.android中動畫定製屬性

這是我的看法,其中繪製直線

public class DrawView : View 
    { 
     Paint paint = new Paint(); 

     private double mvalue = 90; 
     public double Angle 
     { 
      get { return mvalue; } 
      set { 
       ObjectAnimator anim = ObjectAnimator.OfFloat(this, "Angle", (float)this.Angle, (float)value); 
       anim.SetDuration(500); 
       anim.Start(); 
       mvalue = value; 

      } 
     } 
     public DrawView(Context context):base(context) 
     { 
      paint.Color = Color.Green; 
     } 

     protected override void OnDraw(Canvas canvas) 
     { 
      base.OnDraw(canvas); 
      Value = Angle* Math.Pi/180; 
     var startX = 300; 
     var startY = 300; 
     var endX = 500 + 40 * Math.Sin(Value); 
     var endY = 500 + 40 * Math.Cos(Value); 

     canvas.DrawLine(startX, startY, (float)endX, (float)endY, paint);   
     } 
    } 

在主要活動中,我已經添加的按鈕在哪個角度給出,

public class MainActivity : Activity 
    { 
     DrawView drawview; 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
drawview = new DrawView(this); 
      Button b = new Button(this); 
      b.SetHeight(50); 
      b.SetWidth(50); 
      b.Click += B_Click; 
      LinearLayout lay = new LinearLayout(this); 
      lay.AddView(b); 
      lay.AddView(drawview); 
} 
private void B_Click(object sender, System.EventArgs e) 
     { 
      drawview.Angle= 180; 
     } 
} 

請人建議如何動畫上面一行某些角度

回答

0

邏輯正確,但有幾個問題。既然你已經定義了一個Angle屬性,你應該使用Angle屬性來控制動畫而不是使用Value屬性,那麼它可以很好地工作,就像this。欲瞭解更多信息,你可以閱讀document

protected override void OnDraw(Canvas canvas) 
    { 
     base.OnDraw(canvas); 
     Angle = Angle * Math.PI/180; 
     var startX = 300; 
     var startY = 300; 
     var endX = 500 + 40 * Math.Sin(Angle); 
     var endY = 500 + 40 * Math.Cos(Angle); 

     canvas.DrawLine(startX, startY, (float)endX, (float)endY, paint); 
    } 

當點擊一個按鈕,啓動Animation

private void B_Click(object sender, System.EventArgs e) 
    { 
     //certain angle decided by EditText's text 
     Int32 number = int.Parse(et.Text.ToString()); 
     drawview.Angle = number; 
    }