我将一些图像本地存储在设备上作为压缩位图。要检索它们,我从文件路径中获取它们,解码它们,然后将它们存储为BitmapDrawables。
我现在需要使用这些bitmapDrawable来填充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 android.graphics.drawable.BitmapDrawable@425df378
所以我现在需要以某种方式将它们传递给我的viewPager适配器类。据我所知,我需要将它们转换为可绘制对象的int数组,我的适配器类将使用它来填充viewPager。所以我需要看起来像这样的东西:
private int[] imageArray = { Resource.drawable.a, Resource.drawable.b, Resource.drawable.c}
任何人都可以帮助将这些转换为我需要的int数组,或者建议我如何使用我拥有的drawble对象来做到这一点,以便我有适合我的ViewPager适配器的东西吗?这是我第一次使用viewPager,它比我想象的更棘手。
答案 0 :(得分:0)
我通过将Bitmap数组最后传递给适配器来解决它。像这样:
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;
}
}