我試圖將自定義轉換添加到片段。正如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的屏幕寬度!
這是一個很好的觀點。 TNX –