Chapter 2 (8) Image Library (Galery ),
Category: C #, Android, VS2015;
Created on: 1. Introduction
An Image Library (also called a gallery) is a layout widget that displays each image in a horizontally scrolling list. the currently selected image is placed in the center of the view.
Note: Android has discarded This widget because Galery is less efficient. The official suggestion is to use HorizontalScrollView instead. However, many of the Image Browsing functions on mobile phones are implemented using Galery. If you still like this widget, you can continue to use it in a later version of the project. Ii. Example 8 -- Demo08Gallery
1. Run
In the simulator, drag and drop the image to observe the effect.
2. Add the Demo08Gallery. axml file.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical"> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
Save the file and click Refresh at the top of Solution Explorer.
3. Add the Demo08Gallery. cs file.
Using System; using Android. app; using Android. content; using Android. OS; using Android. views; using Android. widget; using Java. lang; namespace ch05demos. srcActivity {[Activity (Label = "Demo08ActionBar")] public class Demo08Gallery: Activity {protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. demo08_Gallery); var g = FindViewById <Gallery> (Resource. id. gallery); g. adapter = new ImageAdapter (this) {CurrentWidth = 550, CurrentHeight = 550}; g. itemClick + = Gallery_ItemClick;} private void Gallery_ItemClick (object sender, AdapterView. itemClickEventArgs e) {Toast. makeText (this, e. position. toString (), ToastLength. short ). show () ;}} public class ImageAdapter: BaseAdapter {private Context context; private int [] thumbIds = {Resource. drawable. sample_1, Resource. drawable. sample_2, Resource. drawable. sample_3, Resource. drawable. sample_4, Resource. drawable. sample_5, Resource. drawable. sample_6, Resource. drawable. sample_7}; // The default value is 500 (this is a new function of C #6.0, which can be used only in VS2015) public int CurrentWidth {get; set ;}= 500; public int CurrentHeight {get; set; }= 500; public ImageAdapter (Context c) {context = c;} public override int Count {get {return thumbIds. length ;}} public override View GetView (int position, View convertView, ViewGroup parent) {ImageView I = new ImageView (context); I. setImageResource (thumbIds [position]); I. layoutParameters = new Gallery. layouparams (500,500); I. setScaleType (ImageView. scaleType. fitXy); return I;} public override long GetItemId (int position) {return 0;} public override Java. lang. object GetItem (int position) {return null ;}}}
Run.