1

我試圖將自定義轉換添加到片段。正如Link建議的,正確的解決方案是創建一個自定義視圖作爲片段容器,然後通過動畫添加新的屬性,使片段的轉換運行。但絕對它的Java。我實施瞭如下的C#和Xamarin:如何使用Xamarin中的動畫屬性創建自定義視圖

class SmartFrameLayout : FrameLayout 
{ 

    public SmartFrameLayout(Context context) : base(context) { } 
    public SmartFrameLayout(Context context, IAttributeSet attrs) : base(context, attrs) { } 
    public SmartFrameLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { } 
    public SmartFrameLayout(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) { } 

    //public float getXFraction() 
    //{ 
    // if (Width == 0) return 0; 
    // return GetX()/Width; 
    //} 

    //public void setXFraction(float fraction) 
    //{ 
    // Log.Debug("Fraction", fraction.ToString()); 
    // float xx = GetX(); 
    // SetX(xx * fraction); 
    //} 

    //private float XFraction; 


    public float XFraction 
    { 
     get { 
      if (Width == 0) return 0; 
      return GetX()/Width; 
     } 
     set { 
      float xx = GetX(); 
      SetX(xx * value); 
     } 
    } 

} 

正如你所看到的,首先我想實現的是一樣的教程(除C#不支持只讀的局部變量爲「最終」替換!) 但在objectAnimator屬性沒有正確調用。然後我想可能使用C#屬性將解決問題。但事實並非如此。

這裏是我的動畫的XML文件,名爲「from_right.xml」:

<?xml version="1.0" encoding="utf-8" ?> 
<objectAnimator 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:propertyName="xFraction" 
    android:valueType="floatType" 
    android:valueFrom="1.0" 
    android:valueTo="0" 
    android:duration="500"/> 

我改變propertyName的以「XFraction」,甚至是別的,但結果是一樣的。

使用「x」作爲propertyName和「1000」作爲valueFrom效果​​良好。

所以我想出了主要問題是objectAnimator根本無法調用setXFraction!

請告訴我我做錯了什麼,或者如果有更好的解決方案來獲得objectAnimator中valueFrom的屏幕寬度!

回答

2

您需要將setXFractiongetXFraction方法公開給Java;他們目前只在託管代碼中,並且不能訪問Java VM。

使用[Export]屬性揭露這些方法的Java,使動畫師可以使用它們:

[Export] 
    public float getXFraction() 
    { 
     if (Width == 0) return 0; 
     return GetX()/Width; 
    } 

    [Export] 
    public void setXFraction(float fraction) 
    { 
     Log.Debug("Fraction", fraction.ToString()); 
     float xx = GetX(); 
     SetX(xx * fraction); 
    } 

這將導致下面的Java代碼中SmartFrameLayout小號Android Callable Wrapper正在生成:

public float getXFraction() 
{ 
    return n_getXFraction(); 
} 

private native float n_getXFraction(); 


public void setXFraction (float p0) 
{ 
    n_setXFraction (p0); 
} 

private native void n_setXFraction (float p0); 
+2

這是一個很好的觀點。 TNX –

相關問題