android---图片切换

1、布局

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<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="60dp"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:background="#55000000"

android:gravity="center_vertical"

android:spacing="16dp"/>

</RelativeLayout>

2、Activity

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.Window;

importandroid.view.ViewGroup.LayoutParams;

importandroid.view.animation.AnimationUtils;

importandroid.widget.AdapterView;

importandroid.widget.Gallery;

importandroid.widget.ImageSwitcher;

importandroid.widget.ImageView;

importandroid.widget.AdapterView.OnItemSelectedListener;

importandroid.widget.ViewSwitcher.ViewFactory;

publicclassImageActivityextendsActivityimplementsViewFactory{

privateImageSwitcherimageSwitcher;

privateGallerygallery;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.imagelayout);

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));

gallery=(Gallery)findViewById(R.id.gallery);

ImageAdapterimageAdapter=newImageAdapter(this);

gallery.setAdapter(imageAdapter);

gallery.setOnItemSelectedListener(newOnItemSelectedListener(){

@Override

publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,

intindex,longarg3){

imageSwitcher.setImageResource(ConstData.imageId[index]);

}

@Override

publicvoidonNothingSelected(AdapterView<?>arg0){

}

});

}

@Override

publicViewmakeView(){

ImageViewi=newImageView(this);

i.setBackgroundColor(0xFF000000);

i.setScaleType(ImageView.ScaleType.FIT_CENTER);

i.setLayoutParams(newImageSwitcher.LayoutParams(

LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

returni;

}

}

3、Adapter

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.view.ViewGroup.LayoutParams;

importandroid.widget.BaseAdapter;

importandroid.widget.Gallery;

importandroid.widget.ImageView;

publicclassImageAdapterextendsBaseAdapter{

privateImageActivityimageActivity;

publicImageAdapter(ImageActivityimageActivity){

this.imageActivity=imageActivity;

}

@Override

publicintgetCount(){

//TODOAuto-generatedmethodstub

returnConstData.imageId.length;

}

@Override

publicObjectgetItem(inti){

//TODOAuto-generatedmethodstub

returni;

}

@Override

publiclonggetItemId(inti){

//TODOAuto-generatedmethodstub

returni;

}

@Override

publicViewgetView(inti,Viewview,ViewGroupviewgroup){

ImageViewimageView=null;

imageView=newImageView(imageActivity);

imageView.setLayoutParams(newGallery.LayoutParams(

LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

imageView.setAdjustViewBounds(true);

imageView.setImageResource(ConstData.imageId[i]);

returnimageView;

}

}

相关推荐