歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android之ImageSwitcher 圖片查看

布局文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.     <ImageSwitcher android:id="@+id/switcher"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:layout_alignParentLeft="true"  
  10.         android:layout_alignParentTop="true">  
  11.     </ImageSwitcher>  
  12.     <Gallery android:id="@+id/gallery"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_height="60dp"  
  16.         android:spacing="15dp"  
  17.         android:layout_alignParentBottom="true"  
  18.         android:gravity="center_vertical"  
  19.         android:background="#aaaaaa">  
  20.     </Gallery>  
  21. </RelativeLayout>  
主程序如下:
  1. package com.cloay.imageswitcher;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.view.animation.AnimationUtils;  
  10. import android.widget.AdapterView;  
  11. import android.widget.AdapterView.OnItemSelectedListener;  
  12. import android.widget.Gallery.LayoutParams;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.Gallery;  
  15. import android.widget.ImageSwitcher;  
  16. import android.widget.ImageView;  
  17. import android.widget.ViewSwitcher.ViewFactory;  
  18.   
  19. /** 
  20.  * ImageSwitcherActivity.java 
  21.  * @author cloay 
  22.  * 2011-7-16 
  23.  */  
  24. public class ImageSwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory{  
  25.     private ImageSwitcher imageSwitcher;  
  26.     private Gallery gallery;  
  27.     private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,   
  28.             R.drawable.f, R.drawable.g};  
  29.     private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,   
  30.             R.drawable.f, R.drawable.g};  
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.           
  36.         imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);  
  37.         imageSwitcher.setFactory(this);  
  38.         //設置圖片切換時的動畫效果   
  39.         imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));  
  40.         imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));  
  41.           
  42.         gallery = (Gallery)findViewById(R.id.gallery);  
  43.         //自定義ImageAdapter繼承於BaseAdapter,是一個內部類   
  44.         gallery.setAdapter(new ImageAdapter(this));  
  45.         gallery.setOnItemSelectedListener(this);  
  46.     }  
  47.   
  48.     @Override  
  49.     public View makeView() {  
  50.         ImageView image = new ImageView(this);  
  51.         image.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  52.         image.setLayoutParams(new ImageSwitcher.LayoutParams(  
  53.                 LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));  
  54.         return image;  
  55.     }  
  56.   
  57.     @Override  
  58.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  59.             long arg3) {  
  60.         imageSwitcher.setImageResource(imagesId[arg2]);  
  61.     }  
  62.   
  63.     @Override  
  64.     public void onNothingSelected(AdapterView<?> arg0) {  
  65.           
  66.     }  
  67.     public class ImageAdapter extends BaseAdapter{  
  68.         private Context context;  
  69.         int galleryItemBackground;  
  70.         public ImageAdapter (Context c){  
  71.             context = c;  
  72.             TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery1);  
  73.             galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);  
  74.             typeArray.recycle();  
  75.         }  
  76.         @Override  
  77.         public int getCount() {  
  78.             //返回selectId[]的長度   
  79.             return selectId.length;  
  80.         }  
  81.   
  82.         @Override  
  83.         public Object getItem(int position) {  
  84.             return position;  
  85.         }  
  86.   
  87.         @Override  
  88.         public long getItemId(int position) {  
  89.             return position;  
  90.         }  
  91.   
  92.         @Override  
  93.         public View getView(int position, View convertView, ViewGroup parent) {  
  94.             ImageView imageView = new ImageView(context);  
  95.             //設置資源圖片   
  96.             imageView.setImageResource(selectId[position]);  
  97.             imageView.setAdjustViewBounds(true);  //允許調整邊框   
  98.             //設定底部畫廊,自適應大小   
  99.             imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  100.             //設置畫廊背景   
  101.             imageView.setBackgroundResource(galleryItemBackground);  
  102.             return imageView;  
  103.         }  
  104.           
  105.     }  
  106. }  
Copyright © Linux教程網 All Rights Reserved