The gallery component is used to drag and drop a picture, so let's look at how it's implemented.
I. Realization OF 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. Customize the adapter class to directly overwrite 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,};p ublic Imagegalleryadapter (context C) {//constructor method, used to get 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.set BackgroundColor (0xFFFFFFFF); Img.setimageresource (this.imgres[position]);//Set Resource Img.setscaletype ( ImageView.ScaleType.CENTER);//center Display 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 to use 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, "You have selected" + string.valueof (position + 1) + "picture", Toast.length_short). Show ();}});}}
4. Run the following example:
Ii. combination of gallery and Imageswitcher
At this time gallery we use the Simpleadapter class to complete.
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 the 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 Android.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>> ();p rivate SimpleAdapter Simpleadapter;private imageswitcher imageswitcher;public void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use 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.setbackgroundcolor (0xFFFFFFFF); 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<strin G, integer>) MainActivity.this.simpleAdapter.getItem (position);// Remove MapMainActivity.this.imageSwitcher.setImageResource (Map.get ("img"));//Set Display picture}}); private void Initadapter () {field[] fields = R.drawable.class.getdeclaredfields ();//Java Reflection mechanism gets all the resource picture for (int i = 0; I < ; Fields.length; i++) {if (Fields[i].getname (). StartsWith ("Ispic_")) {Judge the opening 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. Running the example:
Android Combat Simple Tutorial-tenth gun (Gallery Module Gallery Practical Research)