2016-10-22 19 views
1

私はImageViewの高さと幅を固定しています(幅100dp、高さ50dp)。この画像ビューに画像を動的にロードする必要があります。イメージを幅に合わせてアンドロイドでアスペクト比を維持する方法

この画像をImageViewの幅に塗りつぶしても、縦横比を維持するように拡大します(高さの限界のために画像の一部が見えない場合は気にしません)。

私が欲しいものはcenterCropと本当に似ていますが、私は自分のイメージを垂直にセンタリングしたくありません!

ありがとうございます。

EDIT1:

は、ここに私のコードです:

これは私のレイアウトです。私はbackground idでimageViewの画像を設定したいと思います。

<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

あなたImageViewのに

android:scaleType="matrix" 

を追加します。あなたが希望の幅(あなたImageViewの幅)とビットマップオブジェクトを作成するJavaコードを

と: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を作る必要があります!

+0

drawableをビットマップに変換せずにこの正確なことを行うことは可能でしょうか?それは私の使用のために本当に遅いです。 –

+2

私はこれを正確に探していたのですか? +1 – instanceof

0

あなたはBitmapイメージ、すなわちimageBitmap、およびImageView、すなわち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

私はあなただけにImageViewの高さを設定することができると思いますが、例えば50dpを入力し、scaleTypeをhttps://developer.android.com/reference/android/widget/ImageView.ScaleType.htmlに記載されたいずれかに変更してください。私は何が何をするのか覚えていない。あなたのレイアウトの最初の

+0

それは動作しません!私はこれと数日間戦ってきました。私が見つけた最も近いscaleTypeはcenterCropでしたが、私は中心の機能を望んでいません! –

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); 
        } 
       }); 

これが役に立ちます。 :)

関連する問題