LayoutTransition实现显示、隐藏动画

 

 

  1 public class Main4Activity extends Activity {
  2 
  3     private TextView tv1;
  4     private Button button1;
  5     private RelativeLayout rlMain;
  6     private int mHeight = 0;
  7     int topHide;
  8     int topShow;
  9 
 10     @Override
 11     protected void onCreate(Bundle savedInstanceState) {
 12         super.onCreate(savedInstanceState);
 13         setContentView(R.layout.activity_main4);
 14 
 15         rlMain = (RelativeLayout) findViewById(R.id.rlMain);
 16         tv1 = (TextView) findViewById(R.id.tv1);
 17         tv1.setVisibility(View.GONE);
 18         button1 = (Button) findViewById(R.id.button1);
 19 
 20         final LayoutTransition transitioner = new LayoutTransition();
 21         transitioner.setDuration(150);
 22         rlMain.setLayoutTransition(transitioner);
 23 
 24         mHeight = getResources().getDisplayMetrics().heightPixels
 25                 - getStatusBarHeight(this);
 26 
 27         getWindow().getDecorView().addOnLayoutChangeListener(
 28                 new View.OnLayoutChangeListener() {
 29 
 30                     @Override
 31                     public void onLayoutChange(View arg0, int arg1, int arg2,
 32                             int arg3, int arg4, int arg5, int arg6, int arg7,
 33                             int arg8) {
 34 
 35                         //计算显示时y坐标
 36                         topShow = mHeight - dip2px(Main4Activity.this, 70)
 37                                 - getActionBar().getHeight();
 38                         //计算隐藏时y坐标
 39                         topHide = mHeight - getActionBar().getHeight();
 40 
 41                         // VISIBLE
 42                         ObjectAnimator customAppearingAnim = ObjectAnimator
 43                                 .ofFloat(null, "y", topHide, topShow);
 44                         customAppearingAnim
 45                                 .addListener(new AnimatorListenerAdapter() {
 46                                     public void onAnimationEnd(Animator anim) {
 47                                         View view = (View) ((ObjectAnimator) anim)
 48                                                 .getTarget();
 49                                         view.setY(topShow);
 50                                     }
 51                                 });
 52 
 53                         // GONE
 54                         ObjectAnimator customDisappearingAnim = ObjectAnimator
 55                                 .ofFloat(null, "y", topShow, topHide);
 56                         customDisappearingAnim
 57                                 .addListener(new AnimatorListenerAdapter() {
 58                                     public void onAnimationEnd(Animator anim) {
 59                                         View view = (View) ((ObjectAnimator) anim)
 60                                                 .getTarget();
 61                                         view.setY(topHide);
 62                                     }
 63                                 });
 64 
 65                         transitioner.setAnimator(LayoutTransition.APPEARING,
 66                                 customAppearingAnim);
 67 
 68                         transitioner.setAnimator(LayoutTransition.DISAPPEARING,
 69                                 customDisappearingAnim);
 70 
 71                     }
 72                 });
 73 
 74         button1.setOnClickListener(new OnClickListener() {
 75 
 76             @Override
 77             public void onClick(View arg0) {
 78 
 79                 if (tv1.getVisibility() == View.GONE) {
 80 
 81                     tv1.setVisibility(View.VISIBLE);
 82 
 83                 } else {
 84                     tv1.setVisibility(View.GONE);
 85                 }
 86 
 87             }
 88         });
 89 
 90     }
 91 
 92     /**
 93      * dip转px
 94      * @param context
 95      * @param dipValue
 96      * @return
 97      */
 98     public static int dip2px(Context context, float dipValue) {
 99         final float scale = context.getResources().getDisplayMetrics().density;
100         return (int) (dipValue * scale + 0.5f);
101     }
102 
103     /**
104      * 获取系统状态栏高度
105      * @param context
106      * @return
107      */
108     public int getStatusBarHeight(Context context) {
109         Class<?> c = null;
110         Object obj = null;
111         Field field = null;
112         int x = 0, statusBarHeight = 0;
113         try {
114             c = Class.forName("com.android.internal.R$dimen");
115             obj = c.newInstance();
116             field = c.getField("status_bar_height");
117             x = Integer.parseInt(field.get(obj).toString());
118             statusBarHeight = context.getResources().getDimensionPixelSize(x);
119             Log.v("@@@@@@", "the status bar height is : " + statusBarHeight);
120         } catch (Exception e1) {
121             e1.printStackTrace();
122         }
123         return statusBarHeight;
124     }
125 
126 }

 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/rlMain"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent" >
 6 
 7     <TextView
 8         android:id="@+id/tv1"
 9         android:layout_width="match_parent"
10         android:layout_height="70dp"
11         android:layout_alignParentBottom="true"
12         android:background="@android:color/holo_red_dark"
13         android:gravity="center"
14         android:text="@string/hello_world"
15         android:textColor="@android:color/white" />
16 
17     <Button
18         android:id="@+id/button1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:layout_alignParentLeft="true"
22         android:layout_alignParentTop="true"
23         android:layout_marginLeft="44dp"
24         android:text="Button" />
25 
26     <TextView
27         android:id="@+id/textView1"
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content"
30         android:layout_alignLeft="@+id/button1"
31         android:layout_below="@+id/button1"
32         android:maxLines="10"
33         android:scrollbars="vertical"
34         android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
35 
36 </RelativeLayout>

 

posted on 2014-11-18 10:49  屌丝迷途  阅读(1108)  评论(0编辑  收藏  举报

导航