使用ViewPager和Glide的Android相册

时间:2017-09-06 11:18:15

标签: android android-viewpager android-glide

我正在创建一个Android活动,其中包含一个带有ViewPager和TextView的ScrollView。 ViewPager的页面是片段,其中包含一个ImageView。我用Glide将图像加载到ImageView中。

我希望图像的宽度填满屏幕,并调整要调整大小的高度以保持图像的比例。问题是

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

它不会起作用。图像的高度始终为0.

我找到了一个次优的解决方案:https://stackoverflow.com/a/19505733/4420321。 问题是我需要知道图像的确切比例才能正确调整高度。

现在它正在使用800x600图像,但没有任何其他图像。有没有办法让它以任何比例工作?或者这个问题有更好的解决方案吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

这将使图像根据原始图像的宽高比填充屏幕宽度和高度。要使用此方法,您需要对宽高比进行硬编码。

Display display = getWindowManager().getDefaultDisplay();
ImageView imageView = (LinearLayout) findViewById(R.id.imageView);
int width = display.getWidth(); 
int height = (int) width * 0.75; // 0.75 if image aspect ration is 4:3, change accordingly 
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height);

我认为这可以解决您的问题。