Simple tutorial on Android-gun 10 (Gallery component Gallery practical research) and androidgallery

Source: Internet
Author: User

Simple tutorial on Android-gun 10 (Gallery component Gallery practical research) and androidgallery

The Gallery component is used to drag and drop images. Let's take a look at how it is implemented.

1. Implement Gallery

1. The layout file is simple:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/MyLayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FFFFFF"    android:orientation="vertical" >    <Gallery        android:id="@+id/myGallery"        android:gravity="center_vertical"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>


2. Custom adapter class. You can directly override several methods in the BaseAdapter class.

Package org. yayun. demo; import android. content. context; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. imageView; import android. widget. gallery. layoutParams; public class ImageGalleryAdapter extends BaseAdapter {private Context context; private int imgRes [] = new int [] {R. drawable. ispic_a, R. drawable. ispic_ B, R. drawable. ispic_c, R. drawable. ispic_d, R. drawable. ispic_e,}; public ImageGalleryAdapter (Context c) {// constructor, used to obtain the Context object this. context = c;} public int getCount () {return imgRes. length;} public Object getItem (int position) {return imgRes [position];} public long getItemId (int position) {return imgRes [position];} public View getView (int position, view convertView, ViewGroup parent) {ImageView img = new ImageView (this. context); img. setBackgroundColor (0 xFFFFFFFF); img. setImageResource (this. imgRes [position]); // sets the resource img. setScaleType (ImageView. scaleType. CENTER); // CENTER the img. setLayoutParams (new Gallery. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); return img ;}}


3. MainActivity. java:

Package org. yayun. demo; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. toast; import android. widget. adapterView. onItemClickListener; import android. widget. gallery; public class MainActivity extends Activity {private Gallery gallery; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState );/ /Life cycle method super. setContentView (R. layout. main); // set the layout manager gallery = (Gallery) findViewById (R. id. myGallery); gallery. setAdapter (new ImageGalleryAdapter (this); gallery. setOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (MainActivity. this, "the number you selected" + String. valueOf (position + 1) + "Images", Toast. LENGTH_SHORT ). show ();}});}}


4. Run the following instance:

2. Integration of Gallery and ImageSwitcher

In this case, we use the SimpleAdapter class to complete the Gallery.

1. layout file:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/MyLayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FFFFFF"    android:gravity="bottom"    android:orientation="vertical" >    <ImageSwitcher        android:id="@+id/imageSwitcher"        android:layout_width="fill_parent"        android:layout_height="wrap_content" >    </ImageSwitcher>    <Gallery        android:id="@+id/gallery"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:spacing="5dp" /></LinearLayout>


2. Define a display template:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="#FFFFFF"    android:orientation="horizontal" >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:scaleType="center" /></LinearLayout>


3. MainActivity. java program:

Package org. yayun. demo; import java. lang. reflect. field; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. imageSwitcher; import android. widget. imageView; import android. widget. simpleAdapter; import android. widget. toast; import Ndroid. widget. adapterView. onItemClickListener; import android. widget. gallery. layoutParams; import android. widget. gallery; import android. widget. viewSwitcher. viewFactory; public class MainActivity extends Activity {private Gallery gallery; private List <Map <String, Integer> list = new ArrayList <Map <String, Integer> (); private SimpleAdapter simpleAdapter; private ImageSwitcher imageSwitcher; public void on Create (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // life cycle method super. setContentView (R. layout. main); // set the layout manager initAdapter (); gallery = (Gallery) findViewById (R. id. gallery); imageSwitcher = (ImageSwitcher) findViewById (R. id. imageSwitcher); imageSwitcher. setFactory (new ViewFactory () {public View makeView () {ImageView imageView = new ImageView (MainActivity. this); imageView. setBackgro UndColor (0 xFFFFFFFF); imageView. setScaleType (ImageView. scaleType. CENTER); imageView. setLayoutParams (new ImageSwitcher. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); return imageView ;}}); gallery. setAdapter (simpleAdapter); gallery. setOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Map <String, Integer> map = (Map <String, Integer>) MainActivity. this. simpleAdapter. getItem (position); // retrieves mapMainActivity. this. imageSwitcher. setImageResource (map. get ("img"); // set the display image});} private void initAdapter () {Field [] fields = R. drawable. class. getDeclaredFields (); // the Java reflection mechanism obtains all resource images for (int I = 0; I <fields. length; I ++) {if (fields [I]. getName (). startsWith ("ispic _") {// start Map <String, Integer> map = new HashMap <String, Integer> (); try {map. put ("img", fields [I]. getInt (R. drawable. class);} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke. printStackTrace ();} this. list. add (map) ;}} simpleAdapter = new SimpleAdapter (this, this. list, R. layout. grid_layout, new String [] {"img"}, new int [] {R. id. img });}}


4. Run the instance:

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.