我设计了一个简单的布局,没有动画就完成了设计,但是现在我想在textview click事件时添加动画,但我不知道如何使用它。我的xml设计看起来不错还是不好?任何建议,将不胜感激。
我的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); } }); } }
您可以做两件事来添加动画,首先可以让android为您设置布局动画。这样,每次您更改布局中的某些内容(例如更改视图可见性或视图位置)时,android都会自动创建淡入淡出/过渡动画。要使用那套
android:animateLayoutChanges="true"
在布局的根节点上。
您的第二个选择是手动添加动画。为此,我建议您使用Android 3.0(Honeycomb)中引入的新动画API。我可以举几个例子:
这淡出了View:
View
view.animate().alpha(0.0f);
这使它淡入:
view.animate().alpha(1.0f);
这会将View其高度向下移动:
view.animate().translationY(view.getHeight());
将View其移动到其他位置后,它将返回到其初始位置:
view.animate().translationY(0);
您也可以使用setDuration()设置动画的持续时间。例如,这会View在2秒内逐渐消失:
setDuration()
view.animate().alpha(0.0f).setDuration(2000);
而且,您可以根据需要组合任意数量的动画,例如,它会淡出a View并在0.3秒内将其同时向下移动:
view.animate() .translationY(view.getHeight()) .alpha(0.0f) .setDuration(300);
您还可以为动画分配一个侦听器,并对各种事件做出反应。就像动画开始时,动画结束时或重复时一样。通过使用抽象类,AnimatorListenerAdapter您不必AnimatorListener一次实现所有的回调,而只需实现所需的那些。这使代码更具可读性。例如,以下代码淡出并View在0.3秒(300毫秒)的时间内将其向下移动高度,动画完成后,其可见性设置为View.GONE。
AnimatorListenerAdapter
AnimatorListener
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); } });