ImageView 宽度适配,从顶部开始

3

我有一张图片需要适应宽度,但由于它很长,我需要保留最上面的部分可见,我尝试使用ImageView:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@mipmap/day_length_background"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    />

但结果是: enter image description here 我不能使用fitXY,因为它会改变宽高比。
我需要的效果是这样的:enter image description here

尝试将其设置为imageView的背景。如果不起作用,您可以提供您正在使用的实际图像链接。 - ADM
5个回答

4
你可以使用这个实现了 TopCropImageView 的代码:
class TopCropImageView : AppCompatImageView {
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        recomputeImgMatrix()
    }

    override fun setFrame(l: Int, t: Int, r: Int, b: Int): Boolean {
        recomputeImgMatrix()
        return super.setFrame(l, t, r, b)
    }

    private fun init() {
        scaleType = ScaleType.MATRIX
    }

    private fun recomputeImgMatrix() {
        val drawable = drawable ?: return
        val matrix = imageMatrix
        val scale: Float
        val viewWidth = width - paddingLeft - paddingRight
        val viewHeight = height - paddingTop - paddingBottom
        val drawableWidth = drawable.intrinsicWidth
        val drawableHeight = drawable.intrinsicHeight
        scale = if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
            viewHeight.toFloat() / drawableHeight.toFloat()
        } else {
            viewWidth.toFloat() / drawableWidth.toFloat()
        }
        matrix.setScale(scale, scale)
        imageMatrix = matrix
    }
}

顶部将对齐,底部的视图将被裁剪。


1

1
fitStart不起作用,因为图像的高度大于屏幕高度,所以它适合高度而不是宽度 :| - AVEbrahimi

0
将您的图像放入drawable文件夹中,并将该图像放入您的布局背景中,例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/image">

0

你也可以将你的视图分成两半 输入图像描述

或者在Java中使用自定义矩阵

if (imageView.getScaleType() != ImageView.ScaleType.MATRIX) {
                    imageView.setScaleType(ImageView.ScaleType.MATRIX);
                }
                Matrix m = imageView.getImageMatrix();
                int dw = imageView.getDrawable().getIntrinsicWidth();
                int dh = imageView.getDrawable().getIntrinsicHeight();
                int msWidth, msHeight;
                float scalew, scaleh;

                Display display = activity.getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                msWidth = size.x;
                if (imageView.getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
                    msWidth = dw < msWidth ? dw : msWidth;
                }
                scalew = (float) msWidth / dw;
                msHeight = size.y;
                if (imageView.getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
                    msHeight = dh < msHeight ? dh : msHeight;
                }
                scaleh = (float) msHeight / dh;
                float scale = Math.max(scalew, scaleh);

                m.setScale(scale, scale);
                m.postTranslate(0, 0);
                imageView.setImageMatrix(m);
            }

-1
centercrop是你的问题。尝试使用fitXY,如果宽高比大致相同,应该可以解决。

1
不,fitXY 会破坏宽高比。 - AVEbrahimi
如果你只是需要宽度与手机匹配,可以尝试将高度改为wrap_content。如果图片因此超出屏幕长度,可能需要将其放入ScrollView中以保持纵横比并将宽度与屏幕匹配。这是我会尝试的事情。 - Ryan McCaffrey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接