Android: Uses gallery and imageSwitch to create an image browser that can slide between the left and right cycles.
To enable infinite sliding between the left and right sides of the image browser, We need to customize the gallery adapter.
To customize the adapter, you must first understand these methods.
@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubreturn null;}
The getCount method returns the number of data sources.
The getItem method returns an object, that is, the object corresponding to the data ID position in the current container.
GetItemId returns the data ID in the current container
GetView: Get the View to be displayed.
If you want to achieve left-side circular sliding, first we need to return the maximum number of data sources, and then set the remainder of the number of original data sources for all data IDs. Finally, set the initial position of gallery to the center of 0-maximum. you can.
The changed adapter is like this.
package com.example.imageswitcher;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.ImageView.ScaleType;public class MyAdapter extends BaseAdapter{private int id_image[];private Context contex;public MyAdapter(Context contex,int id_image[]) {this.contex=contex;this.id_image=id_image;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn Integer.MAX_VALUE;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn id_image[position%id_image.length];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position%id_image.length;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView imageView=new ImageView(contex);imageView.setBackgroundResource(id_image[position%id_image.length]);imageView.setLayoutParams(new Gallery.LayoutParams(250, 200));imageView.setScaleType(ScaleType.FIT_XY);return imageView;}}MainActivity
Package com. example. imageswitcher; import android. OS. bundle; import android. app. activity; import android. view. layoutInflater. factory; import android. view. menu; import android. view. view; import android. view. window; import android. view. animation. animationUtils; import android. widget. adapterView; import android. widget. adapterView. onItemSelectedListener; import android. widget. gallery; import android. widget. imageSwitcher; import android. widget. imageView; import android. widget. imageView. scaleType; import android. widget. viewSwitcher. viewFactory; public class MainActivity extends Activity implements OnItemSelectedListener, ViewFactory {private ImageSwitcher imageSwitcher; private Gallery gallery; private int id_image [] = {R. drawable. beauty1, R. drawable. beauty2, R. drawable. beauty3, R. drawable. beauty4, R. drawable. beauty5, R. drawable. beauty6, R. drawable. beauty7, R. drawable. beauty8, R. drawable. beauty9}; private MyAdapter myAdapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); gallery = (Gallery) findViewById (R. id. id_gallery); imageSwitcher = (ImageSwitcher) findViewById (R. id. id_imageSwitcher); myAdapter = new MyAdapter (this, id_image); imageSwitcher. setFactory (this); gallery. setOnItemSelectedListener (this); // sets the fade-in and fade-out effect imageSwitcher. setInAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_in); imageSwitcher. setOutAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_out); gallery. setAdapter (myAdapter); // do not forget to set the initial position of the gallery to the center. setSelection (id_image.length * 100);} @ Overridepublic void onItemSelected (AdapterView
Parent, View view, int position, long id) {// TODO Auto-generated method stubimageSwitcher. setBackgroundResource (id_image [position % id_image.length]);} @ Overridepublic void onNothingSelected (AdapterView
Parent) {// TODO Auto-generated method stub} @ Overridepublic View makeView () {// TODO Auto-generated method stubImageView image = new ImageView (this); image. setScaleType (ScaleType. FIT_CENTER); return image ;}}