如何在Android中将图像调整为imageview高度?

时间:2017-04-06 08:32:10

标签: java android image

我想做一些图像查看器(sorta)应用程序,我正在使用viewpager在图像之间导航。图像的尺寸均为1260x2038,位于res / drawables目录中。我想做什么:

  1. 在纵向模式下,图像将适合设备高度
  2. 在横向模式下,图像将适合设备宽度并可垂直滚动。
  3. 因为我知道大图像会导致内存不足异常,所以我将其设置为如果设备高度大于或等于1920px,我想将最大高度成像为1920px。

    我尝试将imageview放在一个滚动视图中,以使这些工作起作用,并且它们的工作方式不像我预期的那样。

    这就是我的代码目前的相关部分,

    fragment_page.xml

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ScrollView
            android:id="@+id/scroll"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
    
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />
    
        </ScrollView>
    </LinearLayout>
    

    FragmentPage.java

    public class PageFragment extends Fragment
    {
        private ScrollView scroll;
        private ImageView image;
        private int imageId;
        private int deviceWidth,deviceHeight;
        private static double ratio = 0.61825;
        private static double ratio2 = 1.61746;
    
        private static LinearLayout.LayoutParams scrollPortraitParams   = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        private static LinearLayout.LayoutParams scrollLandscapeParams  = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        private static ScrollView.LayoutParams imagePortraitParams      = new ScrollView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        private static ScrollView.LayoutParams imageLandscapeParams     = new ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            imageId = getArguments().getInt("IMAGE_ID");
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
        {
            return inflater.inflate(R.layout.fragment_page, container, false);
        }
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
        {
            super.onViewCreated(view, savedInstanceState);
            scroll  = (ScrollView) view.findViewById(R.id.scroll);
            image   = (ImageView) view.findViewById(R.id.image);
    
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            deviceWidth  = size.x;
            deviceHeight = size.y;
    
            loadImages();
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) 
        {
            super.onConfigurationChanged(newConfig);
    
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            deviceWidth  = size.x;
            deviceHeight = size.y;
    
            if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
            {
                MainActivity.isInPortraitMode = true;
                loadImages();
            }
            else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                MainActivity.isInPortraitMode = false;
                loadImages();
            }
        }
    
        private void loadImages()
        {
            if(MainActivity.isInPortraitMode)
            {
                scroll.setLayoutParams(scrollPortraitParams);
                image.setLayoutParams(imagePortraitParams);
    
                if(deviceHeight >= 1920)
                {
                    Glide.with(getActivity())
                         .load(imageId)
                         .override(1187, 1920)
                         .fitCenter()
                         .into(image);
                }
                else
                {
                    Glide.with(getActivity())
                         .load(imageId)
                         .override(deviceWidth, deviceHeight)
                         .fitCenter()
                         .into(image);
                }
            }
            else
            {
                if(deviceWidth >= 1920)
                {
                    scroll.setLayoutParams(scrollLandscapeParams);
                    image.setLayoutParams(imageLandscapeParams);
                    Glide.with(getActivity())
                         .load(imageId)
                         .override(1187, 1920)
                         .fitCenter()
                         .into(image);
                }
                else
                {
                    scroll.setLayoutParams(scrollLandscapeParams);
                    image.setLayoutParams(imageLandscapeParams);
                    Glide.with(getActivity())
                         .load(imageId)
                         .override(deviceWidth, (int)(deviceWidth * ratio2))
                         .fitCenter()
                         .into(image);
                }
            }
        }
    }
    

    非常感谢任何帮助。感谢。

0 个答案:

没有答案