Heim > php教程 > php手册 > 【Android】第5章(8)图片库(Galery)

【Android】第5章(8)图片库(Galery)

WBOY
Freigeben: 2016-07-06 13:30:52
Original
1287 Leute haben es durchsucht

分类:C#、Android、VS2015; 创建日期:2016-02-07 一、简介 图库(也叫画廊)是一个布局小部件,用于在可水平滚动的列表中显示每一副图片,当前所选的图片将置于视图的中心。 注意:Android已经弃用了这个小部件,弃用的原因是用Galery实现的效率比较低,

分类:C#、Android、VS2015;

创建日期:2016-02-07

一、简介

图库(也叫画廊)是一个布局小部件,用于在可水平滚动的列表中显示每一副图片,当前所选的图片将置于视图的中心。

注意:Android已经弃用了这个小部件,弃用的原因是用Galery实现的效率比较低,官方的建议是改为用HorizontalScrollView来替代这个小部件。但是,目前手机上的图片浏览功能很多都是用Galery来实现的,如果你仍然喜欢这个小部件,也可以在高版本的项目中继续使用它。

二、示例8--Demo08Gallery

1、运行截图

在模拟器中用鼠标左右拖放图片观察效果。

image

2、添加Demo08Gallery.axml文件

<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>
Nach dem Login kopieren

保存文件,然后单击解决方案资源管理器上方的【刷新】按钮。

3、添加Demo08Gallery.cs文件

<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>
Nach dem Login kopieren

运行。


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage