Android添加简单的animation,而setvisibility(view.Gone)

我已经devise了一个简单的布局。我已经完成了没有animation的devise,但现在我想添加animation,当textview单击事件,我不知道如何使用它。 我的xmldevise看起来不错吗? 任何build议,将不胜感激。

我的XML

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:longClickable="false" android:orientation="vertical" android:weightSum="16" > <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:orientation="vertical" android:background="#00DDA0" android:layout_weight="3" > </LinearLayout> <TextView android:id="@+id/Information1" android:layout_width="match_parent" android:layout_height="1dp" android:text="Child Information" android:background="#0390BE" android:layout_weight="0.75" android:textColor="#FFFFFF" android:layout_gravity="center|fill_horizontal"/> <LinearLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="8.5" android:background="#BBBBBB" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" /> </LinearLayout> <TextView android:id="@+id/Information2" android:layout_width="match_parent" android:layout_height="0dp" android:text="Parent Information" android:background="#0390BE" android:layout_weight="0.75" android:textColor="#FFFFFF" android:layout_gravity="center|fill_horizontal"/> <LinearLayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="0dp" android:orientation="vertical" android:background="#BBBBBB" android:layout_weight="8.5" > <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" /> </LinearLayout> <TextView android:id="@+id/Information3" android:layout_width="match_parent" android:layout_height="0dp" android:text="Siblings" android:background="#0390BE" android:layout_weight="0.75" android:textColor="#FFFFFF" android:layout_gravity="center|fill_horizontal"/> <LinearLayout android:id="@+id/layout3" android:layout_width="fill_parent" android:layout_height="0dp" android:orientation="vertical" android:background="#BBBBBB" android:layout_weight="8.5" > <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" /> </LinearLayout> <TextView android:id="@+id/Information4" android:layout_width="match_parent" android:layout_height="0dp" android:text="Teacher Information" android:background="#0390BE" android:layout_weight="0.75" android:textColor="#FFFFFF" android:layout_gravity="center|fill_horizontal"/> <LinearLayout android:id="@+id/layout4" android:layout_width="fill_parent" android:layout_height="0dp" android:orientation="vertical" android:background="#BBBBBB" android:layout_weight="8.5" > <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" /> </LinearLayout> <TextView android:id="@+id/Information5" android:layout_width="match_parent" android:layout_height="0dp" android:text="Grade Information" android:background="#0390BE" android:layout_weight="0.75" android:textColor="#FFFFFF" android:layout_gravity="center|fill_horizontal"/> <LinearLayout android:id="@+id/layout5" android:layout_width="fill_parent" android:layout_height="0dp" android:orientation="vertical" android:background="#BBBBBB" android:layout_weight="8.5" > <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" /> </LinearLayout> <TextView android:id="@+id/Information6" android:layout_width="match_parent" android:layout_height="0dp" android:text="Health Information" android:background="#0390BE" android:layout_weight="0.75" android:textColor="#FFFFFF" android:layout_gravity="center|fill_horizontal"/> <LinearLayout android:id="@+id/layout6" android:layout_width="fill_parent" android:layout_height="0dp" android:orientation="vertical" android:background="#BBBBBB" android:layout_weight="8.5" > <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" android:layout_weight="8.5" /> </LinearLayout> </LinearLayout> 

我的Java

 public class Certify_Info extends Activity { private static TextView tv2,tv3,tv5,tv6,tv4,tv1; private static LinearLayout l1,l2,l3,l4,l5,l6; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_certify__info); tv1=(TextView) findViewById(R.id.Information1); tv2=(TextView) findViewById(R.id.Information2); tv3=(TextView) findViewById(R.id.Information3); tv4=(TextView) findViewById(R.id.Information4); tv5=(TextView) findViewById(R.id.Information5); tv6=(TextView) findViewById(R.id.Information6); l1=(LinearLayout) findViewById(R.id.layout1); l2=(LinearLayout) findViewById(R.id.layout2); l3=(LinearLayout) findViewById(R.id.layout3); l4=(LinearLayout) findViewById(R.id.layout4); l5=(LinearLayout) findViewById(R.id.layout5); l6=(LinearLayout) findViewById(R.id.layout6); l2.setVisibility(View.GONE); l3.setVisibility(View.GONE); l4.setVisibility(View.GONE); l5.setVisibility(View.GONE); l6.setVisibility(View.GONE); tv1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub l2.setVisibility(View.GONE); l3.setVisibility(View.GONE); l4.setVisibility(View.GONE); l5.setVisibility(View.GONE); l6.setVisibility(View.GONE); l1.setVisibility(View.VISIBLE); } }); tv2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub l1.setVisibility(View.GONE); l3.setVisibility(View.GONE); l4.setVisibility(View.GONE); l5.setVisibility(View.GONE); l6.setVisibility(View.GONE); l2.setVisibility(View.VISIBLE); } }); tv3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub l1.setVisibility(View.GONE); l2.setVisibility(View.GONE); l4.setVisibility(View.GONE); l5.setVisibility(View.GONE); l6.setVisibility(View.GONE); l3.setVisibility(View.VISIBLE); } }); tv4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub l1.setVisibility(View.GONE); l2.setVisibility(View.GONE); l3.setVisibility(View.GONE); l4.setVisibility(View.GONE); l5.setVisibility(View.GONE); l6.setVisibility(View.GONE); l4.setVisibility(View.VISIBLE); } }); tv5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub l1.setVisibility(View.GONE); l2.setVisibility(View.GONE); l3.setVisibility(View.GONE); l4.setVisibility(View.GONE); l6.setVisibility(View.GONE); l5.setVisibility(View.VISIBLE); } }); tv6.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub l1.setVisibility(View.GONE); l2.setVisibility(View.GONE); l3.setVisibility(View.GONE); l4.setVisibility(View.GONE); l5.setVisibility(View.GONE); l6.setVisibility(View.VISIBLE); } }); } } 

你可以做两件事来添加animation,首先你可以让android为你animation布局的变化。 这样,每次你改变布局的东西,如改变视图的可见性或查看位置,android会自动创build淡入淡出/过渡animation。 使用该设置

 android:animateLayoutChanges="true" 

在布局的根节点上。

你的第二个select是手动添加animation。 为此,我build议您使用Android 3.0(Honeycomb)中引入的新animationAPI。 我可以举几个例子:

这淡化了一个View

 view.animate().alpha(0.0f); 

这消失在:

 view.animate().alpha(1.0f); 

这会将View向下移动一个高度:

 view.animate().translationY(view.getHeight()); 

这将View移动到其他位置之后的起始位置:

 view.animate().translationY(0); 

您也可以使用setDuration()来设置animation的持续时间。 例如,这会在2秒内淡出一个View

 view.animate().alpha(0.0f).setDuration(2000); 

您可以将任意数量的animation组合成任意一个,例如,这会淡化一个View并在0.3秒的时间内将其同时向下移动:

 view.animate() .translationY(view.getHeight()) .alpha(0.0f) .setDuration(300); 

您还可以为animation分配侦听器,并对各种事件做出反应。 就像animation开始时,结束或重复时一样。通过使用抽象类AnimatorListenerAdapter您不必一次性实现AnimatorListener所有callback,而只需要实现所需的callback。 这使得代码更具可读性。 例如,下面的代码淡出一个View在0.3秒(300毫秒)的时间内将其向下移动其高度,当animation完成时,其可见性被设置为View.GONE

 view.animate() .translationY(view.getHeight()) .alpha(0.0f) .setDuration(300) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); view.setVisibility(View.GONE); } }); 

请检查这个链接。 这将允许animation像L2R,R2L,T2B,B2Tanimation。

此代码从左到右显示animation

 TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0); animate.setDuration(500); animate.setFillAfter(true); view.startAnimation(animate); view.setVisibility(View.GONE); 

如果你想从R2L做到这一点,然后使用

 TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0); 

从上到下如

 TranslateAnimation animate = new TranslateAnimation(0,0,0,view.getHeight()); 

和副反之亦然。

基于@Xaver Kapeller的回答,我想出了一种在屏幕上出现新视图(还有animation来隐藏它们)时创build滚动animation的方法。

从这个状态开始:

  • 按键
  • 最后一个button

  • 按键
  • button1
  • button2
  • button3
  • button4
  • 最后一个button

反之亦然。

因此,当用户点击第一个button时,会使用淡入淡出的animation显示元素“button1”,“button2”,“button3”和“button4”,元素“上一个button”将向下移动直到结束。 布局的高度也会改变,允许正确使用滚动视图。

这是用animation显示元素的代码:

 private void showElements() { // Precondition if (areElementsVisible()) { Log.w(TAG, "The view is already visible. Nothing to do here"); return; } // Animate the hidden linear layout as visible and set // the alpha as 0.0. Otherwise the animation won't be shown mHiddenLinearLayout.setVisibility(View.VISIBLE); mHiddenLinearLayout.setAlpha(0.0f); mHiddenLinearLayout .animate() .setDuration(ANIMATION_TRANSITION_TIME) .alpha(1.0f) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); updateShowElementsButton(); mHiddenLinearLayout.animate().setListener(null); } }) ; mLastButton .animate() .setDuration(ANIMATION_TRANSITION_TIME) .translationY(mHiddenLinearLayoutHeight); // Update the high of all the elements relativeLayout LayoutParams layoutParams = mAllElementsRelativeLayout.getLayoutParams(); // TODO: Add vertical margins layoutParams.height = mLastButton.getHeight() + mHiddenLinearLayoutHeight; } 

这是隐藏animation元素的代码:

 private void hideElements() { // Precondition if (!areElementsVisible()) { Log.w(TAG, "The view is already non-visible. Nothing to do here"); return; } // Animate the hidden linear layout as visible and set mHiddenLinearLayout .animate() .setDuration(ANIMATION_TRANSITION_TIME) .alpha(0.0f) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { Log.v(TAG, "Animation ended. Set the view as gone"); super.onAnimationEnd(animation); mHiddenLinearLayout.setVisibility(View.GONE); // Hack: Remove the listener. So it won't be executed when // any other animation on this view is executed mHiddenLinearLayout.animate().setListener(null); updateShowElementsButton(); } }) ; mLastButton .animate() .setDuration(ANIMATION_TRANSITION_TIME) .translationY(0); // Update the high of all the elements relativeLayout LayoutParams layoutParams = mAllElementsRelativeLayout.getLayoutParams(); // TODO: Add vertical margins layoutParams.height = mLastButton.getHeight(); } 

请注意,隐藏animation的方法有一个简单的方法。 在animation侦听器mHiddenLinearLayout上,我不得不使用下面的方法去除侦听器本身:

 mHiddenLinearLayout.animate().setListener(null); 

这是因为一旦一个animation监听器被附加到一个视图上,下一次在这个视图中执行任何animation时,监听器也将被执行。 这可能是animation侦听器中的一个错误。

该项目的源代码在GitHub上: https : //github.com/jiahaoliuliu/ViewsAnimated

快乐的编码!

更新 :对于任何附加到视图的侦听器,应在animation结束后将其删除。 这是通过使用

 view.animate().setListener(null);