android 属性动画应用,不知道这个效果好看,反正挺好玩的!

来源:互联网 发布:美国经济数据发布日 编辑:程序博客网 时间:2024/05/01 06:28
前两天为了看漫画,下载了一个动漫APP,打开一看,滑动的时候竟然下面的TAB跟上面的TITEL都隐藏掉了,变成全屏了,感觉好牛逼的样子哦,当初我以为是隐藏跟现实的属性那,后来一仔细看,不是,那么只有动画来实现了,看来还得用属性动画来实现:属性动画嘛就是改变了对象的属性了。不知道这个效果好不好,应该适合一些全屏阅读类的app.

先看看它的效果:


在看看咱们的效果:


好了 ,代码很少就能实现这个效果:

[java] view plaincopy
  1. package com.example.hidetitle;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.ViewPropertyAnimator;  
  9. import android.view.View.OnTouchListener;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.ListView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private boolean ismove  = true;  
  17.     private ViewPropertyAnimator animatebottom,animatetop;  
  18.     @SuppressLint("NewApi")  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         ListView mListView = (ListView) findViewById(R.id.my_listview);  
  24.         LinearLayout  tabbottom  = (LinearLayout) findViewById(R.id.tabbottom);  
  25.         LinearLayout  tabtop  = (LinearLayout) findViewById(R.id.tabtop);  
  26.         animatebottom = tabbottom.animate();  
  27.         animatetop = tabtop.animate();  
  28.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
  29.                 android.R.layout.simple_expandable_list_item_1);  
  30.         for (int i = 0; i < 100; i++) {  
  31.             adapter.add(i + "");  
  32.         }  
  33.         mListView.setAdapter(adapter);  
  34.         mListView.setOnTouchListener(new OnTouchListener() {  
  35.               
  36.             private float y;  
  37.             private boolean down = true;  
  38.             private float lasty;  
  39.   
  40.             @Override  
  41.             public boolean onTouch(View v, MotionEvent event) {  
  42.                 switch (event.getAction()) {  
  43.                 case MotionEvent.ACTION_DOWN:  
  44.                     y = event.getY();  
  45.                     if (down) {  
  46.                         lasty = y;  
  47.                     }  
  48.                     down = false;  
  49.                     break;  
  50.                 case MotionEvent.ACTION_MOVE:  
  51.                     break;  
  52.                 case MotionEvent.ACTION_UP:  
  53.                     down = true;  
  54.                     if (lasty - event.getY() < -5) {  
  55.                         onScrollReset();  
  56.                     } else if (lasty - event.getY() > 5) {  
  57.                         onScroll();  
  58.                     }  
  59.                     break;  
  60.                 }  
  61.                   
  62.                 return false;  
  63.             }  
  64.         });  
  65.     }  
  66.   
  67.       
  68.       
  69.     @SuppressLint("NewApi")  
  70.     public void onScroll() {  
  71.         if (ismove) {  
  72.             animatebottom.setDuration(500).translationY(200);  
  73.             animatetop.setDuration(500).translationY(-200);  
  74.             ismove = false;  
  75.         }  
  76.   
  77.     }  
  78.     @SuppressLint("NewApi")  
  79.     public void onScrollReset() {  
  80.         if (!ismove) {  
  81.             animatebottom.setDuration(500).translationY(0);  
  82.             animatetop.setDuration(500).translationY(0);  
  83.             ismove = true;  
  84.         }  
  85.     }  
  86.       
  87.       
  88. }  
xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent" >  
  4.   
  5.     <ListView  
  6.         android:id="@+id/my_listview"  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent" >  
  9.     </ListView>  
  10.   
  11.     <RelativeLayout  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="match_parent" >  
  14.   
  15.         <LinearLayout  
  16.             android:id="@+id/tabbottom"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="50dip"  
  19.             android:layout_alignParentBottom="true"  
  20.             android:background="#880090D9"  
  21.             android:gravity="center"  
  22.             android:orientation="vertical" >  
  23.   
  24.             <TextView  
  25.                 android:id="@+id/textView1"  
  26.                 android:layout_width="wrap_content"  
  27.                 android:layout_height="wrap_content"  
  28.                 android:text="标题"  
  29.                 android:textColor="#ffffff"  
  30.                 android:textSize="18sp" />  
  31.         </LinearLayout>  
  32.           
  33.         <LinearLayout  
  34.             android:id="@+id/tabtop"  
  35.             android:layout_width="match_parent"  
  36.             android:layout_height="50dip"  
  37.             android:layout_alignParentTop="true"  
  38.             android:background="#880090D9"  
  39.             android:gravity="center"  
  40.             android:orientation="vertical" >  
  41.   
  42.             <TextView  
  43.                 android:layout_width="wrap_content"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:text="标题"  
  46.                 android:textColor="#ffffff"  
  47.                 android:textSize="18sp" />  
  48.         </LinearLayout>  
  49.     </RelativeLayout>  
  50.   
  51. </FrameLayout>  


好玩!

android 属性动画应用,不知道这个效果好看,反正挺好玩的!

分类: Android知识点 132人阅读 评论(4) 收藏 举报
android标题栏滑动隐藏属性动画ViewPropertyAnimator滑动隐藏

前两天为了看漫画,下载了一个动漫APP,打开一看,滑动的时候竟然下面的TAB跟上面的TITEL都隐藏掉了,变成全屏了,感觉好牛逼的样子哦,当初我以为是隐藏跟现实的属性那,后来一仔细看,不是,那么只有动画来实现了,看来还得用属性动画来实现:属性动画嘛就是改变了对象的属性了。不知道这个效果好不好,应该适合一些全屏阅读类的app.

先看看它的效果:


在看看咱们的效果:


好了 ,代码很少就能实现这个效果:

[java] view plaincopy
  1. package com.example.hidetitle;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.ViewPropertyAnimator;  
  9. import android.view.View.OnTouchListener;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.ListView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private boolean ismove  = true;  
  17.     private ViewPropertyAnimator animatebottom,animatetop;  
  18.     @SuppressLint("NewApi")  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         ListView mListView = (ListView) findViewById(R.id.my_listview);  
  24.         LinearLayout  tabbottom  = (LinearLayout) findViewById(R.id.tabbottom);  
  25.         LinearLayout  tabtop  = (LinearLayout) findViewById(R.id.tabtop);  
  26.         animatebottom = tabbottom.animate();  
  27.         animatetop = tabtop.animate();  
  28.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
  29.                 android.R.layout.simple_expandable_list_item_1);  
  30.         for (int i = 0; i < 100; i++) {  
  31.             adapter.add(i + "");  
  32.         }  
  33.         mListView.setAdapter(adapter);  
  34.         mListView.setOnTouchListener(new OnTouchListener() {  
  35.               
  36.             private float y;  
  37.             private boolean down = true;  
  38.             private float lasty;  
  39.   
  40.             @Override  
  41.             public boolean onTouch(View v, MotionEvent event) {  
  42.                 switch (event.getAction()) {  
  43.                 case MotionEvent.ACTION_DOWN:  
  44.                     y = event.getY();  
  45.                     if (down) {  
  46.                         lasty = y;  
  47.                     }  
  48.                     down = false;  
  49.                     break;  
  50.                 case MotionEvent.ACTION_MOVE:  
  51.                     break;  
  52.                 case MotionEvent.ACTION_UP:  
  53.                     down = true;  
  54.                     if (lasty - event.getY() < -5) {  
  55.                         onScrollReset();  
  56.                     } else if (lasty - event.getY() > 5) {  
  57.                         onScroll();  
  58.                     }  
  59.                     break;  
  60.                 }  
  61.                   
  62.                 return false;  
  63.             }  
  64.         });  
  65.     }  
  66.   
  67.       
  68.       
  69.     @SuppressLint("NewApi")  
  70.     public void onScroll() {  
  71.         if (ismove) {  
  72.             animatebottom.setDuration(500).translationY(200);  
  73.             animatetop.setDuration(500).translationY(-200);  
  74.             ismove = false;  
  75.         }  
  76.   
  77.     }  
  78.     @SuppressLint("NewApi")  
  79.     public void onScrollReset() {  
  80.         if (!ismove) {  
  81.             animatebottom.setDuration(500).translationY(0);  
  82.             animatetop.setDuration(500).translationY(0);  
  83.             ismove = true;  
  84.         }  
  85.     }  
  86.       
  87.       
  88. }  
xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent" >  
  4.   
  5.     <ListView  
  6.         android:id="@+id/my_listview"  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent" >  
  9.     </ListView>  
  10.   
  11.     <RelativeLayout  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="match_parent" >  
  14.   
  15.         <LinearLayout  
  16.             android:id="@+id/tabbottom"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="50dip"  
  19.             android:layout_alignParentBottom="true"  
  20.             android:background="#880090D9"  
  21.             android:gravity="center"  
  22.             android:orientation="vertical" >  
  23.   
  24.             <TextView  
  25.                 android:id="@+id/textView1"  
  26.                 android:layout_width="wrap_content"  
  27.                 android:layout_height="wrap_content"  
  28.                 android:text="标题"  
  29.                 android:textColor="#ffffff"  
  30.                 android:textSize="18sp" />  
  31.         </LinearLayout>  
  32.           
  33.         <LinearLayout  
  34.             android:id="@+id/tabtop"  
  35.             android:layout_width="match_parent"  
  36.             android:layout_height="50dip"  
  37.             android:layout_alignParentTop="true"  
  38.             android:background="#880090D9"  
  39.             android:gravity="center"  
  40.             android:orientation="vertical" >  
  41.   
  42.             <TextView  
  43.                 android:layout_width="wrap_content"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:text="标题"  
  46.                 android:textColor="#ffffff"  
  47.                 android:textSize="18sp" />  
  48.         </LinearLayout>  
  49.     </RelativeLayout>  
  50.   
  51. </FrameLayout>  


好玩!


0 0
原创粉丝点击