Home  >  Article  >  php教程  >  【Android】Chapter 5 (8) Picture Gallery (Galery)

【Android】Chapter 5 (8) Picture Gallery (Galery)

WBOY
WBOYOriginal
2016-07-06 13:30:521231browse

Category: C#, Android, VS2015; Creation date: 2016-02-07 1. Introduction The gallery (also called gallery) is a layout widget used to display each picture in a horizontally scrollable list. Currently The selected picture will be centered in the view. Note: Android has deprecated this widget. The reason for deprecation is that the efficiency of using Galery is relatively low,

Category: C#, Android, VS2015;

Creation date: 2016-02-07

1. Introduction

The Gallery (also called Gallery) is a layout widget that displays each 【Android】Chapter 5 (8) Picture Gallery (Galery) in a horizontally scrollable list, with the currently selected 【Android】Chapter 5 (8) Picture Gallery (Galery) placed in the center of the view.

Note: Android has deprecated this widget. The reason for deprecation is that the efficiency of using Galery is relatively low. The official recommendation is to use HorizontalScrollView to replace this widget. However, many 【Android】Chapter 5 (8) Picture Gallery (Galery) browsing functions on mobile phones are currently implemented using Galery. If you still like this widget, you can continue to use it in higher version projects.

2. Example 8--Demo08Gallery

1. Running screenshots

Use the mouse left and right to drag and drop the 【Android】Chapter 5 (8) Picture Gallery (Galery) to observe the effect.

【Android】Chapter 5 (8) Picture Gallery (Galery)

2. Add the Demo08Gallery.axml file

<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span>
<span style="color: #0000ff;"><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;">
    android:orientation</span><span style="color: #0000ff;">="vertical"</span><span style="color: #ff0000;">
    android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
    android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
    android:gravity</span><span style="color: #0000ff;">="center_vertical"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">Gallery
        </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/gallery"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span> <span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span></span></span>

Save the file and click the [Refresh] button above the Solution Explorer.

3. Add the Demo08Gallery.cs file

<span style="color: #0000ff;">using</span><span style="color: #000000;"> System;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.App;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Content;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.OS;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Views;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Widget;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> java.Lang;

</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ch05demos.SrcActivity
{
    [Activity(Label </span>= <span style="color: #800000;">"</span><span style="color: #800000;">Demo08Gallery</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Demo08Gallery : Activity
    {
        </span><span style="color: #0000ff;">PRotected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnCreate(Bundle savedInstanceState)
        {
            </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.demo08_Gallery);
            </span><span style="color: #0000ff;">var</span> g = FindViewById<gallery><span style="color: #000000;">(Resource.Id.gallery);
            g.Adapter </span>= <span style="color: #0000ff;">new</span> ImageAdapter(<span style="color: #0000ff;">this</span><span style="color: #000000;">)
            {
                CurrentWidth </span>= <span style="color: #800080;">550</span><span style="color: #000000;">,
                CurrentHeight </span>= <span style="color: #800080;">550</span><span style="color: #000000;">
            };
            g.ItemClick </span>+=<span style="color: #000000;"> Gallery_ItemClick;
        }

        </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Gallery_ItemClick(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, AdapterView.ItemClickEventArgs e)
        {
            Toast.MakeText(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">, e.Position.ToString(), ToastLength.Short).Show();
        }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> ImageAdapter : BaseAdapter
    {
        </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Context context;
        </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span>[] thumbIds =<span style="color: #000000;"> {
            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
        };

        </span><span style="color: #008000;">//</span><span style="color: #008000;">默认值为500(这是C# 6.0新增的功能,仅VS2015可以这样用)</span>
         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> CurrentWidth { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #800080;">500</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> CurrentHeight { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #800080;">500</span><span style="color: #000000;">;

        </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ImageAdapter(Context c)
        {
            context </span>=<span style="color: #000000;"> c;
        }

        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> Count
        {
            </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span><span style="color: #000000;"> thumbIds.Length; }
        }

        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> View GetView(<span style="color: #0000ff;">int</span><span style="color: #000000;"> position, View convertView, ViewGroup parent)
        {
            ImageView i </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ImageView(context);
            i.SetImageResource(thumbIds[position]);
            i.LayoutParameters </span>= <span style="color: #0000ff;">new</span> Gallery.LayoutParams(<span style="color: #800080;">500</span>, <span style="color: #800080;">500</span><span style="color: #000000;">);
            i.SetScaleType(ImageView.ScaleType.FitXy);
            </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> i;
        }

        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">long</span> GetItemId(<span style="color: #0000ff;">int</span><span style="color: #000000;"> position)
        {
            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
        }

        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> Java.Lang.Object GetItem(<span style="color: #0000ff;">int</span><span style="color: #000000;"> position)
        {
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">;
        }
    }
}</span></gallery>

Run.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn