Gallery of Android

Source: Internet
Author: User

1. Add Galleryactivity_main.xml to the xml layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Gallery        android:id="@+id/gallery"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>


2. Custom ImageAdapterImageAdapter. java
Package com. example. gallery; import java. util. list; import android. content. context; import android. content. res. typedArray; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. imageView; @ SuppressWarnings ("deprecation") public class ImageAdapter extends BaseAdapter {private Context context; private List <Integer> list; private TypedArray typedArray; private int item_background; public ImageAdapter (Context context Context, list <Integer> list) {this. context = context; this. list = list; this. typedArray = context. obtainStyledAttributes (R. styleable. gallery_style); item_background = typedArray. getResourceId (R. styleable. gallery_style_android_galleryItemBackground, 0); typedArray. recycle () ;}@ Override public int getCount () {return list. size () ;}@ Override public Object getItem (int position) {return position ;}@ Override public long getItemId (int position) {return position ;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView (context); // sets the imageView of the displayed image. setImageResource (list. get (position); // you can specify the size of the image to be scaled. setScaleType (ImageView. scaleType. FIT_XY); // sets the layout parameter imageView. setLayoutParams (new Gallery. layoutParams (150,100); // sets the background border imageView. setBackgroundResource (item_background); return imageView ;}}



3. The background parameter res/values/attrs. xml of each ImageView
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="gallery_style">        <attr name="android:galleryItemBackground" />    </declare-styleable></resources>


4. Bind data and set the listener MainActivity. java in MainActivity
Package com. example. gallery; import java. util. arrayList; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. gallery; import android. widget. toast; @ SuppressWarnings ("deprecation") public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState ); SetContentView (R. layout. activity_main); Gallery gallery = (Gallery) findViewById (R. id. gallery); ArrayList <Integer> list = new ArrayList <Integer> (); list. add (R. drawable. img1); list. add (R. drawable. img2); list. add (R. drawable. img3); list. add (R. drawable. img4); list. add (R. drawable. img5); list. add (R. drawable. img6); list. add (R. drawable. img7); ImageAdapter adapter = new ImageAdapter (this, list); gallery. setAdapter (Adapter); gallery. setOnItemSelectedListener (new AdapterView. OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView <?> Parent, View v, int position, long id) {Toast. makeText (getApplicationContext (), "selected:" + String. valueOf (position), Toast. LENGTH_SHORT ). show () ;}@ Override public void onNothingSelected (AdapterView <?> Arg0) {// No response here }});}}


5. Image resource note: it is best to use a png image. Because jpg is a compressed image, decompression in android may cause memory overflow errors.6. Result Display


Note: Please indicate the source for reprinting. After all, the code is coded one by one ~



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.