1

これは私のレイアウトファイルです

<LinearLayout ...
<ImageView
    android:id="@+id/feed_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:adjustViewBounds="true"
    android:contentDescription="@string/image_content_description" />

ただし、幅は親の幅と一致ImageViewしません。(幅は画像ソースの幅)

画像ソースが URL から遅延読み込みで読み込まれました。

画像ソースに関係なく画像ビューの幅を拡大縮小する方法は?

私が欲しい

幅 = 一致 (または塗りつぶし) の親。

高さ = 自動スケーリング

4

5 に答える 5

0
<ImageView
    android:id="@+id/idImage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"/>

必要なアスペクト比に従って高さを計算します

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    int height = (display.getWidth() * 9) /16; // in this case aspect ratio 16:9

    ImageView image = (ImageView) findViewById(R.id.idImage);
    image.getLayoutParams().height = height;
于 2015-12-04T21:58:17.670 に答える
0

これを実現するには、imageView を拡張する新しい AspectRatioImageView を作成します。

public class AspectRatioImageView extends ImageView {

    public AspectRatioImageView(Context context) {
        super(context);
    }

    public AspectRatioImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable drw = getDrawable();
        if (null == drw || drw.getIntrinsicWidth() <= 0) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        } else {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = width * drw.getIntrinsicHeight() / drw.getIntrinsicWidth();
            setMeasuredDimension(width, height);
        }
    }
}

そして、レイアウト xml で次を使用します。

<my.app.AspectRatioImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ar_imageview"/>
于 2014-03-17T08:37:13.600 に答える
0

画像をビューに合わせたい場合は、android:scaleType="fitXY" を使用します

于 2013-07-19T06:04:47.053 に答える