http://blog.csdn.net/wangjinyu501/article/details/8144332

http://blog.csdn.net/wangjinyu501/article/details/8144332

http://blog.csdn.net/wangjinyu501/article/details/8144332

http://blog.csdn.net/wangjinyu501/article/details/8144332




Android 仿微信之(二)--主页面实现篇

分类:Android 11388人阅读 评论(187) 收藏 举报

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

Android 仿微信之(二)--主页面实现篇_第1张图片

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

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

Android 仿微信之(二)--主页面实现篇_第2张图片

先以tabwidget为例,代码如下:

[java] view plain copy
  1. importandroid.app.TabActivity;
  2. importandroid.content.Intent;
  3. importandroid.os.Bundle;
  4. importandroid.view.View;
  5. importandroid.view.Window;
  6. importandroid.widget.RadioGroup;
  7. importandroid.widget.RadioGroup.OnCheckedChangeListener;
  8. importandroid.widget.TabHost;
  9. importandroid.widget.TextView;
  10. publicclassMainActivityextendsTabActivity{
  11. /**Calledwhentheactivityisfirstcreated.*/
  12. privateTabHosttabHost;
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  17. setContentView(R.layout.main);
  18. tabHost=this.getTabHost();
  19. TabHost.TabSpecspec;
  20. Intentintent;
  21. intent=newIntent().setClass(this,AddExamActivity.class);
  22. spec=tabHost.newTabSpec("微信").setIndicator("微信").setContent(intent);
  23. tabHost.addTab(spec);
  24. intent=newIntent().setClass(this,MyExamActivity.class);
  25. spec=tabHost.newTabSpec("通讯录").setIndicator("通讯录").setContent(intent);
  26. tabHost.addTab(spec);
  27. intent=newIntent().setClass(this,MyMessageActivity.class);
  28. spec=tabHost.newTabSpec("朋友们").setIndicator("朋友们").setContent(intent);
  29. tabHost.addTab(spec);
  30. intent=newIntent().setClass(this,Activity.class);
  31. spec=tabHost.newTabSpec("设置").setIndicator("设置").setContent(intent);
  32. tabHost.addTab(spec);
  33. intent=newIntent().setClass(this,SettingActivity.class);
  34. spec=tabHost.newTabSpec("设置").setIndicator("设置").setContent(intent);
  35. tabHost.addTab(spec);
  36. tabHost.setCurrentTab(1);
  37. }
  38. }
xml布局文件如下:

[html] view plain copy
  1. <?xmlversion="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. <LinearLayout
  8. android:orientation="vertical"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. <FrameLayout
  12. android:id="@android:id/tabcontent"//id必须为:tabcontent
  13. android:layout_width="fill_parent"
  14. android:layout_height="0.0dip"
  15. android:layout_weight="1.0"/>
  16. <TabWidget
  17. android:id="@android:id/tabs"//id必须为:tabs
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="0.0"/>
  21. </LinearLayout>这些id都是系统的,只有这样,系统才会找到他们正确辨认。
  22. </TabHost>
再就是点击button的切换效果了,这里需要使用selector来实现,如下:

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

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


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

[java] view plain copy
  1. importjava.util.ArrayList;
  2. importandroid.os.Bundle;
  3. importandroid.app.Activity;
  4. importandroid.content.Intent;
  5. importandroid.support.v4.view.PagerAdapter;
  6. importandroid.support.v4.view.ViewPager;
  7. importandroid.support.v4.view.ViewPager.OnPageChangeListener;
  8. importandroid.view.Display;
  9. importandroid.view.Gravity;
  10. importandroid.view.KeyEvent;
  11. importandroid.view.LayoutInflater;
  12. importandroid.view.View;
  13. importandroid.view.WindowManager;
  14. importandroid.view.WindowManager.LayoutParams;
  15. importandroid.view.animation.Animation;
  16. importandroid.view.animation.TranslateAnimation;
  17. importandroid.widget.ImageView;
  18. importandroid.widget.LinearLayout;
  19. importandroid.widget.PopupWindow;
  20. publicclassMainWeixinextendsActivity{
  21. publicstaticMainWeixininstance=null;
  22. privateViewPagermTabPager;//声明对象
  23. privateImageViewmTabImg;//动画图片
  24. privateImageViewmTab1,mTab2,mTab3,mTab4;
  25. privateintzero=0;//动画图片偏移量
  26. privateintcurrIndex=0;//当前页卡编号
  27. privateintone;//单个水平动画位移
  28. privateinttwo;
  29. privateintthree;
  30. privateLinearLayoutmClose;
  31. privateLinearLayoutmCloseBtn;
  32. privateViewlayout;
  33. privatebooleanmenu_display=false;
  34. privatePopupWindowmenuWindow;
  35. privateLayoutInflaterinflater;
  36. @Override
  37. publicvoidonCreate(BundlesavedInstanceState){
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.main_weixin);
  40. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);//启动activity时不自动弹出软键盘
  41. instance=this;
  42. mTabPager=(ViewPager)findViewById(R.id.tabpager);
  43. mTabPager.setOnPageChangeListener(newMyOnPageChangeListener());
  44. mTab1=(ImageView)findViewById(R.id.img_weixin);
  45. mTab2=(ImageView)findViewById(R.id.img_address);
  46. mTab3=(ImageView)findViewById(R.id.img_friends);
  47. mTab4=(ImageView)findViewById(R.id.img_settings);
  48. mTabImg=(ImageView)findViewById(R.id.img_tab_now);//动画图片
  49. mTab1.setOnClickListener(newMyOnClickListener(0));
  50. mTab2.setOnClickListener(newMyOnClickListener(1));
  51. mTab3.setOnClickListener(newMyOnClickListener(2));
  52. mTab4.setOnClickListener(newMyOnClickListener(3));
  53. DisplaycurrDisplay=getWindowManager().getDefaultDisplay();//获取屏幕当前分辨率
  54. intdisplayWidth=currDisplay.getWidth();
  55. one=displayWidth/4;//设置水平动画平移大小
  56. two=one*2;
  57. three=one*3;
  58. //将要分页显示的View装入数组中
  59. LayoutInflatermLi=LayoutInflater.from(this);
  60. Viewview1=mLi.inflate(R.layout.main_tab_weixin,null);
  61. Viewview2=mLi.inflate(R.layout.main_tab_address,null);
  62. Viewview3=mLi.inflate(R.layout.main_tab_friends,null);
  63. Viewview4=mLi.inflate(R.layout.main_tab_settings,null);
  64. //每个页面的view数据
  65. finalArrayList<View>views=newArrayList<View>();
  66. views.add(view1);
  67. views.add(view2);
  68. views.add(view3);
  69. views.add(view4);
  70. //填充ViewPager的数据适配器
  71. PagerAdaptermPagerAdapter=newPagerAdapter(){
  72. @Override
  73. publicbooleanisViewFromObject(Viewarg0,Objectarg1){
  74. returnarg0==arg1;
  75. }
  76. @Override
  77. publicintgetCount(){
  78. returnviews.size();
  79. }
  80. @Override
  81. publicvoiddestroyItem(Viewcontainer,intposition,Objectobject){
  82. ((ViewPager)container).removeView(views.get(position));
  83. }
  84. //@Override
  85. //publicCharSequencegetPageTitle(intposition){
  86. //returntitles.get(position);
  87. //}
  88. @Override
  89. publicObjectinstantiateItem(Viewcontainer,intposition){
  90. ((ViewPager)container).addView(views.get(position));
  91. returnviews.get(position);
  92. }
  93. };
  94. mTabPager.setAdapter(mPagerAdapter);
  95. }
  96. /**
  97. *头标点击监听
  98. */
  99. publicclassMyOnClickListenerimplementsView.OnClickListener{
  100. privateintindex=0;
  101. publicMyOnClickListener(inti){
  102. index=i;
  103. }
  104. publicvoidonClick(Viewv){
  105. mTabPager.setCurrentItem(index);
  106. }
  107. };
  108. /*
  109. *页卡切换监听(原作者:D.Winter)
  110. */
  111. publicclassMyOnPageChangeListenerimplementsOnPageChangeListener{
  112. publicvoidonPageSelected(intarg0){
  113. Animationanimation=null;
  114. switch(arg0){
  115. case0:
  116. mTab1.setImageDrawable(getResources().getDrawable(
  117. R.drawable.tab_weixin_pressed));
  118. if(currIndex==1){
  119. animation=newTranslateAnimation(one,0,0,0);
  120. mTab2.setImageDrawable(getResources().getDrawable(
  121. R.drawable.tab_address_normal));
  122. }elseif(currIndex==2){
  123. animation=newTranslateAnimation(two,0,0,0);
  124. mTab3.setImageDrawable(getResources().getDrawable(
  125. R.drawable.tab_find_frd_normal));
  126. }elseif(currIndex==3){
  127. animation=newTranslateAnimation(three,0,0,0);
  128. mTab4.setImageDrawable(getResources().getDrawable(
  129. R.drawable.tab_settings_normal));
  130. }
  131. break;
  132. case1:
  133. mTab2.setImageDrawable(getResources().getDrawable(
  134. R.drawable.tab_address_pressed));
  135. if(currIndex==0){
  136. animation=newTranslateAnimation(zero,one,0,0);
  137. mTab1.setImageDrawable(getResources().getDrawable(
  138. R.drawable.tab_weixin_normal));
  139. }elseif(currIndex==2){
  140. animation=newTranslateAnimation(two,one,0,0);
  141. mTab3.setImageDrawable(getResources().getDrawable(
  142. R.drawable.tab_find_frd_normal));
  143. }elseif(currIndex==3){
  144. animation=newTranslateAnimation(three,one,0,0);
  145. mTab4.setImageDrawable(getResources().getDrawable(
  146. R.drawable.tab_settings_normal));
  147. }
  148. break;
  149. case2:
  150. mTab3.setImageDrawable(getResources().getDrawable(
  151. R.drawable.tab_find_frd_pressed));
  152. if(currIndex==0){
  153. animation=newTranslateAnimation(zero,two,0,0);
  154. mTab1.setImageDrawable(getResources().getDrawable(
  155. R.drawable.tab_weixin_normal));
  156. }elseif(currIndex==1){
  157. animation=newTranslateAnimation(one,two,0,0);
  158. mTab2.setImageDrawable(getResources().getDrawable(
  159. R.drawable.tab_address_normal));
  160. }elseif(currIndex==3){
  161. animation=newTranslateAnimation(three,two,0,0);
  162. mTab4.setImageDrawable(getResources().getDrawable(
  163. R.drawable.tab_settings_normal));
  164. }
  165. break;
  166. case3:
  167. mTab4.setImageDrawable(getResources().getDrawable(
  168. R.drawable.tab_settings_pressed));
  169. if(currIndex==0){
  170. animation=newTranslateAnimation(zero,three,0,0);
  171. mTab1.setImageDrawable(getResources().getDrawable(
  172. R.drawable.tab_weixin_normal));
  173. }elseif(currIndex==1){
  174. animation=newTranslateAnimation(one,three,0,0);
  175. mTab2.setImageDrawable(getResources().getDrawable(
  176. R.drawable.tab_address_normal));
  177. }elseif(currIndex==2){
  178. animation=newTranslateAnimation(two,three,0,0);
  179. mTab3.setImageDrawable(getResources().getDrawable(
  180. R.drawable.tab_find_frd_normal));
  181. }
  182. break;
  183. }
  184. currIndex=arg0;
  185. animation.setFillAfter(true);//True:图片停在动画结束位置
  186. animation.setDuration(150);//动画持续时间
  187. mTabImg.startAnimation(animation);//开始动画
  188. }
  189. }
效果如下:

Android 仿微信之(二)--主页面实现篇_第3张图片
这样关于主页面的实现就完成了,之后会详细说一下各个Activity的实现。欢迎大家关注!










30

更多相关文章

  1. 在TextView上显示图片信息
  2. 在android中实现两张图片对比
  3. Android 获取网络图片方法
  4. Android代码总结,Sdcard和图片相关
  5. android桌面小火箭升空动画
  6. android图片缩放(指定大小) drawable获取图片后怎么设置图片大小
  7. Android调用相机拍照并返回路径和调用系统图库选择图片
  8. [android]简单方法记载 在adapter中异步加载网络图片
  9. android打开,保存图片到sd卡,显示图片

随机推荐

  1. android 模拟器,创建sdcard 但是却是只读
  2. Android后台任务(HandlerThread、AsyncTas
  3. Android(安卓)与SQlite 数据库操作(新手步
  4. Android(安卓)Studio(AS)-->drawable与mi
  5. Ubuntu下Android(安卓)JNI初步学习之——
  6. Android之通过ContentProvider实现两个ap
  7. 基于 Android(安卓)NDK
  8. 【移动开发】Android游戏开发SurfaceView
  9. android 日记log保存到本地简单方法
  10. Android(安卓)React Native安装指南