2016-10-22 68 views
1

我有一個ImageView具有固定的高度和寬度(比如寬度爲100dp,高度爲50dp)。我需要動態加載圖像到這個圖像視圖。如何適應圖像的寬度,並保持在android的寬高比

我希望此圖像以滿足ImageView寬度的方式縮放,但要保持高寬比(我不關心圖像的一部分因高度限制而不可見)。

我想要的與centerCrop非常相似,但我不希望我的圖像垂直居中!

在此先感謝。

EDIT1:

這裏是我的代碼:

這是我的佈局。我想爲imageView設置圖像與background ID。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

<ImageView 
    android:id="@+id/background" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/footer" 
    android:layout_alignBottom="@id/footer" 
    /> 

<RelativeLayout 
    android:id="@+id/footer" 
    android:layout_width="match_parent" 
    android:layout_height="100dp"> 

</RelativeLayout> 

注意的ImageView綁定到第二RelativeLayout的大小:

android:layout_alignTop="@+id/footer" 
    android:layout_alignBottom="@id/footer" 

回答

1

首先在佈局中添加

android:scaleType="matrix" 

您的ImageView。在您創建具有所需寬度的位圖對象的Java代碼(您ImageView的寬度)

和:

然後在Java代碼中添加此:

ackground.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 

      Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.background_image); 

      float imageRatio = (float) backgroundBitmap.getWidth()/(float) backgroundBitmap.getHeight(); 

      int imageViewWidth = background.getWidth(); 
      int imageRealHeight = (int) (imageViewWidth/imageRatio); 

      Bitmap imageToShow = Bitmap.createScaledBitmap(backgroundBitmap, imageViewWidth, imageRealHeight, true); 
      background.setImageBitmap(imageToShow); 

     } 
    }); 

我將其分解爲你將其設置爲您的imageView。但你需要使你的imageView scaleType爲矩陣,以防止android自動縮放它!

+0

是否可以在不將drawable轉換爲位圖的情況下做到這一點?這對我的使用來說確實很慢。 –

+2

你有什麼想法,我一直在尋找這個! +1 – instanceof

0

讓我們假設你有Bitmap圖像,即imageBitmapImageView,即myImageView 。這裏是你如何調整其大小,使其充滿你的ImageView的整個寬度保持寬高比:

float imageOriginalWidthHeightRatio = (float) imageBitmap.getWidth()/(float) imageBitmap.getHeight(); 

int imageToShowWidth = myImageView.getWidth() 
int imageToShowHeight = (int) (imageToShowWidth/imageOriginalWidthHeightRatio); 

Bitmap imageToShow = Bitmap.createScaledBitmap(imageBitmap, imageToShowWidth, imageToShowHeight, true); 
myImageView.setImageBitmap(imageToShow); 
+0

感謝您的回答。它與我自己的一個小調整:) –

0

要創建寬度等於屏幕寬度的圖像,並根據寬高比按比例設置高度,請執行以下操作。在這裏我提到如何將圖像從url加載到imageview。

Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() { 
        @Override 
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { 

         // creating the image that maintain aspect ratio with width of image is set to screenwidth. 
         int width = imageView.getMeasuredWidth(); 
         int diw = resource.getWidth(); 
         if (diw > 0) { 
          int height = 0; 
          height = width * resource.getHeight()/diw; 
          resource = Bitmap.createScaledBitmap(resource, width, height, false); 
         } 
               imageView.setImageBitmap(resource); 
        } 
       }); 

希望這會有所幫助。 :)

相關問題