Use of gallery and Imageswitcher in Android

Source: Internet
Author: User

The effect is as follows:

The layout file Activity_main.xml as follows:

<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:paddingbottom="@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 ">    <imageswitcherandroid:id="@+id/imageswitcher"android:layout_width= "Wrap_content" android:layout_height="Wrap_content"android:layout_weight="2"android: Paddingtop="30px" >                                            </imageswitcher>    <Galleryandroid:id= "@+id/gallery1"android:layout_width="Match_ Parent "android:layout_height=" Wrap_content "android:layout_weight=" 1 " android:spacing="5px"android:unselectedalpha="0.6" />                                                </relativelayout>

The Mainactivity.java code is as follows:

Import Android. App. ActionBar. Layoutparams;Import Android. App. Activity;Import Android. OS. Bundle;Import Android. View. View;Import Android. View. Animation. Animationutils;Import Android. Widgets. Adapterview;Import Android. Widgets. Adapterview. Onitemselectedlistener;Import Android. Widgets. Gallery;Import Android. Widgets. Imageswitcher;Import Android. Widgets. ImageView;Import Android. Widgets. Viewswitcher. Viewfactory;public class Mainactivity extends Activity {private int imageid[] = new int[] {R. drawable. A+ U. drawable. bR. drawable. C+ U. drawable. D+ U. drawable. E-3. drawable. FR. drawable. G-3. drawable. h-3. drawable. I-3. drawable. JR. drawable. K};Private Imageswitcher Imageswitcher;Private Gallery Gallery;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Imageswitcher = (Imageswitcher) This. Findviewbyid(R. ID. Imageswitcher);Gallery = (gallery) This. Findviewbyid(R. ID. Gallery1);Set the animation effect Imageswitcher. Setinanimation(animationutils. Loadanimation(This, Android. R. Anim. Fade_in));Imageswitcher. Setoutanimation(animationutils. Loadanimation(This, Android. R. Anim. Fade_out));Imageswitcher. Setfactory(New Viewfactory () {@Override public View Makeview () {ImageView ImageView = new Ima Geview (mainactivity. this);ImageView. Setscaletype(ImageView. ScaleType. FIT_center);//Set Keep aspect ratio centeredImageView. Setlayoutparams(New Imageswitcher. Layoutparams(Layoutparams. WRAP_content, Layoutparams. WRAP_content));Return ImageView;}        });Galleryadapter adapter = new Galleryadapter (mainactivity. this, imageId);Gallery. Setadapter(adapter);Gallery. SetSelection(imageId. Length/2);Gallery. Setonitemselectedlistener(New Onitemselectedlistener () {@Override public void onitemselected (adapterview<?> parent, Vi EW view,int position, long id) {Imageswitcher. Setimageresource(Imageid[position]);} @Override public void onnothingselected (Adapterview<?> arg0) {}});}}

One of the adapters required:

ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Gallery;ImportAndroid.widget.ImageView; Public  class galleryadapter extends baseadapter {    Private int[] imageId;PrivateContext Mcontext;/** * Enter an array of context and picture resources * @param mcontext * @param imageId * *     Public Galleryadapter(Context Mcontext,int[] imageId) { This. Mcontext = Mcontext; This. imageId = ImageId; }@Override     Public int GetCount() {returnImageid.length; }@Override     PublicObjectGetItem(intPosition) {returnImageid[position]; }@Override     Public Long Getitemid(intPosition) {returnPosition }@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {ImageView imageView1;if(Convertview = =NULL) {ImageView1 =NewImageView (Mcontext);            Imageview1.setscaletype (ImageView.ScaleType.FIT_XY); Imageview1.setlayoutparams (NewGallery.layoutparams ( the,135));            TypedArray TypedArray = mcontext. Obtainstyledattributes (R.styleable.gallery); Imageview1.setbackgroundresource (Typedarray.getresourceid (r.styleable.gallery_android_galleryitembackg Round0)); Imageview1.setpadding (5,0,5,0);//Set the inner margin of the ImageView}Else{imageView1 = (ImageView) Convertview; } imageview1.setimageresource (Imageid[position]);//Set the picture to be displayed for ImageView        returnImageView1;//Return to ImageView}}

To this ok!

Use of gallery and Imageswitcher in Android

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.