(1)gallery 即綁定一個adapter 即可。
(2)Activity 實現ViewFactory。並實現其中的構造方法
makeView();。這個方法主要是返回一個view對象。並為switcher設置imageSwitcher.setFactory(this);--這個Factory 是用來切換ImageSwitcher的view的。
實現OnItemSelectedListener為gallery重寫
onNothingSelected();
onItemSelected();
- package com.bsn.cc;
-
-
-
-
- import Android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.Gallery.LayoutParams;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.ViewSwitcher.ViewFactory;
-
- public class ImageSwitcherActivity extends Activity implements OnItemSelectedListener,ViewFactory{
- private Gallery gallery;
- private ImageSwitcher imageSwitcher;
- private ImageAdapter imageAdapter;
- private int []resId={
- R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,
- R.drawable.item5, R.drawable.item6, R.drawable.item7,
- R.drawable.item8, R.drawable.item9, R.drawable.item10,
- R.drawable.item11, R.drawable.item12, R.drawable.item13,
- R.drawable.item14, R.drawable.item15
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- gallery = (Gallery) findViewById(R.id.dddd);
- imageAdapter = new ImageAdapter(this);
- gallery.setAdapter(imageAdapter);
- gallery.setOnItemSelectedListener(this);
- imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
- imageSwitcher.setFactory(this);
-
- imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_in));
- imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_out));
- }
- private class ImageAdapter extends BaseAdapter{
- int mGalleryItemBackground;
- private Context mContext;
- public ImageAdapter(Context context)
- {
- mContext = context;
- // 這裡為自定義屬性R.styleable.Gallery ..中的屬性定義的是gallery的默認邊框
- TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
- mGalleryItemBackground = typedArray.getResourceId(
- R.styleable.Gallery_android_galleryItemBackground, 0);
- }
- @Override
- public int getCount() {
- return Integer.MAX_VALUE;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView=new ImageView(mContext);
- imageView.setImageResource(resId[position]);
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- imageView.setLayoutParams(new Gallery.LayoutParams(136,88));
- imageView.setBackgroundResource(mGalleryItemBackground);
- return imageView;
- }
- }
- @Override
- public View makeView() {
-
- ImageView imageView = new ImageView(this);
- imageView.setBackgroundColor(0xFF000000);
- imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
- imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
-
- return imageView;
- }
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- imageSwitcher.setImageResource(resId[arg2 % resId.length]);
-
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
-
- }
- }