Use of Android Imageswitcher and gallery

Source: Internet
Author: User

Android Imageswitcher and gallery can be used to make a similar slide effect, Imageswticher control function is used to display pictures and set pictures in and out of animation, popular point, Imageswitcher is a panel to display a specific picture (view), on this panel, you can set the picture into the time of the animation, as well as the animation when leaving, gallery is a container, used to store the imageswticher inside the specific image displayed.

Layout file:

<relativelayout xmlns:android= "Http://shemas.android.com/apk/res/android"

Android:layout_width= "Match_parent"

android:layout_hegiht= "Match_paernt" >

<imageswticher <! --Declare the Imageswitcher control, used to display the picture, a bit like a picture display--

Android:id= "@+id/imgs"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

Android:layout_alignparenttop= "true" <!--settings control and parent layout top and left face-up

Android:layout_alignparentleft= "true"/>

<gallery

Android:id= "@+id/gallery"

Android:layout_width= "Fill_parent"

android:layout_hegiht= "100DP"

Android:layout_alignparentbottom= "true" <!--set control and parent layout bottom and left align

Android:layout_alignparentleft= "true"

Android:gravity= "Center_vertical"

android:spacing= "6DP"/> <!--set the distance between each part of the container--

</RelativeLayout>

Introduction to Android Code:

public class Mainactivity extends Activity implements Android.widget.ViewSwitcher.ViewFactory, Android.widget.AdapterView.OnItemSelectedListener

{

Private Imageswitcher Imgswitcher;

Private Gallery Gallery;

Private Integer imgs[] ={r.drawable.a,r.drawable.b,r.drawable.c,r.drawable.d,r.drawabe.e,r.drawable.f, r.drawable.g};//here to add your own pictures

protected void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (r.layout.mainlayout);//mainlayout for the layout file defined above

Imgswitcher = (imageswitcher) Findviewbyid (R.ID.IMGS);

Gallery = (gallery) Findviewbyid (r.id.gallery);

Imgswitcher.setinanimation (this,android. R.ANIM.FADE_IN);//Set Picture into animation

Imgswitcher.setoutanimation (this,android. R.anim.fade_out);//Set Picture exit animation

Imgswither.setfactory (this);//Set the View factory, do not know what this view factory is for! Seems to correspond with Makeview.

Galleryadapter adapter = new Galleryadaper (this);//This custom adapter is primarily used to set the data inside the gallery container

Gallery.setonitemselectedlistener (this);

Gallery.setadapter (adapter);

            

}

@Override

public void onitemselected (adapterview<?> parent,view view,int position,int ID)

{

Imgswitcher.setimageresource (Imgs[position]);//According to the gallery choice, display the corresponding picture in the Imageswitcher

}

@Override

public void onnothingselected (adapterview<?> parent)

{

}

@Override

Public View Makeview ()

{

ImageView view = new ImageView (this);

View.setbackgroundcolor (0xff000000);

View.setlayoutparams (New Imageswitcher.layoutparams (layoutparams.fill_parent,layoutparams.fill_parent));

View.setscaletype (ImageSwitcher.ScaleType.FIT_CENTER);

return view;

}

Class Galleryadapter extends Baseadapter

{

Private context context;

Galleryadaper (Context c)

{

context = C;

}

public int GetCount ()

{

return imgs.length;

}

Public Object getItem (int position)

{

return imgs[position];

}

public long getitemid (int position)

{

return position;

}

Public View getView (int position,view view,viewgroup Groupview)

{

ImageView v = new ImageView (context);

V.setimageresource (Imgs[position]);

V.setadjustviewbounds (TRUE)//setting allows resizing of the picture, the reason for setting it is that the gallery height setting in the layout file is:

/*android:layout_height= "" 100dp*, this time using the image resource height can be larger or smaller, plus this attribute, you can let gallery automatically adjust the size of the picture/

V.setlayoutparams (New Gallery.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content));

return v;

}

}

}

Use of Android Imageswitcher and gallery

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.