(三)Android仿微信—主页面实现篇

来源:互联网 发布:nginx 安装禅道 编辑:程序博客网 时间:2024/05/02 03:05

这一篇将讲述如何构建主页面,先看一下微信主页面的截图


  从截图中可以看出,它的菜单是在程序的底部,当我们选择一个按钮后,它的颜色会发生变化,好像有灯在亮,这个实现起来比较简单,可以有多种方式供我们选择,TabActivity或者tabwidget+radiobutton或者activitygroup+radiobutton或者activitygroup+gridview或者activitygroup+grally等都可以,按钮的变化可以使用selector用两张图片来控制。

关于activitygroup,大家可以看一下这个图片:


先以tabwidget为例,代码如下:

[java] view plaincopy
  1. import android.app.TabActivity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.Window;  
  6. import android.widget.RadioGroup;  
  7. import android.widget.RadioGroup.OnCheckedChangeListener;  
  8. import android.widget.TabHost;  
  9. import android.widget.TextView;  
  10.   
  11. public class MainActivity extends TabActivity {  
  12.     /** Called when the activity is first created. */  
  13.     private TabHost tabHost;  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  19.         setContentView(R.layout.main);  
  20.                
  21.         tabHost=this.getTabHost();  
  22.         TabHost.TabSpec spec;  
  23.         Intent intent;  
  24.   
  25.         intent=new Intent().setClass(this, AddExamActivity.class);  
  26.         spec=tabHost.newTabSpec("微信").setIndicator("微信").setContent(intent);  
  27.         tabHost.addTab(spec);  
  28.           
  29.         intent=new Intent().setClass(this,MyExamActivity.class);  
  30.         spec=tabHost.newTabSpec("通讯录").setIndicator("通讯录").setContent(intent);  
  31.         tabHost.addTab(spec);  
  32.           
  33.         intent=new Intent().setClass(this, MyMessageActivity.class);  
  34.         spec=tabHost.newTabSpec("朋友们").setIndicator("朋友们").setContent(intent);  
  35.         tabHost.addTab(spec);  
  36.           
  37.         intent=new Intent().setClass(this, Activity.class);  
  38.         spec=tabHost.newTabSpec("设置").setIndicator("设置").setContent(intent);  
  39.         tabHost.addTab(spec);  
  40.   
  41.         intent=new Intent().setClass(this, SettingActivity.class);  
  42.         spec=tabHost.newTabSpec("设置").setIndicator("设置").setContent(intent);  
  43.         tabHost.addTab(spec);  
  44.   
  45.         tabHost.setCurrentTab(1);         
  46.     }    
  47. }  
xml布局文件如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost   
  3.     android:id="@android:id/tabhost" //id必须为:tabhost  
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="fill_parent"  
  6.     xmlns:android="http://schemas.android.com/apk/res/android">  
  7.       
  8.     <LinearLayout   
  9.         android:orientation="vertical"   
  10.         android:layout_width="fill_parent"   
  11.         android:layout_height="fill_parent">  
  12.           
  13.         <FrameLayout   
  14.             android:id="@android:id/tabcontent" //id必须为:tabcontent  
  15.             android:layout_width="fill_parent"   
  16.             android:layout_height="0.0dip"   
  17.             android:layout_weight="1.0" />  
  18.           
  19.         <TabWidget   
  20.             android:id="@android:id/tabs" //id必须为:tabs      
  21.             android:layout_width="fill_parent"   
  22.             android:layout_height="wrap_content"   
  23.             android:layout_weight="0.0" />  
  24.               
  25.     </LinearLayout>        这些id都是系统的,只有这样,系统才会找到他们正确辨认。  
  26.       
  27. </TabHost>  
  再就是点击button的切换效果了,这里需要使用selector来实现,如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/tab_weixin_pressed" />//点击后的绿色效果  
  5.     <item android:drawable="@drawable/tab_weixin_normal" />//未点击的正常效果  
  6. </selector>  
  这样就利用tabwidget实现了最基础的菜单布局,但是相信很多人都知道,使用tabwidget需要消耗比较大的内存,后来就有人使用activitygroup和其他的组件结合使用,如上举例。这里就不再介绍了,感兴趣的朋友可以去查阅资料。下面我们使用另外一个方法,这种方法相对来说更加优雅。为什么这么说,因为viewpager这个类相信大家都不陌生了,其实官方微信是不支持滑动页面的,但是利用viewpager这个类,我们也可以既可以点击又可以滑动的切换页面。而且我们也不使用selector来实现button的改变,而是利用一个动画来实现,这个工程就是绿色图片来覆盖各个button,呈现出一个绿色的效果。底部菜单也不是使用耗费资源的tabwidget而是利用布局来自定义的,xml文件如下,一眼就可以明白:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/mainweixin"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     android:background="#eee" >    
  8.   
  9.     <RelativeLayout  
  10.         android:id="@+id/main_bottom"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="55dp"  
  13.         android:layout_alignParentBottom="true"  
  14.         android:orientation="vertical"  
  15.         android:background="@drawable/bottom_bar"  
  16.         >               
  17.         
  18.         <ImageView  
  19.             android:id="@+id/img_tab_now"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"                      
  22.             android:scaleType="matrix"  
  23.             android:layout_gravity="bottom"               
  24.             android:layout_alignParentBottom="true"  
  25.             android:src="@drawable/tab_bg" />   //动画所用图片,绿色的         
  26.            
  27.          <LinearLayout    //底部菜单的父布局  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_alignParentBottom="true"  
  31.             android:paddingBottom="2dp"              
  32.             >  
  33.               
  34.             <LinearLayout  
  35.                 android:layout_width="wrap_content"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:gravity="center_horizontal"  
  38.                 android:orientation="vertical"  
  39.                 android:layout_weight="1">                     
  40.                 <ImageView  
  41.                     android:id="@+id/img_weixin"  
  42.                     android:layout_width="wrap_content"  
  43.                     android:layout_height="wrap_content"                      
  44.                     android:scaleType="matrix"  
  45.                     android:clickable="true"  
  46.                     android:src="@drawable/tab_weixin_pressed" />  
  47.                 <TextView  
  48.                     android:layout_width="wrap_content"  
  49.                     android:layout_height="wrap_content"  
  50.                     android:text="微信"  
  51.                     android:textColor="#fff"  
  52.                     android:textSize="12sp" />                  
  53.              </LinearLayout>  
  54.              <LinearLayout  
  55.                 android:layout_width="wrap_content"  
  56.                 android:layout_height="wrap_content"  
  57.                 android:gravity="center_horizontal"  
  58.                 android:orientation="vertical"  
  59.                 android:layout_weight="1">                     
  60.                 <ImageView  
  61.                     android:id="@+id/img_address"  
  62.                     android:layout_width="wrap_content"  
  63.                     android:layout_height="wrap_content"                      
  64.                     android:scaleType="matrix"  
  65.                     android:clickable="true"  
  66.                     android:src="@drawable/tab_address_normal" />  
  67.                 <TextView  
  68.                     android:layout_width="wrap_content"  
  69.                     android:layout_height="wrap_content"  
  70.                     android:text="通讯录"  
  71.                     android:textColor="#fff"  
  72.                     android:textSize="12sp" />                  
  73.              </LinearLayout>  
  74.              <LinearLayout  
  75.                 android:layout_width="wrap_content"  
  76.                 android:layout_height="wrap_content"  
  77.                 android:gravity="center_horizontal"  
  78.                 android:orientation="vertical"  
  79.                 android:layout_weight="1">                     
  80.                 <ImageView  
  81.                     android:id="@+id/img_friends"  
  82.                     android:layout_width="wrap_content"  
  83.                     android:layout_height="wrap_content"                      
  84.                     android:scaleType="matrix"  
  85.                     android:clickable="true"  
  86.                     android:src="@drawable/tab_find_frd_normal" />  
  87.                 <TextView  
  88.                     android:layout_width="wrap_content"  
  89.                     android:layout_height="wrap_content"  
  90.                     android:text="朋友们"  
  91.                     android:textColor="#fff"  
  92.                     android:textSize="12sp" />                  
  93.              </LinearLayout>  
  94.                
  95.              <LinearLayout  
  96.                 android:layout_width="wrap_content"  
  97.                 android:layout_height="wrap_content"  
  98.                 android:gravity="center_horizontal"  
  99.                 android:orientation="vertical"  
  100.                 android:layout_weight="1">                     
  101.                 <ImageView  
  102.                     android:id="@+id/img_settings"  
  103.                     android:layout_width="wrap_content"  
  104.                     android:layout_height="wrap_content"                      
  105.                     android:scaleType="matrix"  
  106.                     android:clickable="true"  
  107.                     android:src="@drawable/tab_settings_normal" />  
  108.                 <TextView  
  109.                     android:layout_width="wrap_content"  
  110.                     android:layout_height="wrap_content"  
  111.                     android:text="设置"  
  112.                     android:textColor="#fff"  
  113.                     android:textSize="12sp" />                  
  114.              </LinearLayout>          
  115.             
  116.         </LinearLayout>       
  117.          
  118.     </RelativeLayout>  
  119.   
  120.     <LinearLayout  //viewpager类,用来切换页面  
  121.         android:layout_width="fill_parent"  
  122.         android:layout_height="wrap_content"   
  123.         android:layout_alignParentTop="true"  
  124.         android:layout_above="@id/main_bottom"         
  125.         android:orientation="vertical" >  
  126.           
  127.         <android.support.v4.view.ViewPager  
  128.             android:id="@+id/tabpager"  
  129.             android:layout_width="wrap_content"  
  130.             android:layout_height="wrap_content"  
  131.             android:layout_gravity="center" >   
  132.         </android.support.v4.view.ViewPager>    
  133.     </LinearLayout>  
  134.   
  135. </RelativeLayout>  
 如图:


  所以,主页面的代码主要是这样的,一方面要使用viewpager来实现滑动页面,另一方面要实现自定义菜单改变颜色的动画效果。代码如下:

  

[java] view plaincopy
  1. import java.util.ArrayList;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.support.v4.view.PagerAdapter;  
  6. import android.support.v4.view.ViewPager;  
  7. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  8. import android.view.Display;  
  9. import android.view.Gravity;  
  10. import android.view.KeyEvent;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.WindowManager;  
  14. import android.view.WindowManager.LayoutParams;  
  15. import android.view.animation.Animation;  
  16. import android.view.animation.TranslateAnimation;  
  17. import android.widget.ImageView;  
  18. import android.widget.LinearLayout;  
  19. import android.widget.PopupWindow;  
  20.   
  21. public class MainWeixin extends Activity {  
  22.   
  23.     public static MainWeixin instance = null;  
  24.   
  25.     private ViewPager mTabPager;//声明对象  
  26.     private ImageView mTabImg;// 动画图片  
  27.     private ImageView mTab1, mTab2, mTab3, mTab4;  
  28.     private int zero = 0;// 动画图片偏移量  
  29.     private int currIndex = 0;// 当前页卡编号  
  30.     private int one;// 单个水平动画位移  
  31.     private int two;  
  32.     private int three;  
  33.     private LinearLayout mClose;  
  34.     private LinearLayout mCloseBtn;  
  35.     private View layout;  
  36.     private boolean menu_display = false;  
  37.     private PopupWindow menuWindow;  
  38.     private LayoutInflater inflater;  
  39.   
  40.     @Override  
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.main_weixin);  
  44.         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);// 启动activity时不自动弹出软键盘  
  45.         instance = this;  
  46.   
  47.         mTabPager = (ViewPager) findViewById(R.id.tabpager);  
  48.         mTabPager.setOnPageChangeListener(new MyOnPageChangeListener());  
  49.   
  50.         mTab1 = (ImageView) findViewById(R.id.img_weixin);  
  51.         mTab2 = (ImageView) findViewById(R.id.img_address);  
  52.         mTab3 = (ImageView) findViewById(R.id.img_friends);  
  53.         mTab4 = (ImageView) findViewById(R.id.img_settings);  
  54.   
  55.         mTabImg = (ImageView) findViewById(R.id.img_tab_now);//动画图片  
  56.   
  57.         mTab1.setOnClickListener(new MyOnClickListener(0));  
  58.         mTab2.setOnClickListener(new MyOnClickListener(1));  
  59.         mTab3.setOnClickListener(new MyOnClickListener(2));  
  60.         mTab4.setOnClickListener(new MyOnClickListener(3));  
  61.   
  62.         Display currDisplay = getWindowManager().getDefaultDisplay();// 获取屏幕当前分辨率  
  63.         int displayWidth = currDisplay.getWidth();  
  64.         one = displayWidth / 4// 设置水平动画平移大小  
  65.         two = one * 2;  
  66.         three = one * 3;  
  67.   
  68.         // 将要分页显示的View装入数组中  
  69.         LayoutInflater mLi = LayoutInflater.from(this);  
  70.         View view1 = mLi.inflate(R.layout.main_tab_weixin, null);  
  71.         View view2 = mLi.inflate(R.layout.main_tab_address, null);  
  72.         View view3 = mLi.inflate(R.layout.main_tab_friends, null);  
  73.         View view4 = mLi.inflate(R.layout.main_tab_settings, null);  
  74.   
  75.         // 每个页面的view数据  
  76.         final ArrayList<View> views = new ArrayList<View>();  
  77.         views.add(view1);  
  78.         views.add(view2);  
  79.         views.add(view3);  
  80.         views.add(view4);  
  81.   
  82.         // 填充ViewPager的数据适配器  
  83.         PagerAdapter mPagerAdapter = new PagerAdapter() {  
  84.   
  85.             @Override  
  86.             public boolean isViewFromObject(View arg0, Object arg1) {  
  87.                 return arg0 == arg1;  
  88.             }  
  89.   
  90.             @Override  
  91.             public int getCount() {  
  92.                 return views.size();  
  93.             }  
  94.   
  95.             @Override  
  96.             public void destroyItem(View container, int position, Object object) {  
  97.                 ((ViewPager) container).removeView(views.get(position));  
  98.             }  
  99.   
  100.             // @Override  
  101.             // public CharSequence getPageTitle(int position) {  
  102.             // return titles.get(position);  
  103.             // }  
  104.   
  105.             @Override  
  106.             public Object instantiateItem(View container, int position) {  
  107.                 ((ViewPager) container).addView(views.get(position));  
  108.                 return views.get(position);  
  109.             }  
  110.         };  
  111.   
  112.         mTabPager.setAdapter(mPagerAdapter);  
  113.     }  
  114.   
  115.     /** 
  116.      * 头标点击监听 
  117.      */  
  118.     public class MyOnClickListener implements View.OnClickListener {  
  119.         private int index = 0;  
  120.   
  121.         public MyOnClickListener(int i) {  
  122.             index = i;  
  123.         }  
  124.   
  125.         public void onClick(View v) {  
  126.             mTabPager.setCurrentItem(index);  
  127.         }  
  128.     };  
  129.   
  130.     /* 
  131.      * 页卡切换监听(原作者:D.Winter) 
  132.      */  
  133.     public class MyOnPageChangeListener implements OnPageChangeListener {  
  134.         public void onPageSelected(int arg0) {  
  135.             Animation animation = null;  
  136.             switch (arg0) {  
  137.             case 0:  
  138.                 mTab1.setImageDrawable(getResources().getDrawable(  
  139.                         R.drawable.tab_weixin_pressed));  
  140.                 if (currIndex == 1) {  
  141.                     animation = new TranslateAnimation(one, 000);  
  142.                     mTab2.setImageDrawable(getResources().getDrawable(  
  143.                             R.drawable.tab_address_normal));  
  144.                 } else if (currIndex == 2) {  
  145.                     animation = new TranslateAnimation(two, 000);  
  146.                     mTab3.setImageDrawable(getResources().getDrawable(  
  147.                             R.drawable.tab_find_frd_normal));  
  148.                 } else if (currIndex == 3) {  
  149.                     animation = new TranslateAnimation(three, 000);  
  150.                     mTab4.setImageDrawable(getResources().getDrawable(  
  151.                             R.drawable.tab_settings_normal));  
  152.                 }  
  153.                 break;  
  154.             case 1:  
  155.                 mTab2.setImageDrawable(getResources().getDrawable(  
  156.                         R.drawable.tab_address_pressed));  
  157.                 if (currIndex == 0) {  
  158.                     animation = new TranslateAnimation(zero, one, 00);  
  159.                     mTab1.setImageDrawable(getResources().getDrawable(  
  160.                             R.drawable.tab_weixin_normal));  
  161.                 } else if (currIndex == 2) {  
  162.                     animation = new TranslateAnimation(two, one, 00);  
  163.                     mTab3.setImageDrawable(getResources().getDrawable(  
  164.                             R.drawable.tab_find_frd_normal));  
  165.                 } else if (currIndex == 3) {  
  166.                     animation = new TranslateAnimation(three, one, 00);  
  167.                     mTab4.setImageDrawable(getResources().getDrawable(  
  168.                             R.drawable.tab_settings_normal));  
  169.                 }  
  170.                 break;  
  171.             case 2:  
  172.                 mTab3.setImageDrawable(getResources().getDrawable(  
  173.                         R.drawable.tab_find_frd_pressed));  
  174.                 if (currIndex == 0) {  
  175.                     animation = new TranslateAnimation(zero, two, 00);  
  176.                     mTab1.setImageDrawable(getResources().getDrawable(  
  177.                             R.drawable.tab_weixin_normal));  
  178.                 } else if (currIndex == 1) {  
  179.                     animation = new TranslateAnimation(one, two, 00);  
  180.                     mTab2.setImageDrawable(getResources().getDrawable(  
  181.                             R.drawable.tab_address_normal));  
  182.                 } else if (currIndex == 3) {  
  183.                     animation = new TranslateAnimation(three, two, 00);  
  184.                     mTab4.setImageDrawable(getResources().getDrawable(  
  185.                             R.drawable.tab_settings_normal));  
  186.                 }  
  187.                 break;  
  188.             case 3:  
  189.                 mTab4.setImageDrawable(getResources().getDrawable(  
  190.                         R.drawable.tab_settings_pressed));  
  191.                 if (currIndex == 0) {  
  192.                     animation = new TranslateAnimation(zero, three, 00);  
  193.                     mTab1.setImageDrawable(getResources().getDrawable(  
  194.                             R.drawable.tab_weixin_normal));  
  195.                 } else if (currIndex == 1) {  
  196.                     animation = new TranslateAnimation(one, three, 00);  
  197.                     mTab2.setImageDrawable(getResources().getDrawable(  
  198.                             R.drawable.tab_address_normal));  
  199.                 } else if (currIndex == 2) {  
  200.                     animation = new TranslateAnimation(two, three, 00);  
  201.                     mTab3.setImageDrawable(getResources().getDrawable(  
  202.                             R.drawable.tab_find_frd_normal));  
  203.                 }  
  204.                 break;  
  205.             }  
  206.             currIndex = arg0;  
  207.             animation.setFillAfter(true);// True:图片停在动画结束位置  
  208.             animation.setDuration(150);// 动画持续时间  
  209.             mTabImg.startAnimation(animation);// 开始动画  
  210.         }         
  211.       
  212. }  
  效果如下:

  
  这样关于主页面的实现就完成了,之后会详细说一下各个Activity的实现。欢迎大家关注!

0 0
原创粉丝点击