android 实现左右拖动的网页焦点图

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

主布局文件main.xml:

  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

  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的分辨率,因为焦点图比较大


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值