我设计了一个简单的布局。我已经完成了没有动画的设计,但是现在我想在textview单击事件时添加动画,我不知道如何使用它。我的 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>
我的爪哇
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);
这将 aView向下移动高度:
view.animate().translationY(view.getHeight());
这将View在它被移动到其他地方后返回到它的起始位置:
view.animate().translationY(0);
您还可以使用setDuration()来设置动画的持续时间。View例如,这会在 2 秒内淡出:
setDuration()
view.animate().alpha(0.0f).setDuration(2000);
您可以根据需要组合任意数量的动画,例如,View在 0.3 秒内淡出 a 并同时将其向下移动:
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); } });