gpt4 book ai didi

Android - ImageView bottomCrop 而不是 centerCrop

转载 作者:IT老高 更新时间:2023-10-28 23:26:33 28 4
gpt4 key购买 nike

我正在尝试定位 ImageView,以使图像的底部始终固定在 View 的底部,无论 ImageView 的高度有多小。但是,似乎没有一种比例类型适合我想要做的事情。 CenterCrop 很接近,但我不希望图像居中。类似于 CSS 处理绝对定位的方式。

原因是,我需要为 ImageView 的高度设置动画,但让它看起来好像“显示”了图像的上部。我认为弄清楚这种裁剪图像和动画 ImageView 高度的方法是最简单的方法,但如果有人知道更好的方法,我希望被指出正确的方向。

任何帮助表示赞赏。

最佳答案

Jpoliachik's答案很酷,让我想将它概括为支持上/下和左/右,数量可变。 :) 现在顶部裁剪,只需调用 setCropOffset(0,0) ,底部裁剪 setCropOffset(0,1),左侧裁剪也是 setCropOffset(0, 0),然后右裁剪 setCropOffset(1,0)。如果您想在一维中将视口(viewport)偏移图像的一部分,您可以调用例如setCropOffset(0, 0.25f) 将其下移 25% 的不可见空间,而 0.5f 会将其居中。干杯!

/**
* {@link android.widget.ImageView} that supports directional cropping in both vertical and
* horizontal directions instead of being restricted to center-crop. Automatically sets {@link
* android.widget.ImageView.ScaleType} to MATRIX and defaults to center-crop.
*/
public class CropImageView extends android.support.v7.widget.AppCompatImageView {
private static final float DEFAULT_HORIZONTAL_OFFSET = 0.5f;
private static final float DEFAULT_VERTICAL_OFFSET = 0.5f;

private float mHorizontalOffsetPercent = DEFAULT_HORIZONTAL_OFFSET;
private float mVerticalOffsetPercent = DEFAULT_VERTICAL_OFFSET;

public CropImageView(Context context) {
this(context, null);
}

public CropImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public CropImageView(Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
setScaleType(ScaleType.MATRIX);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
applyCropOffset();
}

/**
* Sets the crop box offset by the specified percentage values. For example, a center-crop would
* be (0.5, 0.5), a top-left crop would be (0, 0), and a bottom-center crop would be (0.5, 1)
*/
public void setCropOffset(float horizontalOffsetPercent, float verticalOffsetPercent) {
if (mHorizontalOffsetPercent < 0
|| mVerticalOffsetPercent < 0
|| mHorizontalOffsetPercent > 1
|| mVerticalOffsetPercent > 1) {
throw new IllegalArgumentException("Offset values must be a float between 0.0 and 1.0");
}

mHorizontalOffsetPercent = horizontalOffsetPercent;
mVerticalOffsetPercent = verticalOffsetPercent;
applyCropOffset();
}

private void applyCropOffset() {
Matrix matrix = getImageMatrix();

float scale;
int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
int drawableWidth = 0, drawableHeight = 0;
// Allow for setting the drawable later in code by guarding ourselves here.
if (getDrawable() != null) {
drawableWidth = getDrawable().getIntrinsicWidth();
drawableHeight = getDrawable().getIntrinsicHeight();
}

// Get the scale.
if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
// Drawable is flatter than view. Scale it to fill the view height.
// A Top/Bottom crop here should be identical in this case.
scale = (float) viewHeight / (float) drawableHeight;
} else {
// Drawable is taller than view. Scale it to fill the view width.
// Left/Right crop here should be identical in this case.
scale = (float) viewWidth / (float) drawableWidth;
}

float viewToDrawableWidth = viewWidth / scale;
float viewToDrawableHeight = viewHeight / scale;
float xOffset = mHorizontalOffsetPercent * (drawableWidth - viewToDrawableWidth);
float yOffset = mVerticalOffsetPercent * (drawableHeight - viewToDrawableHeight);

// Define the rect from which to take the image portion.
RectF drawableRect =
new RectF(
xOffset,
yOffset,
xOffset + viewToDrawableWidth,
yOffset + viewToDrawableHeight);
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.FILL);

setImageMatrix(matrix);
}
}

关于Android - ImageView bottomCrop 而不是 centerCrop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18952271/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com