Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 元件之 ImageSwitcher 圖片切換器用法

編輯:Android 開發實例

ImageSwitcher 是 Android 中用來控制圖片展示效果的一個元件,例如幻燈片式的切換效果,頗有質感,拿來做相簿可說是一絕。

源碼下載

一、重要方法

setImageURI(Uri uri):設定圖片位址

setImageResource(int resid):設定圖片資源庫

setImageDrawable(Drawable drawable):繪製圖片

二、實例

  1.  <ImageSwitcher android:id="@+id/switcher" 
  2.         android:layout_width="match_parent" 
  3.         android:layout_height="match_parent" 
  4.         android:layout_alignParentTop="true" 
  5.         android:layout_alignParentLeft="true" 
  6.     /> 
  7.     
  8.     <Gallery android:id="@+id/gallery" 
  9.         android:background="#55000000" 
  10.         android:layout_width="match_parent" 
  11.         android:layout_height="60dp" 
  12.         android:layout_alignParentBottom="true" 
  13.         android:layout_alignParentLeft="true" 
  14.         
  15.         android:gravity="center_vertical" 
  16.         android:spacing="16dp" 
  17.     /> 

is = (ImageSwitcher) findViewById(R.id.switcher);

is.setFactory(this);

設定動畫效果

  is.setInAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_in));
  is.setOutAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_out));

三、完整程式碼

1. 布局檔

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"   
  4.     android:layout_width="match_parent"   
  5.     android:layout_height="match_parent">   
  6.     
  7.     <ImageSwitcher android:id="@+id/switcher" 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent" 
  10.         android:layout_alignParentTop="true" 
  11.         android:layout_alignParentLeft="true" 
  12.     /> 
  13.     
  14.     <Gallery android:id="@+id/gallery" 
  15.         android:background="#55000000" 
  16.         android:layout_width="match_parent" 
  17.         android:layout_height="60dp" 
  18.         android:layout_alignParentBottom="true" 
  19.         android:layout_alignParentLeft="true" 
  20.         
  21.         android:gravity="center_vertical" 
  22.         android:spacing="16dp" 
  23.     /> 
  24.  
  25. </RelativeLayout> 
  26.  

  2. Java 程式碼

  1. package wjq.WidgetDemo; 
  2.  
  3. import android.app.Activity; 
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.view.Window;
  9. import android.view.animation.AnimationUtils;
  10. import android.widget.AdapterView;
  11. import android.widget.BaseAdapter;
  12. import android.widget.Gallery;
  13. import android.widget.ImageSwitcher;
  14. import android.widget.ImageView;
  15. import android.widget.AdapterView.OnItemClickListener;
  16. import android.widget.AdapterView.OnItemSelectedListener;
  17. import android.widget.Gallery.LayoutParams;
  18. import android.widget.ViewSwitcher.ViewFactory;
  19.  
  20. public class ImageSwitcherDemo extends Activity implements
  21.   OnItemSelectedListener, ViewFactory { 
  22.  private ImageSwitcher is; 
  23.  private Gallery gallery; 
  24.  
  25.  private Integer[] mThumbIds = { R.drawable.b, R.drawable.c, 
  26.    R.drawable.d, R.drawable.f, R.drawable.g, 
  27.    }; 
  28.  
  29.  private Integer[] mImageIds = { R.drawable.b, R.drawable.c, 
  30.    R.drawable.d, R.drawable.f, R.drawable.g, }; 
  31.  
  32.  @Override 
  33.  protected void onCreate(Bundle savedInstanceState) { 
  34.   super.onCreate(savedInstanceState); 
  35.   requestWindowFeature(Window.FEATURE_NO_TITLE); 
  36.   setContentView(R.layout.imageswitcherpage); 
  37.  
  38.   is = (ImageSwitcher) findViewById(R.id.switcher); 
  39.   is.setFactory(this); 
  40.  
  41.   is.setInAnimation(AnimationUtils.loadAnimation(this, 
  42.     android.R.anim.fade_in)); 
  43.   is.setOutAnimation(AnimationUtils.loadAnimation(this, 
  44.     android.R.anim.fade_out)); 
  45.  
  46.   gallery = (Gallery) findViewById(R.id.gallery); 
  47.  
  48.   gallery.setAdapter(new ImageAdapter(this));
  49.   gallery.setOnItemSelectedListener(this);
  50.  }
  51.  
  52.  @Override 
  53.  public View makeView() { 
  54.   ImageView i = new ImageView(this); 
  55.   i.setBackgroundColor(0xFF000000); 
  56.   i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
  57.   i.setLayoutParams(new ImageSwitcher.LayoutParams(
  58.     LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  59.   return i; 
  60.  }
  61.  
  62.  public class ImageAdapter extends BaseAdapter { 
  63.   public ImageAdapter(Context c) { 
  64.    mContext = c; 
  65.   }
  66.  
  67.   public int getCount() { 
  68.    return mThumbIds.length; 
  69.   }
  70.  
  71.   public Object getItem(int position) { 
  72.    return position; 
  73.   }
  74.  
  75.   public long getItemId(int position) { 
  76.    return position; 
  77.   }
  78.  
  79.   public View getView(int position, View convertView, ViewGroup parent) { 
  80.    ImageView i = new ImageView(mContext); 
  81.    i.setImageResource(mThumbIds[position]); 
  82.    i.setAdjustViewBounds(true);
  83.    i.setLayoutParams(new Gallery.LayoutParams(
  84.      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  85.    i.setBackgroundResource(R.drawable.e); 
  86.    return i; 
  87.   }
  88.  
  89.   private Context mContext; 
  90.  
  91.  }
  92.  
  93.  @Override 
  94.  public void onItemSelected(AdapterView<?> parent, View view, int position, 
  95.    long id) { 
  96.   is.setImageResource(mImageIds[position]); 
  97.  
  98.  }
  99.  
  100.  @Override 
  101.  public void onNothingSelected(AdapterView<?> parent) { 
  102.  
  103.  }
  104.  
  105. }

轉自:https://www.cnblogs.com/salam/archive/2010/10/06/1844660.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved