2013-09-03 20 views
14

Próbuję użyć animacji, aby wyświetlić układ na ekranie. Chodzi o to, że układ rozpocznie się od wysokości 0 i wzrośnie do 100%.Android rozwija LinearLayout za pomocą animacji

Mam poważne problemy i potrzebuję pomocy. Z jakiegoś powodu animacja nie jest wykonywana.

Oto mój plik XML animacja

<?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> 

Plik Układ jest bardzo prosty i jest zaprojektowany jako następujące

<?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> 

mojego kodu działalności jest bardzo proste, jak również

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. 
} 
} 

Dziękujemy

+0

czym polega problem? Co teraz robi twój kod? – nedaRM

+0

Mój kod nie robi absolutnie nic. Wykonuje się dobrze, ale bez animacji ... – svager

Odpowiedz

23

Ok, wymyśliłem to.

układ XML Animacja

<?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> 

plik XML szablonu

<?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> 

i moja klasa aktywny

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

} 
+0

Co oznacza metoda dialog.animate();? – anivaler

+0

Teraz nazywa się jak android.R.anim. * – delive

+0

dla animacja, jakie zmiany są wymagane w pliku animacji? –

1

animacja wydaje się nigdy nie rozpoczęła. spróbuj dodać: animation.start()?

+0

Ok, zorientowałem się, ale nie mogę opublikować pełnej odpowiedzi tutaj, ponieważ moja reputacja jest zbyt niska :( – svager

5

Najprostszym sposobem dla Android 3.0 i powyżej jest ustawienie tej właściwości dla widok, który chcesz dodać:

android:animateLayoutChanges="true"

Można również tworzyć własne animacje:

Jeśli chcesz dostarczyć niestandardowy układ animacje, utworzyć obiekt LayoutTransition i dostarczyć go do układu z metodą setLayoutTransition().

Aby uzyskać więcej informacji zobacz: http://developer.android.com/training/animation/layout.html#activity

+0

To nie jest odpowiedź, OP prosi o niestandardową animację, ale nie wyjaśnia, w jaki sposób dostarczyć niestandardową animację. –

+0

Zgadzam się z Łukaszem, przegapiłeś najważniejszą część odpowiedzi Na podstawie Twojej odpowiedzi 'view.setLayoutTransition (new LayoutTransition())' jest rozwiązaniem, ale nic nie robi. – TWiStErRob

2

Projektowanie rosnącą linearlayout w Androidzie:

Dla tych, którzy korzystają z Xamarin Mono androida:

utwórz folder anim pod zasobów.

następnie dodać animation.xml w anim folderu (grow_anim1)

w wykorzystaniu klasy działalność w ten sposób:

(w moim przypadku używam fragment)

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; 
     } 
    } 
} 
Powiązane problemy