Android通过图像大小动态更改imageview大小

问题描述:

嗨我有一个ImageView,它显示来自url的图像。图像大小不同。它们可以是正方形或纵向形状或横向形状。我需要相应地设置图像视图大小,而不用拉伸或裁剪。Android通过图像大小动态更改imageview大小

说如果图像大小是512x512那么ImageView应该是正方形并且与屏幕中心对齐。

如果图像大小类似于1024x800,则ImageView大小应为水平矩形形状并与屏幕中心对齐。

在所有情况下,宽度应该与父项匹配,但高度应相应调整。我怎样才能做到这一点?

下面是我的xml代码。的ImageView的

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <android.support.v7.widget.CardView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:maxHeight="400dp" 
     android:maxWidth="300dp" 
     android:layout_margin="10dp" 
     app:cardBackgroundColor="@color/white"> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <android.support.v4.widget.NestedScrollView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:scrollbars="none"> 
       <ImageView 
        android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="true" 
        android:scaleType="fitXY" 
        android:minWidth="260dp" 
        tools:ignore="ContentDescription" /> 
      </android.support.v4.widget.NestedScrollView> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@color/grey_light" /> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="match_parent" 
       android:layout_height="35dp" 
       android:layout_gravity="right" 
       android:layout_margin="@dimen/space_small_low" 
       android:background="@drawable/transparent_bg" 
       android:textColor="@color/orange" /> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 
</FrameLayout> 
+0

移除机器人:从XML &scaleType = “fitXY” 由纵横比计算图像高度等(deviceheight * imagewidth)/ devicewidth – user3040153

+0

卸下它会在ImageView的空白空间。 –

设定宽度在XML match_parent并在运行时计算图像的高度使用以下代码来保持纵横比:

Display display = getActivity().getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int newWidth = size.x; 

//Get actual width and height of image 
int width = bitmap.getWidth(); 
int height = bitmap.getHeight(); 

// Calculate the ratio between height and width of Original Image 
float ratio = (float) height/(float) width; 
float scale = getApplicationContext().getResources().getDisplayMetrics().density; 
int newHeight = (int) (width * ratio)/scale; 

通过这个newHeight和newWidth到通过调用图像处理库的.resize函数调整图像大小。

+0

谢谢@nnn。这工作。 –

变化android:scaleType="fitXY"android:scaleType="centerInside"

并设置根据屏幕宽度ImageView宽度,意味着你应该通过屏幕宽度或类似的东西的60百分比计算宽度为ImageView并使用android:scaleType="centerInside"可以计算高度本身,而不改变图像的宽高比。

Jsut把wrap_content它会自动采取一个大小。 机器人:layout_height = “wrap_cont`enter代码here`ent” />

+0

请阅读完整的问题。他希望图像的宽度始终是match_parent。 – nnn

Picasso听到文库的?它不只是中心裁剪图像,它还为您管理缓存的复杂性。

如何设置?在您的Gradle和同步中添加此依赖项。

compile 'com.squareup.picasso:picasso:2.5.2' 

如何使用库?很简单,你只需要调用一个单例代码即可。

ImageView imageView = .... 
Picasso.with(contextHere()) 
     .load("http://example.com/myimage.jpg) 
     .fit() 
     .centerCrop() 
     .into(imageView);