私はCMSから画像を取り込んでいて、バナースタイル(幅= 900、高さ= 250)です。私は全画面ポートレートモードでそれらを使用しなければならないので、当然彼らはひどく歪んでいる。私は、xml、ビットマップのサイズ変更、ピカソ変換など、さまざまなことをたくさん試しました。しかし、この時点で何を試してみるべきか分かりません。私が品質を維持することができた唯一の時間はセンタークロッピングによるものですが、それは選択肢ではありません。アスペクト比を維持するには画像が必要です。アスペクト比を犠牲にすることなくこれを行うことも可能ですか?画像のアスペクト比は1.7、私の銀河s5では0.5625です。詳細はAndroidビットマップスケールバナー画像を肖像画にする
編集:
これは、独自のプロジェクトですが、私は私が使用している画像を含めることはできませんが、彼らは、バナーのスタイルです。それらはscreenSizeHeight - toolbarHeight
とscreensizewidth/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());
は、より多くの情報で編集しました。イメージを横向きに回転させないか、あなたが示唆していることを誤解していませんか? – user3050491