2014-10-30 44 views
0

我在設備上本地存儲一些圖像作爲壓縮位圖。爲了檢索它們,我從它們的文件路徑中獲取它們,對它們進行解碼,然後將它們存儲爲BitmapDrawables。Xamarin Android - 位圖Drawables到Int數組ViewPager

我現在需要使用這些bitmapDrawables來填充viewPager。

這是一個有效地得到我所有的位圖轉換爲位圖數組的代碼:

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(); 
    } 

而且我個Console.WriteLine結果:

My Bitmap is [email protected] 

所以我現在需要以某種方式傳遞他們到我的viewPager適配器類。據我所知,我需要將它們轉換爲一個可繪製對象的int數組,我的適配器類將用它來填充viewPager。所以,我需要的東西,看起來像這樣:

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

任何人可以幫助對流動來將這些轉換成int數組我需要,還是建議我怎麼可以使用drawble對象,我有做到這一點,讓我有一些適合我的ViewPager適配器嗎?這是我第一次使用viewPager,比我想象的更復雜。

回答

0

我解決了它通過傳遞一個位圖數組到適配器到底。像這樣:

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; 
     } 
    }