예제 #1
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            SetContentView(Resource.Layout.ScrollImages);

            var position = Intent.GetIntExtra(POSITION, 0);

            viewPager = FindViewById <ViewPager>(Resource.Id.pager);
            viewPager.SetClipToPadding(false);
            //viewPager.PageMargin = DimensionHelper.DpToPx(12);


            //More documentation is available on this page
            //https://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners/Part-26-Retrieving-a-Photo-from-Flickrs-API
            string[] licenses = { "4", "5", "6", "7" };
            string   license  = String.Join(",", licenses);

            license = license.Replace(",", "%2C");

            string url = "https://api.flickr.com/services/rest/" +
                         "?method=flickr.photos.search" +
                         "&api_key={0}" +
                         "&user_id={1}" +
                         "&format=json" +
                         "&page={2}" +
                         "&per_page={3}" +
                         "&nojsoncallback=1";

            var baseUrl = string.Format(url,
                                        ImageConfig.flickrApiKey,
                                        ImageConfig.userId,
                                        ImageConfig.page,
                                        ImageConfig.per_page);


            var client = new System.Net.Http.HttpClient();

            client.GetStringAsync(baseUrl).ContinueWith((requestString) => {
                var flickrResult   = requestString.Result;
                FlickrData apiData = JsonConvert.DeserializeObject <FlickrData>(flickrResult);
                int counter        = 0;
                if (apiData.stat == "ok")
                {
                    foreach (Photo data in apiData.photos.photo)
                    {
                        // To retrieve one photo, use this format:
                        //http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}{size}.jpg
                        counter               = counter + 1;
                        string photoUrl       = "https://farm{0}.staticflickr.com/{1}/{2}_{3}_{4}.jpg";
                        string largeFlickrUrl = string.Format(photoUrl, data.farm, data.server, data.id, data.secret, "b");
                        string name           = string.Format("Image{0}", counter);
                        items.Add(new ScrollImages()
                        {
                            Images = largeFlickrUrl, Activity = this
                        });
                    }
                }

                viewPager.Adapter = new SwipeGalleryStateAdapter(SupportFragmentManager, items);
                viewPager.SetCurrentItem(position, false);
            });
        }
예제 #2
0
        public void GetImages()
        {
            //More documentation is available on this page
            //https://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners/Part-26-Retrieving-a-Photo-from-Flickrs-API
            string[] licenses = { "4", "5", "6", "7" };
            string   license  = String.Join(",", licenses);

            license = license.Replace(",", "%2C");

            string url = "https://api.flickr.com/services/rest/" +
                         "?method=flickr.photos.search" +
                         "&api_key={0}" +
                         "&user_id={1}" +
                         "&format=json" +
                         "&page={2}" +
                         "&per_page={3}" +
                         "&nojsoncallback=1";

            var baseUrl = string.Format(url,
                                        ImageConfig.flickrApiKey,
                                        ImageConfig.userId,
                                        ImageConfig.page,
                                        ImageConfig.per_page);

            HttpClient client = new HttpClient();

            client.GetStringAsync(baseUrl).ContinueWith((requestString) => {
                var flickrResult   = requestString.Result;
                FlickrData apiData = JsonConvert.DeserializeObject <FlickrData>(flickrResult);
                int counter        = 0;
                if (apiData.stat == "ok")
                {
                    foreach (Photo data in apiData.photos.photo)
                    {
                        // To retrieve one photo, use this format:
                        //http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}{size}.jpg
                        counter         = counter + 1;
                        string photoUrl = "https://farm{0}.staticflickr.com/{1}/{2}_{3}_{4}.jpg";

                        string baseFlickrUrl      = string.Format(photoUrl, data.farm, data.server, data.id, data.secret, "n");
                        string squareFlickrUrl    = string.Format(photoUrl, data.farm, data.server, data.id, data.secret, "s");
                        string thumbnailFlickrUrl = string.Format(photoUrl, data.farm, data.server, data.id, data.secret, "t");
                        string largeFlickrUrl     = string.Format(photoUrl, data.farm, data.server, data.id, data.secret, "b");
                        string name = string.Format("Image{0}", counter);

                        //string imgUrl, int drawableId, string squareUrl, string mediumUrl, string largeUrl, string thumbnailUrl)

                        items.Add(new GridItem()
                        {
                            Name         = name,
                            DrawableId   = Resource.Drawable.grid1,
                            ImgUrl       = baseFlickrUrl,
                            SquareUrl    = squareFlickrUrl,
                            LargeUrl     = largeFlickrUrl,
                            ThumbnailUrl = thumbnailFlickrUrl,
                        });
                    }
                }
                else                          //If failed to retrieve drawables from flicker, add some images from resource.
                {
                    items.Add(new GridItem()
                    {
                        Name = "Image1", DrawableId = Resource.Drawable.grid1
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image2", DrawableId = Resource.Drawable.grid2
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image3", DrawableId = Resource.Drawable.grid1
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image4", DrawableId = Resource.Drawable.grid2
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image5", DrawableId = Resource.Drawable.grid1
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image6", DrawableId = Resource.Drawable.grid2
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image7", DrawableId = Resource.Drawable.grid1
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image8", DrawableId = Resource.Drawable.grid2
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image9", DrawableId = Resource.Drawable.grid1
                    });
                    items.Add(new GridItem()
                    {
                        Name = "Image10", DrawableId = Resource.Drawable.grid2
                    });
                }

                this.RunOnUiThread(() => {
                    gridview.Adapter = new GridImageAdapter(this, items);
                });
            });
            //} //Close http client
        }