Android Foundation Top6_2:gallery +image Complete Gallery

Source: Internet
Author: User

Activity:

1<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"2xmlns:tools="Http://schemas.android.com/tools"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical"6Android:paddingbottom="@dimen/activity_vertical_margin"7android:paddingleft="@dimen/activity_horizontal_margin"8android:paddingright="@dimen/activity_horizontal_margin"9android:paddingtop="@dimen/activity_vertical_margin"Tentools:context="com.example.top6_2.MainActivity"> One<Gallery AAndroid:id="@+id/gly" -android:layout_height="wrap_content" -Android:layout_width="fill_parent" theandroid:animationduration=" +" -android:spacing="20DP" -Android:unselectedalpha="0.5" -/> +<ImageView -Android:id="@+id/img" +android:layout_height="wrap_content" AAndroid:layout_width="wrap_content" at/> -  -</LinearLayout>

Java:

Minactivity:

1 Package Com.example.top6_2;2 3 import Android. R.integer;4 import android.app.Activity;5 import Android.os.Bundle;6 import Android.view.Menu;7 import Android.view.MenuItem;8 import Android.view.View;9 import Android.widget.AdapterView;Ten import Android.widget.AdapterView.OnItemClickListener; One import Android.widget.AdapterView.OnItemSelectedListener; A import android.widget.Gallery; - import Android.widget.ImageView; -  the  -  Public classMainactivity extends Activity { -     PrivateGallery gal; -     PrivateImageView IV; +     PrivateImageadapter Adapter;  -     Private StaticFinalint[] imgslds={ + R.DRAWABLE.BJL, A R.DRAWABLE.DFHGC, at R.DRAWABLE.FG, - R.drawable.gj -     }; - @Override -     protected voidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); in Setcontentview (r.layout.activity_main); -iv=(ImageView) Findviewbyid (r.id.img); toGal=(Gallery) Findviewbyid (r.id.gly); +Adapter=NewImageadapter ( This); - Gal.setadapter (adapter); the Gal.setonitemselectedlistener (gailidstener); *     } $         PrivateGallery.onitemselectedlistener gailidstener=NewGallery.onitemselectedlistener () {Panax Notoginseng  - @Override the          Public voidOnitemselected (adapterview<?>Parent, view view, +                 intPositionLongID) { A             //TODO auto-generated Method Stub the Iv.setimageresource (imgslds[position]); +         } -  $ @Override $          Public voidOnnothingselected (adapterview<?>parent) { -             //TODO auto-generated Method Stub -              the         } -     };Wuyi}

Imagradapter:

1 Package Com.example.top6_2;2 3 import Android. R.integer;4 import Android.content.Context;5 import android.view.Gravity;6 import Android.view.View;7 import Android.view.ViewGroup;8 import Android.widget.BaseAdapter;9 import android.widget.Gallery;Ten import Android.widget.ImageView; One  A  Public classImageadapter extends Baseadapter { -     Privatecontext context; -      the     Private StaticFinalint[] imgslds={ - R.DRAWABLE.BJL, - R.DRAWABLE.DFHGC, - R.DRAWABLE.FG, + R.drawable.gj - }; +      PublicImageadapter (Context c) { Acontext=C; at          -     } -      Public intGetCount () { -         //TODO auto-generated Method Stub -         // -         returnimgslds.length; in          -     } to  + @Override -      PublicObject GetItem (intposition) { the         //TODO auto-generated Method Stub *         returnposition; $     }Panax Notoginseng  - @Override the      Public LongGetitemid (intposition) { +         //TODO auto-generated Method Stub A         returnposition; the     } +  - @Override $      PublicView GetView (intposition, View Convertview, ViewGroup parent) { $         //TODO auto-generated Method Stub -ImageView iv=NewImageView (context); - Iv.setimageresource (imgslds[position]); the Iv.setscaletype (ImageView.ScaleType.FIT_CENTER); -Iv.setlayoutparams (NewGallery.layoutparams ( -, the));Wuyi                     returnIV; the     } -  Wu}

:

Android Foundation Top6_2:gallery +image Complete 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.