Android learning --- Gallery View

Source: Internet
Author: User

Android learning --- Gallery View

Gallery and Spinner share the same parent class: AbsPinner, indicating that Gallery and Spinner are both a list box. The difference between them is that the Spinner displays a vertical list selection box, while the Gallery displays a horizontal list selection box. The role of the Spinner is to allow users to choose from, while Gallery allows users to drag to view the previous one and the next one.

The Gallery usage is similar to the usage of the Spinner. You only need to provide a content Adapter for it. The getView method of the Adapter returns the View as the list item of the Gallery list. If the program needs to monitor changes in the Gallery selection, you can add the OnItemSelectedListener listener.


Gallery xml attributes


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPs/Cw + signature + cda-vcd4kpha + signature + CjxwPjxwcmUgY2xhc3M9 "brush: java;">

(2) MainActivity. java

Package com. example. gallery; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. view. animation. animationUtils; import android. widget. adapterView; import android. widget. adapterView. onItemSelectedListener; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. imageSwitcher; import android. widget. imageView; import android. widget. radioGroup. layoutParams; import android. widget. viewSwitcher. viewFactory; public class MainActivity extends Activity {// define the private ImageSwitcher imgSwt = null; private Gallery gallery = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the layout component imgSwt = (ImageSwitcher) findViewById (R. id. imgSwt); gallery = (Gallery) findViewById (R. id. gallery); // image final int images [] = new int [] {R. drawable. name01, R. drawable. name02, R. drawable. name03, R. drawable. name04, R. drawable. name05, R. drawable. name06, R. drawable. name07, R. drawable. name08, R. drawable. name09, R. drawable. name10, R. drawable. name11, R. drawable. name12}; // sets the image switching effect imgSwt. setInAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_in); imgSwt. setOutAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_out); // sets the ViewFactory object imgSwt. setFactory (new ViewFactory () {@ Overridepublic View makeView () {ImageView imageView = new ImageView (MainActivity. this); imageView. setScaleType (ImageView. scaleType. FIT_CENTER); imageView. setLayoutParams (new ImageSwitcher. layoutParams (350,350); return imageView ;}}); // creates a BaseAdapter object and provides the Gallery function to display all images. BaseAdapter adapter = new BaseAdapter () {@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// create imageviewImageView imageView = new ImageView (MainActivity. this); imageView. setImageResource (images [position]); imageView. setScaleType (ImageView. scaleType. FIT_XY); imageView. setLayoutParams (new Gallery. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); return imageView ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic int getCount () {return images. length ;}}; // set the adapter Gallery for gallery. setAdapter (adapter); // Add event gallery. setOnItemSelectedListener (new OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView
 Parent, View view, int position, long id) {imgSwt. setImageResource (images [position]) ;}@ Overridepublic void onNothingSelected (AdapterView
 Parent) {// TODO Auto-generated method stub }});}}

The running effect is as follows:


Related Article

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.