17

我正在尝试使用动画使布局出现在屏幕上。这个想法是布局将从高度 0 开始并增长到 100%。

我对此有真正的麻烦,需要一些帮助。由于某种原因,没有执行动画。

这是我的动画 XML 文件

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1"
        android:fromYScale="1.0"
        android:toYScale="1.0"
        android:fillAfter="false"
         />

</set>

布局文件非常基础,设计如下

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
        android:id="@+id/dialog"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:background="@drawable/border">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Phone"
            android:id="@+id/textView"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Address"
            android:id="@+id/textView1"/>
    <Button android:id="@+id/btn1"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Action 1"
            />
    <Button android:id="@+id/btn2"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Action 2"
            />
</LinearLayout>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate"
        android:id="@+id/btnAnimate" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
        android:onClick="animate"/>
</RelativeLayout>

我的活动代码也很基本

public class MyActivity extends Activity implements Animation.AnimationListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 }

public void animate(View view){
    LinearLayout dialog   = (LinearLayout)findViewById(R.id.dialog);
    dialog.setVisibility(LinearLayout.VISIBLE);
    Animation animation   =    AnimationUtils.loadAnimation(this, R.anim.anim);
    Log.i("animate","Begin Animation");
    animation.reset();
  //  animation.setFillAfter(true);
    animation.setAnimationListener(this);
    dialog.setAnimation(null);
    Log.i("animate","End Animation");
}

@Override
public void onAnimationStart(Animation animation) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onAnimationEnd(Animation animation) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onAnimationRepeat(Animation animation) {
    //To change body of implemented methods use File | Settings | File Templates.
}
}

谢谢

4

4 回答 4

25

好的,我想通了。

动画 XML 布局

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:fillEnabled="true"
 android:fillAfter="true">
 <scale

        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:fillAfter="false"
         />

</set>

布局 XML 文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

  <LinearLayout
        android:id="@+id/dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:visibility="invisible"
        android:background="@drawable/border">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Phone"
            android:id="@+id/textView"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Address"
            android:id="@+id/textView1"/>
    <Button android:id="@+id/btn1"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Action 1"
            />
    <Button android:id="@+id/btn2"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Action 2"
            />
   </LinearLayout>
   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate"
        android:id="@+id/btnAnimate" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
        android:onClick="animate"/>
</RelativeLayout>

和我的活动课

public class MyActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 }

public void animate(View view){
    LinearLayout dialog   = (LinearLayout)findViewById(R.id.dialog);
    dialog.setVisibility(LinearLayout.VISIBLE);
    Animation animation   =    AnimationUtils.loadAnimation(this, R.anim.anim);
    animation.setDuration(500);
    dialog.setAnimation(animation);
    dialog.animate();
    animation.start();
 }

}
于 2013-09-03T16:55:37.090 回答
7

对于 Android 3.0 及更高版本,最简单的方法是为要添加子项的视图设置此属性:

android:animateLayoutChanges="true"

您还可以创建自己的动画:

如果要提供自定义布局动画,请创建一个 LayoutTransition 对象并使用 setLayoutTransition() 方法将其提供给布局。

有关更多信息,请参阅:http: //developer.android.com/training/animation/layout.html#activity

于 2014-06-02T12:15:01.990 回答
2

在android中设计一个不断增长的线性布局:

对于使用 Xamarin 的 Mono android 的用户:

anim在资源下创建一个文件夹。

然后添加文件夹(grow_anim1 animation.xmlanim

在活动类中使用这种方式:

(就我而言,我使用的是片段)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Views.Animations;
namespace BehnoudAndroidApp {
    public class StartPageFragment : Fragment{
        public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2){
            var rootView = p0.Inflate(Resource.Layout.StartPageLayout, p1, false);

            LinearLayout menu1 = rootView.FindViewById<LinearLayout>(Resource.Id.linearlayout1);

            Animation animation1 = AnimationUtils.LoadAnimation(this.Activity, Resource.Animation.grow_anim1);

            animation1.Duration = 5000;

            menu1.Click += delegate { menu1.StartAnimation(animation1); };

            return rootView;
        }
    }
}
于 2015-01-13T12:33:33.007 回答
1

似乎动画从未开始。尝试添加:

animation.start()
于 2013-09-03T00:48:36.810 回答