Learn Android<imageswitcher picture switching components from scratch. 26 .>

Source: Internet
Author: User
Tags set background

The main function of the Imageswitcher component is to complete the switch display of the picture, such as when the user is doing the picture browsing. The ability to toggle the displayed picture with the button by clicking on a sheet, and using the Imageswitcher component to add some animations to each switch, such as the following:Java.lang.Object?Android.view.View?Android.view.ViewGroup?Android.widget.FrameLayout?Android.widget.ViewAnimator?Android.widget.ViewSwitcher?Android.widget.ImageSwitcher
the method used
1 Public Imageswitcher (Context context) Structure Create a Imageswitcher object
2 public void Setfactory (Viewswitcher.viewfactory factory) Ordinary Sets the Viewfactory object. Used to complete the conversion operation of the Viewswitcher when two images are toggled
3 public void Setimageresource (int resid) Ordinary Set the picture resource ID that is displayed
4 public void Setinanimation (Animation inanimation) Ordinary The animation effect when the picture is read into the Imageswitcher
5 public void Setoutanimation (Animation outanimation) Ordinary The animated effect of the picture from Imageswitcher to disappear


Suppose you want to implement the switch function of the picture. The activity class defined must also implement the Viewswitcher.viewfactory interface to specify the operation factory that switches views, such as the following: Public Static Interfaceviewswitcher.viewfactory {/***Create a newViewdisplay, and add it toViewswitcheramong* @returnthe newViewObject*/

Public AbstractView Makeview ();}
Such as Private classViewfactoryimpl Implementsviewfactory {@Override PublicView Makeview () {ImageView img = NewImageView (Myimageswitcherdemo. This);// Realexample of a picture displayImg.setbackgroundcolor (0xFFFFFFFF);// Set Background colorImg.setscaletype (Imageview.scaletype. CENTER);//Center DisplayImg.setlayoutparams ( NewImageswitcher.layoutparams (// fromFit picture SizeLayoutparams. fill_parent, Layoutparams. fill_parent));// Defining Components returnimg;}}
XML file
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ".         Mainactivity "> <imageswitcher android:id=" @+id/imageswitcher1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" Android:layout_centerhoriz Ontal= "true" android:layout_margintop= "114DP" > </ImageSwitcher> <button android:id= "@+id/ Button1 "style="?

Android:attr/buttonstylesmall "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:layout_alignbaseline= "@+id/button2" android:layout_alignbottom= "@+id/button2" Android:layout_marg inright= "20DP" android:layout_toleftof= "@+id/imageswitcher1" android:text= "previous"/> <button a Ndroid:id= "@+id/button2" style= "Android:attr/buttonstylesmall" android:layout_width= "Wrap_content" a ndroid:layout_height= "Wrap_content" android:layout_centervertical= "true" android:layout_torightof= "@+id/imag ESwitcher1 "android:text=" Next "/></relativelayout>



Java files
Package Com.example.imageswitcher;import Android.app.activity;import Android.graphics.color;import Android.os.bundle;import Android.view.view;import Android.view.viewgroup.layoutparams;import Android.view.animation.animationutils;import Android.widget.button;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.viewswitcher.viewfactory;public class MainActivity extends Activity { Private Button Butnext, butprevious;//initialization buttonprivate imageswitcher imageswitcher;//initialization Component private int images[] = { R.DRAWABLE.A1, R.drawable.a2, r.drawable.a3,r.drawable.a4, R.drawable.a5, r.drawable.a6};//set picture data private int foot = 0 ;//Set the corner mark @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); imageswitcher = (Imageswitcher) This.findviewbyid (R.id.imageSwitcher1);// Get Component Butnext = (Button) This.findviewbyid (R.id.button1); Butprevious = (Button) This.findviewbyid (r.id.button2); imageswitcher.setfactory(New MyFactory ());//Set the component factory Imageswitcher.setinanimation for the component (Animationutils.loadanimation (Mainactivity.this, Android. r.anim.fade_in));//Set the picture into the animated imageswitcher.setoutanimation (Animationutils.loadanimation (Mainactivity.this, Android. r.anim.fade_out));//Set picture to leave animation Imageswitcher.setimageresource (images[foot++]);//Set picture// Button Event Listener Butnext.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (view view) {//TODO Auto-generated method Stubimageswitcher.setimageresource (images[foot++]); MainActivity.this.CheckEnable ();//sets whether button is available to prevent array out of bounds});//button Event Listener Butprevious.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubimageswitcher.setimageresource (images[foot--]); MainActivity.this.CheckEnable ();}});}  Class MyFactory implements Viewfactory {@Overridepublic View Makeview () {//TODO auto-generated method Stubimageview image = new ImageView (mainactivity.this);//Set Picture component Image.setbackgroundcolor (Color.gray);//Set alignment effect image.Setscaletype (ImageView.ScaleType.CENTER);//Set image.setlayoutparams in the play (New Imageswitcher.layoutparams (// Adaptive image size Layoutparams.fill_parent, layoutparams.fill_parent)); Defines the component return image;//returns the picture}}public void Checkenable () {if (This.foot < this. IMAGES.LENGTH-1) {this. Butnext.setenabled (TRUE); button can be used} else {this. Butnext.setenabled (FALSE); button is not available}if (This.foot = = 0) {this. Butprevious.setenabled (FALSE); button is not available} else {this. Butprevious.setenabled (TRUE); Button available}}}



+
The textswitcher is basically the same as the operation of the component. No more detailed introduction, the reader can practice their own
Forecast for next section: Gallery drag Assembly

Learn Android&lt;imageswitcher picture switching components from scratch. 26 .&gt;

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.