Android Xamarin中有没有类似于UIScrollVIew的东西?
问题描述:
我正在尝试创建带有图像的水平滚动视图(如照片查看器,但不是全屏)。在iOS中,您可以将子视图添加到UIScroll视图,有什么方法可以在android上执行相同的操作?如果有帮助,我使用Xamarin。 我试图从Xamarin https://components.xamarin.com/view/MultiImageView/ 使用multiimageview成分,但没有检索当前索引,我需要选择的图像数量Android Xamarin中有没有类似于UIScrollVIew的东西?
答
如果您是使用XML的布局,你可以使用下面的
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fillViewport="true"
android:scrollbars="horizontal|vertical" >
你也可以做的正是通过代码同样的事情(见参考文献https://developer.xamarin.com/api/type/Android.Widget.HorizontalScrollView/)。
最后,如果您使用Xamarin.Forms,您可以使用已在所有平台上实施的ScrollView,并且您可以设置“方向”。 (请参阅参考https://developer.xamarin.com/api/type/Xamarin.Forms.ScrollView/)
答
请确保您创建图像的缩略图版本,否则当您有多个图像时,您将开始遇到内存和性能问题。这是来自我自己的应用程序的代码,它代码BaseAdapter <>,并在列表中显示照片以及其他内容。我在列表适配器中进行大小调整,但没有理由不能先完成或缓存然后重新使用。
// get the a photo item based on list position
var item = items[position];
// just getting photo info from my list of photos
var info = item.Data.Photos[0];
var options = new BitmapFactory.Options() { InJustDecodeBounds = true };
BitmapFactory.DecodeFile(info.Filename, options);
// Raw height and width of image
var height = options.OutHeight;
var width = options.OutWidth;
int inSampleSize = 1;
var viewWidth = 100;
var viewHeight = 100;
if (height > viewHeight || width > viewWidth)
{
// Calculate ratios of height and width to requested height and width
var heightRatio = (int)Math.Round((float)height/(float)viewHeight);
var widthRatio = (int)Math.Round((float)width/(float)viewWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
options.InSampleSize = inSampleSize;
options.InJustDecodeBounds = false;
image.SetImageBitmap(BitmapFactory.DecodeFile(info.Filename, options));
在Android中当我试图添加到HorizontalScrollView坠毁多个视图它只是叫滚动型 – Nanoc