使用Xamarin和MvvmCross,我正在编写一个Android应用程序,该应用程序将相册中的图像加载到带有自定义绑定的MvxGridView中:
<MvxGridView
android:id="@+id/grid_Photos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:numColumns="3"
android:verticalSpacing="4dp"
android:horizontalSpacing="4dp"
android:stretchMode="columnWidth"
android:fastScrollEnabled="true"
local:MvxBind="ItemsSource AllPhotos"
local:MvxItemTemplate="@layout/item_photo_thumbnail" />它使用item_photo_thumbnail.axml
<ImageView
local:MvxBind="PicturePath PhotoPath"
style="@style/ImageView_Thumbnail" />以下是绑定类:
public class PicturePathBinding : MvxTargetBinding
{
private readonly ImageView _imageView;
public PicturePathBinding(ImageView imageView)
: base(imageView)
{
_imageView = imageView;
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
public override Type TargetType
{
get { return typeof(string); }
}
public override void SetValue(object value)
{
if (value == null)
{
return;
}
string path = value as string;
if (!string.IsNullOrEmpty(path))
{
Java.IO.File imgFile = new Java.IO.File(path);
if (imgFile.Exists())
{
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.InJustDecodeBounds = true;
BitmapFactory.DecodeFile(imgFile.AbsolutePath, options);
// Calculate inSampleSize
options.InSampleSize = CalculateInSampleSize(options, 100, 100);
// Decode bitmap with inSampleSize set
options.InJustDecodeBounds = false;
Bitmap myBitmap = BitmapFactory.DecodeFile(imgFile.AbsolutePath, options);
_imageView.SetImageBitmap(myBitmap);
}
}
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
var target = Target as ImageView;
if (target != null)
{
target.Dispose();
target = null;
}
}
base.Dispose(isDisposing);
}
private int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
int height = options.OutHeight;
int width = options.OutWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
int halfHeight = height / 2;
int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight &&
(halfWidth / inSampleSize) > reqWidth)
{
inSampleSize *= 2;
}
}
return inSampleSize;
}
}我的问题是,它是非常缓慢和缓慢。我希望每个映像都能异步加载。我不知道该怎么做。在.net (XAML)中,他们的GridView控件可以自动完成一切(使用虚拟化),但是我意识到在Android中,它可能需要手动处理吗?
有人能帮我吗?
发布于 2015-04-26 07:26:32
我目前正在处理远程图像,而不是本地图像,因此我一直在使用提供MvxImageView类的下载缓存插件。这本身可能会给你带来一些好处。
到目前为止,我对Android的经验是,在大多数情况下,如果前景默认的话,一切都会运行。现在,在绑定类中的所有计算代码中,这几乎肯定会在前台运行。
我要做的就是让它跑得更快:
遵循这种模式实际上也可以帮助基于Windows的客户端。
https://stackoverflow.com/questions/29868578
复制相似问题