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

来源:互联网 发布:云打印软件乐视版 编辑:程序博客网 时间:2024/05/18 18:03

 

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

主布局文件main.xml:

view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns: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.             <!--指针图 -->  
  44.             <ImageView  
  45.                 android:id="@+id/focusPointImage"  
  46.                 android:layout_width="wrap_content"  
  47.                 android:layout_height="wrap_content"  
  48.                 android:background="@drawable/focus_point_1"  
  49.             />  
  50.     </LinearLayout>  
  51. </RelativeLayout>  
 

 

主界面的Activity ---->MainActivity

view plain
  1. package com.xmz.face;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.Gallery;  
  9. import android.widget.ImageView;  
  10. import android.widget.TextView;  
  11. import android.widget.AdapterView.OnItemSelectedListener;  
  12. public class MainActivity extends Activity {  
  13.     private Gallery gallery;  
  14.     private TextView titleText;  
  15.     private TextView contentText;  
  16.     private FocusAdapter adapter;  
  17.     private ImageView focusPointImage;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         gallery=(Gallery)findViewById(R.id.focusGallery);  
  24.         titleText=(TextView)findViewById(R.id.titleText);  
  25.         contentText=(TextView)findViewById(R.id.contentText);  
  26.         focusPointImage=(ImageView)findViewById(R.id.focusPointImage);  
  27.         adapter = new FocusAdapter(this);  
  28.         gallery.setAdapter(adapter);  
  29.           
  30.         gallery.setOnItemSelectedListener(new OnItemSelectedListener(){  
  31.             @Override  
  32.             public void onItemSelected(AdapterView<?> arg0, View arg1,  
  33.                     int position, long arg3) {  
  34.                 Map map = adapter.getItem(position);  
  35.                 titleText.setText(map.get("title").toString());  
  36.                 contentText.setText(map.get("content").toString());  
  37.                 switch(position){  
  38.                     case 0:  
  39.                         focusPointImage.setBackgroundResource(R.drawable.focus_point_1);  
  40.                         break;  
  41.                     case 1:  
  42.                         focusPointImage.setBackgroundResource(R.drawable.focus_point_2);  
  43.                         break;  
  44.                     case 2:  
  45.                         focusPointImage.setBackgroundResource(R.drawable.focus_point_3);  
  46.                         break;  
  47.                     case 3:  
  48.                         focusPointImage.setBackgroundResource(R.drawable.focus_point_4);  
  49.                         break;  
  50.                     case 4:  
  51.                         focusPointImage.setBackgroundResource(R.drawable.focus_point_5);  
  52.                         break;  
  53.                 }  
  54.             }  
  55.             @Override  
  56.             public void onNothingSelected(AdapterView<?> arg0) {  
  57.                   
  58.             }  
  59.         });  
  60.           
  61.         //添加图片   
  62.         Map<String, Object> map1 = new  HashMap<String,Object>();  
  63.         map1.put("image", getResources().getDrawable(R.drawable.focus_1));  
  64.         map1.put("title","这是标题1");  
  65.         map1.put("content","这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1这是内容1");  
  66.           
  67.         Map<String, Object> map2 = new  HashMap<String,Object>();  
  68.         map2.put("image", getResources().getDrawable(R.drawable.focus_2));  
  69.         map2.put("title","这是标题2");  
  70.         map2.put("content","这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2这是内容2");  
  71.           
  72.         Map<String, Object> map3 = new  HashMap<String,Object>();  
  73.         map3.put("image", getResources().getDrawable(R.drawable.focus_3));  
  74.         map3.put("title","这是标题3");  
  75.         map3.put("content","这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3这是内容3");  
  76.           
  77.         Map<String, Object> map4 = new  HashMap<String,Object>();  
  78.         map4.put("image", getResources().getDrawable(R.drawable.focus_4));  
  79.         map4.put("title","这是标题4");  
  80.         map4.put("content","这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4这是内容4");  
  81.           
  82.         Map<String, Object> map5 = new  HashMap<String,Object>();  
  83.         map5.put("image", getResources().getDrawable(R.drawable.focus_5));  
  84.         map5.put("title","这是标题5");  
  85.         map5.put("content","这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5这是内容5");  
  86.           
  87.         adapter.addObject(map1);  
  88.         adapter.addObject(map2);  
  89.         adapter.addObject(map3);  
  90.         adapter.addObject(map4);  
  91.         adapter.addObject(map5);  
  92.           
  93.         titleText.setText(adapter.getItem(0).get("title").toString());  
  94.         contentText.setText(adapter.getItem(0).get("content").toString());  
  95.           
  96.     }  
  97. }  
 

 

 

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


0 0