• 第四课 Gallery的使用


    直接上代码

    1.Layout——Main.axml

    <?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:minWidth="25px"
        android:minHeight="25px">
        <TextView
            android:text="@string/Welcome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView1" />
        <Gallery
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gallery1" />
    </LinearLayout>

    2.Activity1.cs

    using System;
     
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Android.Database;
    using Android.Provider;
     
    namespace GalleryViewDemo
    {
        [Activity(Label = "Gallery View Sample", MainLauncher = true, Icon = "@drawable/icon")]
        public class GalleryViewSample : Activity
        {
     
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
     
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
                CreateGallery();
            
            }
     
     
            private void CreateGallery()
            {
                Gallery g = this.FindViewById<Gallery>(Resource.Id.gallery1);
                g.Adapter = new ImageAdapter(this);
            }
        }
    }
     

    3.ImageAdapter.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.Database;
    using Android.Provider;
    
    namespace GalleryViewDemo
    {
        public class ImageAdapter : BaseAdapter
        {
            Context _Context;
            ICursor _ImageCursor;
            protected ICursor ImageCursor
            {
                get
                {
                    if (_ImageCursor == null)
                    {
                        _ImageCursor = GetImageCursor();
                    }
                    return _ImageCursor;
                }
                set
                {
                    _ImageCursor = value;
                }
            }
            public ImageAdapter(Context c)
            {
                _Context = c;
            }
            private ICursor GetImageCursor()
            {
                string[] Projection = { MediaStore.Images.Thumbnails.ImageId };
                var ImageCursor = ((Activity)_Context).ManagedQuery(MediaStore.Images.Thumbnails.ExternalContentUri,
                    Projection, null, null, null);
                return ImageCursor;
            }
            public override int Count
            {
                get { return ImageCursor.Count; }
            }
    
            public override Java.Lang.Object GetItem(int position)
            {
                return position;
            }
    
            public override long GetItemId(int position)
            {
                ImageCursor.MoveToPosition(position);
                var ImageId = ImageCursor.GetString(0);
                return position;
            }
    
            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                if (convertView == null)
                {
                    ImageView ReturnView = new ImageView(_Context);
                    ImageCursor.MoveToPosition(position);
    
                    var ImageId = ImageCursor.GetString(0);
                    ReturnView.SetImageURI(Android.Net.Uri.WithAppendedPath(MediaStore.Images.Thumbnails.ExternalContentUri, ImageId));
                    ReturnView.SetScaleType(ImageView.ScaleType.CenterCrop);
                    return ReturnView;
                }
                else
                {
                    return(ImageView) convertView;
                }
            }
        }
    }

    在模拟器中,先下载了两幅图片,运行成功。

    在手机上运行失败,尚不知道原因,求解。

  • 相关阅读:
    hdu2083
    斐波那数
    hdu2000~hdu2099
    hdu2070
    hdu2071
    hdu2095
    TSINGSEE青犀视频云边端架构视频直播点播平台/人脸识别系统EasyDSS 如何开启debug 日志?
    RTMP协议视频直播点播智能分析平台EasyDSS如何增加Stream模块实现TCP代理?
    RTMP推流平台/视频直播点播分析平台/人脸识别系统EasyDSS如何实现RTMP负载均衡?
    关于视频智能分析平台人脸识别/车牌识别系统EasyDSS登录及直播点播的安全防盗链验证说明
  • 原文地址:https://www.cnblogs.com/catzhou/p/3591486.html
Copyright © 2020-2023  润新知