package com.isoftstone.cry;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class GallerySwitcherActivity extends Activity implements OnItemSelectedListener , ViewFactory
{
private ImageSwitcher mswitcher ;
private Integer[] mThumbIds ={
R.drawable.fc,
R.drawable.ic_launcher,
R.drawable.ic_menu_add,
R.drawable.ic_menu_edit,
R.drawable.ic_menu_help,
R.drawable.psu
};
private Integer[] mImageIds ={
R.drawable.fc,
R.drawable.ic_launcher,
R.drawable.ic_menu_add,
R.drawable.ic_menu_edit,
R.drawable.ic_menu_help,
R.drawable.psu
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_imageswitcher);
mswitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
mswitcher.setFactory(this);
mswitcher.setAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
mswitcher.setAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
Gallery gallery = (Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(this);
}
//gallery adapter
public class ImageAdapter extends BaseAdapter
{
Context context ;
public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
context = c ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(context);
iv.setImageResource(mThumbIds[position]);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
iv.setBackgroundResource(R.drawable.fc);
return iv;
}
}
@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position,
long id) {
// TODO Auto-generated method stub
mswitcher.setImageResource(mImageIds[position]);
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i = new ImageView(this);
i.setBackgroundColor(0xff000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return i;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:spacing="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageSwitcher>
</RelativeLayout>