【问题标题】:Fit image to screen width in viewpager in landscape mode in android在android的横向模式下使图像适合viewpager中的屏幕宽度
【发布时间】:2014-04-28 07:43:50
【问题描述】:

我有一个VerticalViewPager,里面有一些图片。当我在横向模式下旋转我的设备时,我的ImageView 不会缩放到我的屏幕宽度。它适合图像的高度。我用AspectRatioImageView。它适合宽度,但VerticalViewPager 不会向下滚动。 谢谢。

activity_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>

这里是rowitemview.xml

<?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>

这是我的instaniateItemImagePageAdapter 扩展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);
            }
        });

P.S:问题在于viewPager 高度。当我向下滚动图像时,它只是转到另一页而不是滚动缩放的图像。

【问题讨论】:

  • 请添加您的代码和布局。

标签: android android-viewpager android-imageview


【解决方案1】:

这就是你需要的。

这是我发现的一个很好的解决方案。

Android Crop Center of Bitmap

这允许根据您的方向设置高度和宽度。

要将图像的比例更改为全屏,您通常可以像这样使用中心裁剪:

yourImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

希望这些信息有用..:)

【讨论】:

  • 我对图像比例没有问题。我使用AspectRatioImageView,它工作正常。问题是ViewPager 没有将其高度缩放到容器。
  • 谢谢,这解决了左右两边不需要的图像边距问题。
【解决方案2】:

在活动代码中使用android:scalType = "fitxy"imageView.setScaleType(ImageView.ScaleType.FIT_XY);

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多