Android ImageView topCrop/bottomCrop scaletype?
Android ImageView topCrop/bottomCrop scaletype?
我有一个正方形 ImageView
可以显示不同尺寸的图片。我想始终保持图片的原始纵横比并且图像周围没有边距(以便图像占据整个 ImageView)。为此,我在 ImageView
上使用 centerCrop
scaleType。但是,我想这样做,如果图像的顶部和底部被切断(即:图像的高度大于宽度),图像就会被拉向容器的底部。因此,不是在顶部和底部裁剪等量的像素,而是图像与 ImageView
的顶部和侧面齐平,而图像底部的裁剪量是原来的两倍。这在 xml 中是否可行,如果不能,是否有 java 解决方案?
您将无法使用常规 ImageView
执行此操作,它的属性在 xml 中。您可以使用适当的 scaleType
Matrix
来完成它,但是写它是一件很痛苦的事情。我建议您使用可以轻松处理此问题的受人尊敬的库。例如 CropImageView
.
您可能无法在布局中执行此操作。但是用这样的一段代码是可能的:
final ImageView image = (ImageView) findViewById(R.id.image);
// Proposing that the ImageView's drawable was set
final int width = image.getDrawable().getIntrinsicWidth();
final int height = image.getDrawable().getIntrinsicHeight();
if (width < height) {
// This is just one of possible ways to get a measured View size
image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int measuredSize = image.getMeasuredWidth();
int offset = (int) ((float) measuredSize * (height - width) / width / 2);
image.setPadding(0, offset, 0, -offset);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
image.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
}
注意,如果您的 ImageView
有预定义的大小(可能有),那么您需要将此大小放入 dimen
资源,代码将更简单:
ImageView image = (ImageView) findViewById(R.id.image2);
// For sure also proposing that the ImageView's drawable was set
int width = image.getDrawable().getIntrinsicWidth();
int height = image.getDrawable().getIntrinsicHeight();
if (width < height) {
int imageSize = getResources().getDimensionPixelSize(R.dimen.image_size);
int offset = (int) ((float) imageSize * (height - width) / width / 2);
image.setPadding(0, offset, 0, -offset);
}
另请参阅:
我有一个正方形 ImageView
可以显示不同尺寸的图片。我想始终保持图片的原始纵横比并且图像周围没有边距(以便图像占据整个 ImageView)。为此,我在 ImageView
上使用 centerCrop
scaleType。但是,我想这样做,如果图像的顶部和底部被切断(即:图像的高度大于宽度),图像就会被拉向容器的底部。因此,不是在顶部和底部裁剪等量的像素,而是图像与 ImageView
的顶部和侧面齐平,而图像底部的裁剪量是原来的两倍。这在 xml 中是否可行,如果不能,是否有 java 解决方案?
您将无法使用常规 ImageView
执行此操作,它的属性在 xml 中。您可以使用适当的 scaleType
Matrix
来完成它,但是写它是一件很痛苦的事情。我建议您使用可以轻松处理此问题的受人尊敬的库。例如 CropImageView
.
您可能无法在布局中执行此操作。但是用这样的一段代码是可能的:
final ImageView image = (ImageView) findViewById(R.id.image);
// Proposing that the ImageView's drawable was set
final int width = image.getDrawable().getIntrinsicWidth();
final int height = image.getDrawable().getIntrinsicHeight();
if (width < height) {
// This is just one of possible ways to get a measured View size
image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int measuredSize = image.getMeasuredWidth();
int offset = (int) ((float) measuredSize * (height - width) / width / 2);
image.setPadding(0, offset, 0, -offset);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
image.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
}
注意,如果您的 ImageView
有预定义的大小(可能有),那么您需要将此大小放入 dimen
资源,代码将更简单:
ImageView image = (ImageView) findViewById(R.id.image2);
// For sure also proposing that the ImageView's drawable was set
int width = image.getDrawable().getIntrinsicWidth();
int height = image.getDrawable().getIntrinsicHeight();
if (width < height) {
int imageSize = getResources().getDimensionPixelSize(R.dimen.image_size);
int offset = (int) ((float) imageSize * (height - width) / width / 2);
image.setPadding(0, offset, 0, -offset);
}
另请参阅: