在ViewPager中的imageView中拉伸图像

时间:2016-07-16 22:00:03

标签: android android-viewpager imageview xamarin.android

我正在开发一个Android应用程序,它在PageViewer中的ImageView中显示动物的图片。在大多数手机上,一切似乎都很好。但是,在我的S7上(显示分辨率高于图片的分辨率),我无法拉伸图片来扩展设备宽度。

imageView还附有一个Xamarin photoView附件,但我认为它没有任何效果。我试图删除它,它仍然是相同的。

我已经尝试过android:adjustViewBounds和所有android:scaleType,不幸的是它没有帮助。

这是我的xml代码: 部分活动xml:

<fieldguideapp.droid.MyViewPager
    android:id="@+id/pagerAnimalProfile"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="wrap_content" />

PagerLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:id="@+id/imgPagerLayoutImageView" />
    <RelativeLayout
        android:id="@+id/relLayoutImgPagerImageDesc"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:background="#FFFFFFFF"
        android:layout_gravity="bottom">
        <TextView
            android:text="Image desc"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FFFFFFFF"
            android:textColor="#FF000000"
            android:id="@+id/txtImgPagerImageDescription"
            android:layout_alignParentLeft="true" />
        <LinearLayout
            android:id="@+id/linLayoutImgPagerIndicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_alignBottom="@id/txtImgPagerImageDescription" />
        <TextView
            android:text="Image credit"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FFFFFFFF"
            android:textColor="#FF000000"
            android:id="@+id/txtImgPagerImageCredit"
            android:layout_alignParentRight="true"
            android:layout_alignBottom="@id/linLayoutImgPagerIndicator" />
    </RelativeLayout>
</LinearLayout>

改变寻呼机高度的部分代码:

// To specify the height of the pager, select the highest child
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    int height = 0;
    for (int i = 0; i < ChildCount; i++)
    {
        View child = GetChildAt(i);
        child.Measure(widthMeasureSpec, MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified));
        int h = child.MeasuredHeight;
        if (h > height) height = h;
    }
    if (Resources.Configuration.Orientation == Android.Content.Res.Orientation.Portrait)
    {
        heightMeasureSpec = MeasureSpec.MakeMeasureSpec(height, MeasureSpecMode.Exactly);
    }
    base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
编辑:这是我得到的: Portrait Landscape

我希望得到任何帮助或建议。感谢

1 个答案:

答案 0 :(得分:0)

我最终使用Bitmap.CreateScaledBitmap()方法手动调整图片大小以适合屏幕尺寸,然后使用ImageView.SetImageDrawable()进行设置,它完美无缺。我需要在之前和之后调用GC.Collect()以避免OutOfMemoryException。

虽然这不太理想,但确实有效。

相关问题