我正在尝试将照片(通过url下载)加载到imageview中,并使其跨越其父视图的整个宽度,该父视图是listview中的一行。
<ImageView
android:id="@+id/property_row_photo"
android:layout_width="match_parent"
android:layout_height="@dimen/search_list_item_height"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
直接在ImageView上设置scaleType似乎无济于事。我知道每一行的高度,因此我正在测量宽度,然后使用它来调用Picasso的Resize和CenterCrop方法,如下所示:
var height = Convert.ToInt32 (ApplicationContext.Activity.Resources.GetDimension (Resource.Dimension.search_list_item_height));
viewHolder.Photo.Measure (0, height);
var width = viewHolder.Photo.MeasuredWidth;
var url = property.ThumbnailPhotoUrl.Replace ("/thumbnails/", "/{0}/{1}/".WithFormat (width, height));
viewHolder.Photo.SetUrlBitmapWithCenterCrop (url, width, height,
loadingResourceId: Resource.Drawable.property_row_background,
errorResourceId: Resource.Drawable.image_not_available);
public static void SetUrlBitmapWithCenterCrop (this ImageView imageView, string url,
int width, int height, int? loadingResourceId = null, int? errorResourceId = null) {
Picasso.With (imageView.Context)
.Load (url)
.Placeholder (loadingResourceId ?? 0)
.Error (errorResourceId ?? 0)
.Resize (width, height)
.CenterCrop ()
.Tag (imageView.Context)
.Into (imageView);
}
不幸的是,未应用centerCrop scaleType,我在每张照片的左侧和右侧看到大的白色边框。知道我缺少什么吗?请注意,我也尝试过Fit方法,但是没有运气。
最佳答案
尝试同时使用fit和centerinside
Picasso.with(getContext()).load(path).fit().centerInside().into(iv);