【发布时间】:2016-03-01 10:47:40
【问题描述】:
我正在尝试向片段添加自定义过渡。正如Link 建议的那样,正确的解决方案是创建一个自定义视图作为片段容器,然后通过为新添加的属性设置动画,使片段的过渡运行。但绝对是在java上。我在 C# 和 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 的屏幕宽度!
【问题讨论】:
标签: android android-fragments xamarin android-custom-view android-transitions