Xamarin的Android - 位图资源,以int数组的ViewPager [英] Xamarin Android - Bitmap Drawables to Int array for ViewPager
问题描述
我该设备作为COM pressed位图上本地存储一些照片。为了获取他们,我让他们从他们的文件路径,德code它们,然后将它们存储为BitmapDrawables。
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.
我现在需要使用这些bitmapDrawables来填充viewPager。
I now need to use these bitmapDrawables to populate a viewPager.
这是code,有效地得到我所有的位图转换为位图数组:
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 ();
}
和我Console.WriteLine的可结果是:
And the result of my console.writeline is:
My Bitmap is android.graphics.drawable.BitmapDrawable@425df378
所以,我现在需要以某种方式将它们传递到我viewPager适配器类。至于我知道我需要将它们转换到我的适配器类将用于填充viewPager绘制对象的int数组。所以我需要的东西,看起来像这样:
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}
任何人可以帮助的流量将这些转换成int数组我需要,还是建议我怎么可以使用drawble对象我必须这样做,让我有什么适合我的ViewPager适配器吗?这是我第一次用viewPager,它已经比我想象的麻烦。
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.
推荐答案
我在一个位图数组传递到适配器最终解决它。像这样的:
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;
}
}
这篇关于Xamarin的Android - 位图资源,以int数组的ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!