Use of Gallery and ImageSwitcher in Android

Source: Internet
Author: User

Use of Gallery and ImageSwitcher in Android

The effect is as follows:

The layout file activity_main.xml is as follows:


      
       
       
   
  

The MainActivity. java code is as follows:

Import android. app. actionBar. layoutParams; import android. app. activity; import android. OS. bundle; import android. view. view; 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. viewSwitcher. viewFactory; public class MainActivity extends Activity {private int imageId [] = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e, R. drawable. f, R. drawable. g, R. drawable. h, R. drawable. i, R. drawable. j, R. drawable. k}; private ImageSwitcher imageSwitcher; private Gallery gallery; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageSwitcher = (ImageSwitcher) this. findViewById (R. id. imageSwitcher); gallery = (Gallery) this. findViewById (R. id. gallery1); // sets the animation effect imageSwitcher. setInAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_in); imageSwitcher. setOutAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_out); imageSwitcher. setFactory (new ViewFactory () {@ Override public View makeView () {ImageView imageView = new ImageView (MainActivity. this); imageView. setScaleType (ImageView. scaleType. FIT_CENTER); // set to keep the aspect ratio in the center imageView. setLayoutParams (new ImageSwitcher. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); return imageView ;}}); GalleryAdapter adapter = new GalleryAdapter (MainActivity. this, imageId); gallery. setAdapter (adapter); gallery. setSelection (imageId. length/2); gallery. setOnItemSelectedListener (new OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView
  Parent, View view, int position, long id) {imageSwitcher. setImageResource (imageId [position]) ;}@ Override public void onNothingSelected (AdapterView
  Arg0 ){}});}}

One of the required adapters:

Import android. content. context; import android. content. res. typedArray; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. imageView; public class GalleryAdapter extends BaseAdapter {private int [] imageId; private Context mContext; /*** input the Context and image resource array * @ param mContext * @ param imageId */public GalleryAdapter (Context mContext, int [] imageId) {this. mContext = mContext; this. imageId = imageId;} @ Override public int getCount () {return imageId. length ;}@ Override public Object getItem (int position) {return imageId [position] ;}@ Override public long getItemId (int position) {return position ;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView1; if (convertView = null) {imageView1 = new ImageView (mContext); imageView1.setScaleType (ImageView. scaleType. FIT_XY); imageView1.setLayoutParams (new Gallery. layoutParams (180,135); TypedArray typedArray = mContext. obtainStyledAttributes (R. styleable. gallery); imageView1.setBackgroundResource (typedArray. getResourceId (R. styleable. gallery_android_galleryItemBackground, 0); imageView1.setPadding (5, 0, 5, 0); // set the ImageView padding} else {imageView1 = (ImageView) convertView ;} imageView1.setImageResource (imageId [position]); // return imageView1 for the image to be displayed for ImageView; // return ImageView }}

This is OK!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.