4

画像の拡大縮小とトリミングを同時に行い、画面の左端から右端に表示しようとしています。ユーザーの画面よりも少し広い画像を受け取り、次のように拡大縮小できます(XML):

<ImageView
        android:id="@+id/category_image_top"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:maxHeight="170dp"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true" 
        android:focusable="false"
        />

しかし、これは私が得るものです: 私が得るもの

次のように、画像を右上に揃えたいと思います。 私が欲しいもの

これは可能ですか?私はすべてのscaleTypesを試しましたが、作品に注目してください。画像はXとYに合わせて拡大縮小されるか(fitXY、fitStart)、画像は切り取られますが中央に配置されます(centerCrop)。Android:scaleType="cropStart" のようなものが必要です

4

3 に答える 3

4
<ImageView
        android:id="@+id/category_image_top"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:maxHeight="170dp"
        android:scaleType="centerCrop"
        android:paddingLeft="half of your screen width"
        android:paddingBottom="half of your screen height"
        android:adjustViewBounds="true" 
        android:focusable="false"
/>

画像を左右に移動するパディングと、上下に移動する上下のパディングを設定できます

于 2013-12-28T07:01:21.227 に答える
1

xml (ビュー) を介してこの状況に対処する方法が見つからなかったので、(@serenskye が提案したように) コードを作成しました。これが私のコードです。お役に立てば幸いです(ps:ロジックを少し変更しました。画像を幅に合わせたかったので、事前定義されたimageWidghtにスケーリングしてからimageHeightにトリミングしました)

//bm is received image (type = Bitmap)
Bitmap scaledImage = null;
float scaleFactor = (float) bm.getWidth() / (float) imageWidth;

//if scale factor is 1 then there is no need to scale (it will stay the same)
if (scaleFactor != 1) {
    //calculate new height (with ration preserved) and scale image
    int scaleHeight = (int) (bm.getHeight() / scaleFactor);                     
    scaledImage = Bitmap.createScaledBitmap(bm, imageWidth, scaleHeight, false);
}
else {
    scaledImage = bm;
}

Bitmap cropedImage = null;
//if cropped height is bigger then image height then there is no need to crop
if (scaledImage.getHeight() > imageHeight)
    cropedImage = Bitmap.createBitmap(scaledImage, 0, 0, imageWidth, imageHeight);
else
    cropedImage = scaledImage;

iv.setImageBitmap(cropedImage);
于 2013-02-21T09:38:43.530 に答える