2016-07-08 3 views
1

私はCMSから画像を取り込んでいて、バナースタイル(幅= 900、高さ= 250)です。私は全画面ポートレートモードでそれらを使用しなければならないので、当然彼らはひどく歪んでいる。私は、xml、ビットマップのサイズ変更、ピカソ変換など、さまざまなことをたくさん試しました。しかし、この時点で何を試してみるべきか分かりません。私が品質を維持することができた唯一の時間はセンタークロッピングによるものですが、それは選択肢ではありません。アスペクト比を維持するには画像が必要です。アスペクト比を犠牲にすることなくこれを行うことも可能ですか?画像のアスペクト比は1.7、私の銀河s5では0.5625です。詳細はAndroidビットマップスケールバナー画像を肖像画にする

編集:

これは、独自のプロジェクトですが、私は私が使用している画像を含めることはできませんが、彼らは、バナーのスタイルです。それらはscreenSizeHeight - toolbarHeightscreensizewidth/1.2の画像ビューで使用されています。

これは私がビットマップのサイズを変更しようとしているところです。それはちょうどcentercroppingだとはるかに大きいです。

public static final Bitmap scaleAndCropBitmapByPixelDimensions(final Context context, final Bitmap sourceBitmap, final int targetWidth, final int targetHeight) 
{ 
    try 
    { 
     if (context != null && sourceBitmap != null) 
     { 
      int srcWidth = sourceBitmap.getWidth(); 
      int srcHeight = sourceBitmap.getHeight(); 

      float targetAspectRatio = (float)targetWidth/(float)targetHeight; 
      float srcAspectRatio = (float)srcWidth/(float)srcHeight; 

      int scaleWidth = targetWidth; 
      int scaleHeight = targetHeight; 
      int x = 0; 
      int y = 0; 


      if (srcAspectRatio < targetAspectRatio) 
      { 
       scaleWidth = targetWidth; 
       scaleHeight = (int)((float)(1/srcAspectRatio) * (float)scaleWidth); 
       x = 0; 
       y = (scaleHeight - targetHeight)/2; 
      } 
      else 
      { 
       scaleHeight = targetHeight; 
       scaleWidth = (int)((float)srcAspectRatio * (float)scaleHeight); 
       x = (scaleWidth - targetWidth)/2; 
       y = 0; 
      } 


      Bitmap scaledBitmap = Bitmap.createScaledBitmap(sourceBitmap, scaleWidth, scaleHeight, true); 
      Bitmap croppedBitmap = Bitmap.createBitmap(scaledBitmap, x, y, targetWidth, targetHeight); 
      return croppedBitmap; 
     } 
    } 
    catch (Exception ex) 
    { 
     //Log.e(LOG_TAG, "Error scaling and cropping bitmap", ex); 
     ex.printStackTrace(); 
    } 

    return null; 
} 

ビットマップを設定する

@Override 
      public Bitmap transform(Bitmap sourceBitmap) { 
       i_bitmap = Utils.scaleAndCropBitmapByPixelDimensions(mContext,sourceBitmap,targetWidth,targetHeight); 


       if (sourceBitmap != i_bitmap) { 
        // Same bitmap is returned if sizes are the same 
        sourceBitmap.recycle(); 
       } 

       return i_bitmap; 


      } 

ImageViewのXMLの

<ImageView 
    android:id="@+id/bg" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:scaleType="center"  /> 

所望のアスペクト比が反対であるので、まあピカソ Picasso.with(mContext) .load(url) .transform(transformation) .into(getBg());

答えて

0

でイメージをロード変換実際の比率、1つのもののバナー画像(-90°または+ 90°)を回転させるだけです。

もちろん、これは非常に素朴な提案です。おそらく、表示されるコンテンツやその他の制約についてもっと詳しく説明すると、より良いソリューションを提供できるようになるでしょう。

+0

は、より多くの情報で編集しました。イメージを横向きに回転させないか、あなたが示唆していることを誤解していませんか? – user3050491

関連する問題