Xamarin Android - Bitmap Drawables to Int array for ViewPager

喜欢而已 提交于 2019-12-12 02:18:29

问题


I am storing some images locally on the device as compressed bitmaps. To retrieve them i get them from their file path, decode them, and then store them as BitmapDrawables.

I now need to use these bitmapDrawables to populate a viewPager.

This is the code that effectively gets all my bitmaps into a bitmap array:

public void getMySeenExhibits()
    {
        string[] myImages;
        myImages = DBconnection.getAllImageURLs (conn); // sqLite connection - this gets all the file paths from SqLite

        List<Drawable> myBitmapList = new List<Drawable>();

        foreach (var imagePath in myImages) 
        {
            Drawable myBitmap = getImageFromLocalStorage (imagePath); // this converts them to BitmapDrawables
            Console.WriteLine ("My Bitmap is " + myBitmap.ToString ()); // just to check what we get back
            myBitmapList.Add (myBitmap); 

        }

        myBitmapList.ToArray (); 
    }

And the result of my console.writeline is:

My Bitmap is android.graphics.drawable.BitmapDrawable@425df378

So I now need to somehow pass them into my viewPager adapter class. As far as i'm aware i need to convert them to an int array of drawable objects which my adapter class will use to populate the viewPager. So i need something that looks like this:

private int[] imageArray = { Resource.drawable.a, Resource.drawable.b, Resource.drawable.c}

Can anybody help on ow to convert these into the int array i need, or suggest how i can do it using the drawble objects i have, so that i have something suitable for my ViewPager adapter please? It's the first time i've used viewPager and it's been trickier than i imagined.


回答1:


I solved it by passing in a Bitmap array to the adapter in the end. Like this:

public class ViewFragmentAdapter: FragmentPagerAdapter
    {
        Bitmap[] imageArray;


        public ViewFragmentAdapter (Android.Support.V4.App.FragmentManager fm, Bitmap[] imageArray) 
            : base (fm)
        {
            this.imageArray = imageArray;
        }

        public override int Count {
            get { return imageArray.Length; }

        }



        public override Android.Support.V4.App.Fragment GetItem (int position)
        {
            return new viewerFragment(imageArray[position]);
        }
    }


    public class viewerFragment: Android.Support.V4.App.Fragment
    {
        Bitmap bm;

        public viewerFragment(Bitmap bitmap)
        {
            bm = bitmap;
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate (Resource.Layout.myExhibitHistoryItem, container, false);

            var image = view.FindViewById<ImageView> (Resource.Id.exhibitImage);

            image.SetImageBitmap (bm);

            return view;
        }
    }


来源:https://stackoverflow.com/questions/26652482/xamarin-android-bitmap-drawables-to-int-array-for-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!