焦点图在各大网站首页是很常见的一种效果,在Android 上也可以实现,采用Gallery 便可轻松做到!

主布局文件main.xml:

view plain
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <com.xmz.face.FocusGallery
  7. android:id="@+id/focusGallery"
  8. android:layout_width="fill_parent"
  9. android:layout_height="180px"
  10. />
  11. <!--背景-->
  12. <LinearLayout
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_alignBottom="@id/focusGallery"
  16. android:gravity="center_horizontal"
  17. android:orientation="vertical"
  18. android:background="@drawable/focus_bg">
  19. <RelativeLayout
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content">
  22. <!--标题-->
  23. <TextView
  24. android:id="@+id/titleText"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:textColor="#fff"
  28. android:layout_alignParentLeft="true"
  29. />
  30. <!--简介-->
  31. <TextView
  32. android:id="@+id/contentText"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_alignLeft="@id/titleText"
  36. android:layout_below="@id/titleText"
  37. android:textSize="16px"
  38. android:textColor="#ccc"
  39. android:lines="2"
  40. />
  41. </RelativeLayout>
  42. <!--指针图-->
  43. <ImageView
  44. android:id="@+id/focusPointImage"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:background="@drawable/focus_point_1"
  48. />
  49. </LinearLayout>
  50. </RelativeLayout>

主界面的Activity ---->MainActivity

view plain
  1. packagecom.xmz.face;
  2. importjava.util.HashMap;
  3. importjava.util.Map;
  4. importandroid.app.Activity;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.widget.AdapterView;
  8. importandroid.widget.Gallery;
  9. importandroid.widget.ImageView;
  10. importandroid.widget.TextView;
  11. importandroid.widget.AdapterView.OnItemSelectedListener;
  12. publicclassMainActivityextendsActivity{
  13. privateGallerygallery;
  14. privateTextViewtitleText;
  15. privateTextViewcontentText;
  16. privateFocusAdapteradapter;
  17. privateImageViewfocusPointImage;
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. gallery=(Gallery)findViewById(R.id.focusGallery);
  23. titleText=(TextView)findViewById(R.id.titleText);
  24. contentText=(TextView)findViewById(R.id.contentText);
  25. focusPointImage=(ImageView)findViewById(R.id.focusPointImage);
  26. adapter=newFocusAdapter(this);
  27. gallery.setAdapter(adapter);
  28. gallery.setOnItemSelectedListener(newOnItemSelectedListener(){
  29. @Override
  30. publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,
  31. intposition,longarg3){
  32. Mapmap=adapter.getItem(position);
  33. titleText.setText(map.get("title").toString());
  34. contentText.setText(map.get("content").toString());
  35. switch(position){
  36. case0:
  37. focusPointImage.setBackgroundResource(R.drawable.focus_point_1);
  38. break;
  39. case1:
  40. focusPointImage.setBackgroundResource(R.drawable.focus_point_2);
  41. break;
  42. case2:
  43. focusPointImage.setBackgroundResource(R.drawable.focus_point_3);
  44. break;
  45. case3:
  46. focusPointImage.setBackgroundResource(R.drawable.focus_point_4);
  47. break;
  48. case4:
  49. focusPointImage.setBackgroundResource(R.drawable.focus_point_5);
  50. break;
  51. }
  52. }
  53. @Override
  54. publicvoidonNothingSelected(AdapterView<?>arg0){
  55. }
  56. });
  57. //添加图片
  58. Map<String,Object>map1=newHashMap<String,Object>();
  59. map1.put("image",getResources().getDrawable(R.drawable.focus_1));
  60. map1.put("title","这是标题1");
  61. map1.put("content","这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1");
  62. Map<String,Object>map2=newHashMap<String,Object>();
  63. map2.put("image",getResources().getDrawable(R.drawable.focus_2));
  64. map2.put("title","这是标题2");
  65. map2.put("content","这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2");
  66. Map<String,Object>map3=newHashMap<String,Object>();
  67. map3.put("image",getResources().getDrawable(R.drawable.focus_3));
  68. map3.put("title","这是标题3");
  69. map3.put("content","这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3");
  70. Map<String,Object>map4=newHashMap<String,Object>();
  71. map4.put("image",getResources().getDrawable(R.drawable.focus_4));
  72. map4.put("title","这是标题4");
  73. map4.put("content","这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4");
  74. Map<String,Object>map5=newHashMap<String,Object>();
  75. map5.put("image",getResources().getDrawable(R.drawable.focus_5));
  76. map5.put("title","这是标题5");
  77. map5.put("content","这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5");
  78. adapter.addObject(map1);
  79. adapter.addObject(map2);
  80. adapter.addObject(map3);
  81. adapter.addObject(map4);
  82. adapter.addObject(map5);
  83. titleText.setText(adapter.getItem(0).get("title").toString());
  84. contentText.setText(adapter.getItem(0).get("content").toString());
  85. }
  86. }

贴上工程源代码:http://good.gd/1302172.htm

提醒:测试时,建议用800*480的分辨率,因为焦点图比较大


更多相关文章

  1. TextView属性android:ellipsize实现跑马灯效果,TextView内容过长
  2. android GridView item中组件获取焦点
  3. Android把activity设置为窗口样式怎么去掉标题
  4. textview中自动换行显示文本内容
  5. android EditText控件自动获取焦点弹出键盘解决方法

随机推荐

  1. Spring Cloud 2.x系列之网关zuul入门(二)
  2. Spring Cloud 2.x系列之整合rocketMQ
  3. IT工作?咋样的IT工作才是一个好工作?
  4. SpringCloud 2.x之中整合Zipkin进行服务
  5. SpringCloud 2.x之Spring Cloud 中整合Zi
  6. C语言常见的代码题
  7. 样式选择器、组件模块化及伪类选择器的用
  8. CSS选择器讲解
  9. 笑傲Java面试:面霸修炼手册
  10. idea配置