繁体   English   中英

Android Xamarin-如何添加动画以更改视图的位置?

[英]Android Xamarin - How To Add Animations To View's Position Changes?

因此,我对Android有点陌生。 我读了一些教程,但发现它很难理解。 我希望有人能帮助我了解如何实现动画,以便在我正在处理的当前上下文上实现视图(菜鸟友好)。

假设这是我的.axml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/top_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" />

现在,这是我的活动(信息不完全,但足以知道我想做什么:

private LinearLayout _topContainer;
private IDictionary<string, FrameLayout> _framelayoutViewsDictionary = new Dictionary<string, FrameLayout>();
private LinearLayout.LayoutParams layoutParams;

protected override void OnCreate(Bundle bundle)
{
    //SetContentView and other stuff...
    layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent, 1);
    _topContainer = FindViewById<LinearLayout>(Resource.Id.top_container);
}


public void CreateFirstVideoPlayer
{
    _framelayoutViewsDictionary.Add(Constants.Views.TOP_FRAMELAYOUT, new FrameLayout(this)
    {
        Id = 1,
        LayoutParameters = layoutParams
    });

    var fragmentManager = FragmentManager.BeginTransaction();
    fragmentManager.Add(_framelayoutViewsDictionary[Constants.Views.TOP_FRAMELAYOUT].Id, /*Create New MediaPlayer Here*/, Constants.FragmentTag.TOP_FRAGMENT);
    fragmentManager.Commit();
    _topContainer.AddView(_framelayoutViewsDictionary[Constants.Views.TOP_FRAMELAYOUT]);
}

public void CreateSecondVideoPlayer
{
    _framelayoutViewsDictionary.Add(Constants.Views.BOTTOM_FRAMELAYOUT, new FrameLayout(this)
    {
        Id = 2,
        LayoutParameters = layoutParams
    });

    var fragmentManager = FragmentManager.BeginTransaction();
    fragmentManager.Add(_framelayoutViewsDictionary[Constants.Views.BOTTOM_FRAMELAYOUT].Id, /*Create New MediaPlayer Here*/, Constants.FragmentTag.BOTTOM_FRAGMENT);
    fragmentManager.Commit();
    _topContainer.AddView(_framelayoutViewsDictionary[Constants.Views.BOTTOM_FRAMELAYOUT]);
}

如你看到的。 我正在以编程方式创建一个新的FrameLayout,在其顶部添加一个片段。 然后将那个FrameLayout放在一个LinearLine的顶视图上。 现在,当我添加第二个FrameLayout时。 第一个FrameLayout被减半,第二个被添加。 因此,现在每个FrameLayout都占用50:50的空间。 所以我想做的是。 添加第二个FrameLayout时,我希望第一个FrameLayout进行动画处理,以便它缓慢向上移动到屏幕的上半部分,而不是立即出现在屏幕的顶部。 当我删除第二个FrameLayout时,第一个FrameLayout将缓慢地动画回到屏幕中央。

希望这一切都有道理。 如果您需要更多信息,请发表评论!

使用支持库执行片段的滑入/滑出操作就像将片段的自定义动画设置为内置资源abc_slide_in_bottomabc_slide_out_bottom动画一样容易。

例:

SupportFragmentManager
    .BeginTransaction()
    .SetCustomAnimations(
        Resource.Animation.abc_slide_in_bottom, 
        Resource.Animation.abc_slide_out_bottom,
        Resource.Animation.abc_slide_in_bottom, 
        Resource.Animation.abc_slide_out_bottom)
    .AddToBackStack(count.ToString())
    .Add(Resource.Id.linearLayout1, new AFragmentSubClass(), count.ToString())
    .Commit();

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM