简体   繁体   中英

Fit image to screen width in viewpager in landscape mode in android

I have a VerticalViewPager and I have some images in it. When I rotate my device in landscape mode my ImageView doesn't scale to width of my screen. It fits the height if image instead. I used AspectRatioImageView . It fits the width but VerticalViewPager doesn't scroll down. Thank you.

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>

Here is 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>

and here is my instaniateItem of my ImagePageAdapter that extends 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);
            }
        });

PS : The issue is with viewPager height. When I scroll down the image it just goes to the other page instead of scrolling the scaled image.

Here's what you need.

This is a good solution I found out.

Android Crop Center of Bitmap

This allows to set the height and your width according to your orientation.

To change the image's scale to full screen, you generally use center crop like this:

yourImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

Hope this information was useful..:)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM