Android Study Notes: imageswitcher

Source: Internet
Author: User

We have previously introduced a thumbnail control Gallery of Android and a grid display space, both of which use images as examples. However, the controls that we actually use to display images are called imageswitcher, as the name implies, it is an image converter. We often use it to display images in the android UI. Of course, we can also use imageview for operations, but imageswitcher has some specific functions, that is, it can add some animation effects when converting images.

The declaration in the layout is as simple as a clock control.

<ImageSwitcher     android:id="@+id/imageSwitcher"     android:layout_width="fill_parent"     android:layout_height="wrap_content" />

Let's first kidnap it and then introduce several important methods to it.

ImageSwitcher mSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);mSwticher.setFactory(this);

 

As shown above, the most important thing to use imageswitcher is to specify a viewfactory for it, that is, to define how it displays the content, the general practice is to use the imageswitcher class to implement the viewfactory interface and overwrite the corresponding makeview method.

public View makeView() { ImageView image = new ImageView(this);         image.setMinimumHeight(200);         image.setMinimumWidth(200);         image.setScaleType(ImageView.ScaleType.FIT_CENTER);         image.setLayoutParams(new ImageSwitcher.LayoutParams(                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));         return image; }

 

Next, add the animation effect.

mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,         android.R.anim.fade_in)); mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,         android.R.anim.fade_out));

After the image is displayed, we can put the modification method into event processing to form an interactive effect that triggers image conversion.

 

 

Package xiaosi. iamgeswitcher; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. view. viewgroup. layoutparams; import android. view. window; import android. view. animation. animationutils; import android. widget. adapterview; import android. widget. adapterview. onitemselectedlistener; import android. widget. basea Dapter; import android. widget. gallery; import android. widget. imageswitcher; import android. widget. imageview; import android. widget. viewswitcher. viewfactory; public class imageswitcheractivity extends activity implements viewfactory {private imageswitcher; private gallery Gallery; // image set Private integer [] images = {R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. f };@ overrideprotected void Oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setcontentview (R. layout. main); imageswitcher = (imageswitcher) findviewbyid (R. id. switcher); // specify a viewfactory for it, that is, to define how it displays the content, implement the viewfactory interface, and overwrite the corresponding makeview method. Imageswitcher. setfactory (this); // Add the animation effect imageswitcher. setinanimation (animationutils. loadanimation (this, android. r. anim. fade_in); imageswitcher. setoutanimation (animationutils. loadanimation (this, android. r. anim. fade_out); gallery = (Gallery) findviewbyid (R. id. gallery); // Add the adapter Gallery. setadapter (New imageadapter (this); // sets the listener Gallery. setonitemselectedlistener (New onitemselectedlistener ();} // rewrite makeview () Method public view makeview () {imageview = new imageview (this); imageview. setbackgroundcolor (0xff000000); // sets the filling mode imageview. setscaletype (imageview. scaletype. fit_xy); imageview. setlayoutparams (New imageswitcher. layoutparams (layoutparams. match_parent, layoutparams. match_parent); Return imageview;} // adapter public class imageadapter extends baseadapter {private context mcontext; Public imageadapter (con Text C) {mcontext = C;} public int getcount () {return images. length;} public object getitem (INT position) {return position;} public long getitemid (INT position) {return position;} public view getview (INT position, view convertview, viewgroup parent) {imageview = new imageview (mcontext); imageview. setimageresource (images [position]); imageview. setadjustviewbounds (true); imageview. setlayoutparams (New gallery. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); imageview. setbackgroundresource (R. drawable. e); Return imageview;} private class onitemselectedlistener implements onitemselectedlistener {public void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3) {imageswitcher. setimageresource (images [arg2]);} public void onnothingselected (adapterview <?> Arg0 ){}}}

 

Main. xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">         <ImageSwitcher android:id="@+id/switcher"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true"    />        <Gallery android:id="@+id/gallery"        android:background="#55000000"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"                android:gravity="center_vertical"        android:spacing="16dp"    /></RelativeLayout>

 

 

Source code download: Click to download

 

 

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.