:
布局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" ><!-- 圖片切換 --> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ImageSwitcher><!-- 圖片瀏覽 --> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="60dip" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center" android:spacing="10dip" /></RelativeLayout>
activity類:
package cn.bzu.imageswitcher_gallery;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.res.TypedArray;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;import android.support.v4.app.NavUtils;public class ImageSwitcherActivity extends Activity {private ImageSwitcher imageSwitcher;private Gallery gallery;// 存放大圖的數組private int[] imageLarge = { R.drawable.sample_0, R.drawable.sample_1,R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };// 存放小圖的數組private int[] imageSmall = { R.drawable.sample_thumb_0,R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,R.drawable.sample_thumb_7 };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_image_switcher);imageSwitcher=(ImageSwitcher) this.findViewById(R.id.imageSwitcher);gallery=(Gallery) this.findViewById(R.id.gallery);imageSwitcher.setFactory(new MyViewFactory());gallery.setAdapter(new ImageAdapter(this));gallery.setSelection(imageSmall.length/2);gallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {imageSwitcher.setImageResource(imageLarge[position]);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});}//自訂MyViewFactory private class MyViewFactory implements ViewFactory{@Overridepublic View makeView() {ImageView imageView=new ImageView(ImageSwitcherActivity.this);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));return imageView;}}//自訂ImageAdapterprivate class ImageAdapter extends BaseAdapter{private Context context;int galleryItemBackground;public ImageAdapter(Context context) {this.context = context;TypedArray a=obtainStyledAttributes(R.styleable.HelloGallery);galleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);a.recycle();}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn imageSmall.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn imageSmall[position];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView(ImageSwitcherActivity.this);imageView.setImageResource(imageSmall[position]);// imageView.setAdjustViewBounds(true);imageView.setBackgroundResource(galleryItemBackground);return imageView;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_image_switcher, menu);return true;}}
package cn.bzu.imageswitcher_gallery;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.res.TypedArray;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;import android.support.v4.app.NavUtils;public class ImageSwitcherActivity extends Activity {private ImageSwitcher imageSwitcher;private Gallery gallery;// 存放大圖的數組private int[] imageLarge = { R.drawable.sample_0, R.drawable.sample_1,R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };// 存放小圖的數組private int[] imageSmall = { R.drawable.sample_thumb_0,R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,R.drawable.sample_thumb_7 };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_image_switcher);imageSwitcher=(ImageSwitcher) this.findViewById(R.id.imageSwitcher);gallery=(Gallery) this.findViewById(R.id.gallery);imageSwitcher.setFactory(new MyViewFactory());gallery.setAdapter(new ImageAdapter(this));gallery.setSelection(imageSmall.length/2);gallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {imageSwitcher.setImageResource(imageLarge[position]);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});}//自訂MyViewFactory private class MyViewFactory implements ViewFactory{@Overridepublic View makeView() {ImageView imageView=new ImageView(ImageSwitcherActivity.this);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));return imageView;}}//自訂ImageAdapterprivate class ImageAdapter extends BaseAdapter{private Context context;int galleryItemBackground;public ImageAdapter(Context context) {this.context = context;TypedArray a=obtainStyledAttributes(R.styleable.HelloGallery);galleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);a.recycle();}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn imageSmall.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn imageSmall[position];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView(ImageSwitcherActivity.this);imageView.setImageResource(imageSmall[position]);// imageView.setAdjustViewBounds(true);imageView.setBackgroundResource(galleryItemBackground);return imageView;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_image_switcher, menu);return true;}}