Android ImageSwitcher and imageswitcher
<?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"> <ImageSwitcher android:id="@+id/switcher" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="#ff00ff" android:gravity="center_vertical" android:spacing="16dp" /></RelativeLayout>
Package com. example. yanlei. mytk; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. button; import android. widget. spinner; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. context; import android. OS. bundle; import and Roid. 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. viewSwitcher; public class MainActivity extends AppCompatActivity implements Vie WSwitcher. viewFactory {private ImageSwitcher mSwitcher; private Gallery gallery; private Context mContext; private Integer [] mImageIds = {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, R. drawable. sample_8, R. drawable. sample_9, R. drawable. sample_10, R. drawable. sample_11}; @ Over Ride protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); setTitle ("browse image"); mSwitcher = (ImageSwitcher) findViewById (R. id. switcher); gallery = (Gallery) findViewById (R. id. gallery); mSwitcher. setFactory (this); mSwitcher. setInAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade _ In); mSwitcher. setInAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_out); gallery. setAdapter (new ImageAdapter (this); gallery. setOnItemSelectedListener (new AdapterView. onItemSelectedListener () {@ Override public void onItemSelected (AdapterView <?> Adapter, View view, int position, long id) {mSwitcher. setImageResource (mImageIds [position]) ;}@ Override public void onNothingSelected (AdapterView <?> Arg0) {}});} public class ImageAdapter extends BaseAdapter {public ImageAdapter (Context context) {mContext = context ;}@ Override public int getCount () {return mImageIds. length ;}@ Override public Object getItem (int position) {return position ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, view convertView, ViewGroup parent) {ImageView I = new ImageView (mContext); I. setImageResource (mImageIds [position]); I. setAdjustViewBounds (true); I. setLayoutParams (new Gallery. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); I. setBackgroundResource (R. drawable. picture_frame); return I ;}@ Override public View makeView () {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 ;}}