Android study notes 23: slides

Source: Internet
Author: User

You can use gallary and imageswitcher to view slide images in Android, for example, windows.

Use gallary as the image selection part and imageswitcher as the image display part.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Define an imageswitcher component --> <imageswitcher Android: Id = "@ + ID/switcher" Android: layout_width = "320dp" Android: layout_height = "320dp"/> <! -- Define a gallery component --> <gallery Android: Id = "@ + ID/Gallery" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margintop = "25dp" Android: unselectedalpha = "0.6" Android: spacing = "3pt"/> </linearlayout>

Customize a baseadapter as the gallary data source, set a setonitemselectedlistener as the listener, change the image in imageswitcher in real time, and use tyoedarray to save the attribute values in the gallery.

Public class gallarytest extends activity {int [] imageids = new int [] {R. drawable. shuangzi, R. drawable. shuangyu, R. drawable. chunv, R. drawable. tiancheng, R. drawable. tianxie, R. drawable. sheshou, R. drawable. juxie, R. drawable. shuiping, R. drawable. shizi, R. drawable. yang, R. drawable. jinniu, R. drawable. mojie}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcont Entview (R. layout. main); Final gallery = (Gallery) findviewbyid (R. id. gallery); // obtain the imageswitcher object final imageswitcher switcher = (imageswitcher) findviewbyid (R. id. switcher); // set the viewfactory object Switcher for the imageswitcher object. setfactory (New viewfactory () {@ overridepublic view makeview () {imageview = new imageview (gallarytest. this); imageview. setbackgroundcolor (0xff0000); imageview. setscalet Ype (imageview. scaletype. fit_center); imageview. setlayoutparams (New imageswitcher. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); Return imageview ;}}); // sets the animation effect Switcher for image replacement. setinanimation (animationutils. loadanimation (this, android. r. anim. fade_in); switcher. setoutanimation (animationutils. loadanimation (this, android. r. anim. fade_out); // creates a baseadapter object that provides the baseadap for the image displayed by Gallery. TER adapter = new baseadapter () {@ overridepublic int getcount () {return imageids. length ;}@ overridepublic object getitem (INT position) {return position ;}@ overridepublic long getitemid (INT position) {return position ;} // The returned view of this method represents each list item @ overridepublic view getview (INT position, view convertview, viewgroup parent) {// create an imageviewimageview imageview = new imageview (gallarytest. this); imageview. setima Geresource (imageids [position % imageids. length]); // sets the Image view scaling type. setscaletype (imageview. scaletype. fit_xy); // increase or decrease the image size to display the imageview. setlayoutparams (new gallery. layoutparams (75,100); // use tyoedarray to save the property value typedarray = obtainstyledattributes (R. styleable. gallery); imageview. setbackgroundresource (typedarray. getresourceid (R. styleable. gallery_android_galleryitemba Ckground, 0); Return imageview ;}}; Gallery. setadapter (adapter); Gallery. setonitemselectedlistener (New onitemselectedlistener () {// This method is triggered when the selected gallery item changes @ overridepublic void onitemselected (adapterview <?> Parent, view, int position, long ID) {switcher. setimageresource (imageids [position % imageids. Length]) ;}@ overridepublic void onnothingselected (adapterview <?> Parent ){}});}}
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.