android - 动画 LayoutParams LeftMargin 和 TopMargin

标签 android android-layout xamarin.android android-animation android-layoutparams

我已阅读有关如何在 Android 中执行此操作的信息,但我似乎找不到 Xamarin Android 等效于为布局的 LeftMargin 和 TopMargin 设置动画。显然 Xamarin 有“动画”,但我无法在 Xamarin 中设置“newLeftMargin * interpolatedTime”的地方找到“applyTransformation”部分。

这是标准的 Android 引用资料: Android - Change left margin using animation

Animation a = new Animation();
//applyTransformation???? with "newLeftMargin * interpolatedTime"
a.Duration = 500;
MyThingy.StartAnimation(a);

感谢user Apineda提供答案。这是我最终编写的代码,以防有人需要。第一个构造函数从当前状态开始对边距进行动画处理,而不仅仅是从零开始。第二个构造函数要求您指定起始页边距。

class LayoutMarginAnimation : Android.Views.Animations.Animation
{
    private ViewGroup ViewToTransform;
    private int LeftMargin_Destination;
    private int TopMargin_Destination;
    private int LeftMargin_Source;
    private int TopMargin_Source;

    /// <summary>
    /// Animates a layout from it's current margins to specified margins
    /// </summary>
    /// <param name="a_viewToTransform">A view to transform.</param>
    /// <param name="a_LeftMargin_Destination">A left margin destination.</param>
    /// <param name="a_TopMargin_Destination">A top margin destination.</param>
    public LayoutMarginAnimation(
        ViewGroup a_viewToTransform,
        int a_LeftMargin_Destination,
        int a_TopMargin_Destination
    )
    {
        this.ViewToTransform = a_viewToTransform;
        this.LeftMargin_Destination = a_LeftMargin_Destination;
        this.TopMargin_Destination = a_TopMargin_Destination;

        this.LeftMargin_Source = (this.ViewToTransform.LayoutParameters as RelativeLayout.LayoutParams).LeftMargin;
        this.TopMargin_Source = (this.ViewToTransform.LayoutParameters as RelativeLayout.LayoutParams).TopMargin;
    }

    /// <summary>
    /// Animates a layout from specified margins to specified margins, regardless of what the margins are currently set to.  
    /// </summary>
    /// <param name="a_viewToTransform">A view to transform.</param>
    /// <param name="a_LeftMargin_Source">A left margin source.</param>
    /// <param name="a_TopMargin_Source">A top margin source.</param>
    /// <param name="a_LeftMargin_Destination">A left margin destination.</param>
    /// <param name="a_TopMargin_Destination">A top margin destination.</param>
    public LayoutMarginAnimation(
        ViewGroup a_viewToTransform,
        int a_LeftMargin_Source,
        int a_TopMargin_Source,
        int a_LeftMargin_Destination,
        int a_TopMargin_Destination
    )
    {
        this.ViewToTransform = a_viewToTransform;
        this.LeftMargin_Destination = a_LeftMargin_Destination;
        this.TopMargin_Destination = a_TopMargin_Destination;
        this.LeftMargin_Source = a_LeftMargin_Source;
        this.TopMargin_Source = a_TopMargin_Source;
    }

    protected override void ApplyTransformation(float interpolatedTime, Transformation t)
    {
        //Console.WriteLine("ApplyTransformation with interpolatedTime = " + interpolatedTime);
        RelativeLayout.LayoutParams layoutParams = this.ViewToTransform.LayoutParameters as RelativeLayout.LayoutParams;

        layoutParams.LeftMargin = this.LeftMargin_Source + (int)((this.LeftMargin_Destination - this.LeftMargin_Source) * interpolatedTime);
        layoutParams.TopMargin = this.TopMargin_Source + (int)((this.TopMargin_Destination - this.TopMargin_Source) * interpolatedTime);

        this.ViewToTransform.LayoutParameters = layoutParams;
    }
}

下面是调用两个构造函数的方法。

            // Animates a layout from it's current margins to specified margins
            LayoutMarginAnimation animation = new LayoutMarginAnimation(this.DraggableSeedImageContainer, 1000, 1000);
            // Animates a layout from specified margins to specified margins, regardless of what the margins are currently set to.
            //LayoutMarginAnimation animation = new LayoutMarginAnimation(this.DraggableSeedImageContainer, 200, 200, 1000, 1000);
            animation.Duration = 500;
            this.DraggableSeedImageContainer.StartAnimation(animation);

最佳答案

您在执行此操作时遇到问题,那是因为 Animation 类是一个抽象类。您必须创建自己的实现并覆盖 ApplyTransformation() 方法。

使用您提供的链接将其翻译成 Xamarin.Android 我们有:

我的自定义动画类:

class ViewLeftMargingAnimation : Animation
{
    View _viewToTransform;

    int _newLeftMargin;

    public ViewLeftMargingAnimation (View viewToTransform, int newLeftMargin)
    {
        _viewToTransform = viewToTransform;

        _newLeftMargin = newLeftMargin;
    }

    protected override void ApplyTransformation (float interpolatedTime, Transformation t)
    {
        var layoutParams = (LinearLayout.LayoutParams)_viewToTransform.LayoutParameters;
        layoutParams.LeftMargin = (int)(_newLeftMargin * interpolatedTime);
        _viewToTransform.LayoutParameters = layoutParams;
    }
}

使用你的动画:

// View that will be animated:
var button = FindViewById<Button> (Resource.Id.myButton);

// Animation object:
var a = new ViewLeftMargingAnimation (button, 150);

// Animation's duration:
a.Duration = 500;

// Start my animation 
button.StartAnimation (a);

这也可以使用 ValueAnimator 类来实现。关于 Xamarin.Android 动画的更多信息 here .

关于android - 动画 LayoutParams LeftMargin 和 TopMargin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42287734/

相关文章:

android - 如何使用android.telecom.InCallService实现调用合并、等待和电话 session

android - 在真实的安卓设备上模拟低内存

android - 滑动以打开选项 ListView

android - 如何在 Xamarin Picker 中设置项目列表的样式(在 Android 中)

Xamarin.Android:如何链接静态库?

c# - 信任其他 CA 并在 net6 MAUI 解决方案中使用 Android 证书存储

android - 列表适配器中未调用 DiffCallback

java - 验证 SSL 认证 Java/Android

android - 如何进入 ListView 对话框

android - 在同一项目中支持3.0及以下级别