在android的横向模式下,将图像与viewpager中的屏幕宽度相匹配
我有一个在android的横向模式下,将图像与viewpager中的屏幕宽度相匹配,android,android-viewpager,android-imageview,Android,Android Viewpager,Android Imageview,我有一个VerticalViewPage,里面有一些图像。在横向模式下旋转设备时,ImageView无法缩放到屏幕的宽度。它适合的高度,如果不是图像。我使用了AspectRatioImageView。它适合宽度,但不会向下滚动。 多谢各位 活动\u main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.c
VerticalViewPage
,里面有一些图像。在横向模式下旋转设备时,ImageView
无法缩放到屏幕的宽度。它适合的高度,如果不是图像。我使用了AspectRatioImageView
。它适合宽度,但不会向下滚动。
多谢各位
活动\u main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<HackyViewPager
android:id="@+id/vvp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
>
</HackyViewPager>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff" >
<AspectRatioImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/m4_3" />
</LinearLayout>
这是我的ImagePageAdapter
的InstanceItem
,它扩展了PagerAdapter
:
@Override
public Object instantiateItem(ViewGroup container, int position)
{
Context context = MainActivity.this;
container.requestLayout();
AspectRatioImageView imageView = new AspectRatioImageView(context);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.m4_3));
mAttacher = new PhotoViewAttacher(imageView);
mAttacher.setOnMatrixChangeListener(new OnMatrixChangedListener() {
@Override
public void onMatrixChanged(RectF rect) {
if((int)rect.width()>_width)
viewPager.setLocked(true);
else
viewPager.setLocked(false);
}
});
注意:问题在于
viewPager
height。当我向下滚动图像时,它只会转到另一页,而不是滚动缩放后的图像。以下是您需要的内容。
这是我发现的一个很好的解决方案
这允许根据您的方向设置高度和宽度
要将图像比例更改为全屏,您通常会使用中心裁剪,如下所示:
yourImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
希望这些信息有用:)在活动代码中使用
android:scalType=“fitxy”
,例如imageView.setScaleType(imageView.ScaleType.FIT_XY)代码>请添加代码和布局。我对图像比例没有问题。我使用了AspectRatioImageView
,效果很好。问题在于ViewPager
无法将其高度缩放到容器。多亏了这一点,它解决了左右两侧不需要的图像边距问题。