【问题标题】:How to create custom views with properties for animating in Xamarin如何使用 Xamarin 中的动画属性创建自定义视图
【发布时间】: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


    【解决方案1】:

    您需要将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);
        }
    

    这将导致在SmartFrameLayouts Android Callable Wrapper 中生成以下Java 代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      相关资源
      最近更新 更多