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

Source: Internet
Author: User
Tags set background

Imageswitcher component of the main function is to complete the display of the picture, such as when the user in the picture browsing, you can click on a button to toggle the display of pictures, and use the Imageswitcher component in each switch can also add some animation effect, This class is defined as follows: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 that is used to convert viewswitcher when two picture transitions are performed
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


If you want to implement the switch function of the picture, the defined activity class must also implement the Viewswitcher.viewfactory interface to specify the operation factory that switches the view, which is defined as follows: Public Static Interfaceviewswitcher.viewfactory {/***Create a newViewdisplay, and add it to theViewswitcheramong* @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_he ight= "Wrap_content" Android:layout_alignbaselIne= "@+id/button2" android:layout_alignbottom= "@+id/button2" android:layout_marginright= "20DP" Androi        d:layout_toleftof= "@+id/imageswitcher1" android:text= "previous"/> <button android:id= "@+id/button2" Style= "? Android:attr/buttonstylesmall" Android:layout_width= "wrap_content" android:layout_height= "Wrap_c Ontent "android:layout_centervertical=" true "android:layout_torightof=" @+id/imageswitcher1 "android:t Ext= "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;//initialize button private Imageswitcher imageswitcher;//initialize 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 Component Factory Imageswitcher.setinanimation (Animationutils.loadanimation (Mainactivity.this, Android) for the component. 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 ();//Set 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 (New Imageswitcher.layoutparams (//Adaptive picture 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 available} else {this. Butnext.setenabled (FALSE); button is not available}if (This.foot = = 0) {this. Butprevious.setenabled (FALSE); The button is not available} else {this. Butprevious.setenabled (TRUE); Button available}}}



+
The textswitcher is basically the same as the component, no specific introduction, and the reader can practice on its own
Forecast for next section: Gallery drag Assembly

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.