1

私はさまざまなドローアブル (大きいまたは小さい) を持っている可能性があり、常に幅 (match_parent) にまたがり、高さを比例して増減します。比率を維持しています。

これはどのように可能ですか?

私は試しました:

<ImageView
            android:id="@+id/iv_TEST"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/test" />

問題は、幅を増やしたり減らしたりして、見栄えが悪いことです。

ここに画像の説明を入力

4

1 に答える 1

11

修理済み。この問題を修正するには、次のことを行う必要があります。

  1. カスタム イメージ ビュー
  2. "android:src"コードからドローアブルを に設定( imageview.setImageResource())

ImageView を ResizableImageView に置き換えます。

<"the name of your package".ResizableImageView
            android:id="@+id/iv_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

ResizableImageView.class

    public class ResizableImageView extends ImageView {
        public ResizableImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

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

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable d = getDrawable();
            if (d == null) {
                super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
                return;
            }

            int imageHeight = d.getIntrinsicHeight();
            int imageWidth = d.getIntrinsicWidth();

            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            float imageRatio = 0.0F;
            if (imageHeight > 0) {
                imageRatio = imageWidth / imageHeight;
            }
            float sizeRatio = 0.0F;
            if (heightSize > 0) {
                sizeRatio = widthSize / heightSize;
            }

            int width;
            int height;
            if (imageRatio >= sizeRatio) {
                // set width to maximum allowed
                width = widthSize;
                // scale height
                height = width * imageHeight / imageWidth;
            } else {
                // set height to maximum allowed
                height = heightSize;
                // scale width
                width = height * imageWidth / imageHeight;
            }

            setMeasuredDimension(width, height);
        }
    }

解決策:テーブル行内の imageView のサイズをプログラムで変更する必要があります

于 2013-10-16T11:44:56.450 に答える