首页 > 解决方案 > 使用 ViewPropertyAnimation 将对象从不可见缩放到可见

问题描述

有没有办法使用 ViewPropertyAnimation 使我的对象从不可见变为可见?我想把它移到一个位置。当我的对象移动到这个位置时,我想将它的大小增加到原来的大小。

btn.Animate()
.TranslationX(0)
.TranslationY(-250)
.SetDuration(1000)
.SetInterpolator(new OvershootInterpolator(4))
.ScaleXBy(0).ScaleX(1);

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"><android.support.design.widget.FloatingActionButton

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/button1" />

</RelativeLayout>

我的对象朝正确的方向移动,但它的大小保持不变。在此处输入图像描述

标签: javaxmlxamarin

解决方案


您可以先设置 ScaleX(0),然后使用 ScaleX(1) 将其设置为通过某些触发器将大小增加到原来的大小,例如单击按钮。所以代码是这样的:

[Activity(Label = "Animation", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    FloatingActionButton btn;
    Button btn2;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        btn = FindViewById<FloatingActionButton>(Resource.Id.button1);
        btn2 = FindViewById<Button>(Resource.Id.button2);
        btn2.Click += StartAnimate;
        btn.Animate().SetDuration(0).ScaleX(0);
    }

    private void StartAnimate(object sender, EventArgs e)
    {
        btn.Animate().SetStartDelay(100).TranslationX(0).TranslationY(-250).SetDuration(1000).SetInterpolator(new OvershootInterpolator(4)).ScaleX(1);
    }
}

推荐阅读