- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试定位 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/
我目前正在优化我的 Android 应用程序,并且想知道使用 Picasso 调整图像大小和居中裁剪是否更快,或者直接将图像加载到具有设置的 layout_width 和 layout_height
我的应用程序中有一个画廊。我的照片来自互联网,所以它们是动态添加的。我的画廊不会将它们全部裁剪成完美的正方形。我想这样做只是为了让我的画廊看起来更好。我在 XML 中尝试过这个; ImageView
我创建了一个水平的 recyclerview 来显示用户拍摄的图像。这些图像被中心裁剪并使用 Glide 显示。如何在用户查看显示的图像时实现缓慢缩小。 请引用这个例子: https://dribbb
全部。 我想制作闪屏: @drawable/background_splash drawable/background_splash: 但我无法使用 and
我怎样才能拥有一个 ImageView ,其图像填满屏幕,但保持其纵横比并适合中心? 目前我有大图片,大于手机屏幕分辨率,但我怀疑它们会在分辨率更高的设备上被加黑 哪种 scaleType 组合可以达
我得到的是“中心内部”而不是“中心裁剪” activity_main.xml 主要 Activity class MainActivity : AppCom
我正在尝试将照片(通过 url 下载)加载到 ImageView 中,并让它跨越其父 View 的整个宽度,这是 ListView 中的一行。 直接在 ImageView 上设置 scaleType
我想在我的 imageview 中显示图像 (URL) 作为最大宽度和可变高度根据宽度大小,当我使用此代码部分时: imageView = (ImageView) convertView.findVi
假设我从 API 获取一些图像。我事先不知道图像的尺寸是多少,但我知道我希望该图像成为我已设置为某个特定尺寸的 ImageView 的 src .我想要从 API 获得的任何图像填充整个 ImageV
我正在 Android 上创建一个 Activity 。我想在其中显示背景图像。我将一个 ImageView 放入我的主 RelativeLayout 中。 它使 View 像这样。 我
我目前正在使用 Universal Image Loader 1.9.3 并将其初始化为, DisplayImageOptions defaultOptions = new DisplayImageO
我目前正在开发一个抽屉导航,我希望用户照片在这里被缩放、裁剪、着色,然后用作图片圆形裁剪后面的标题图像。 我什至无法发布图片,因为这个帐户的知名度太低了...该死的我用我以前的学生帐户来解决我的大部分
我试图找出 android 在缩放图像时正在做什么,特别是“centercrop”类型。所以为了找到答案,我搜索了 ImageView 源代码并找到了它 here . 所以我尝试的是这段代码: pub
当 2 个 ImageViews 从一个屏幕转到下一个屏幕时,我正在尝试实现共享元素转换。其中一张图像在两个屏幕上都有 centerCrop 的 scaleType。我面临的问题是,当过渡开始时,图像
我正在尝试定位 ImageView,以使图像的底部始终固定在 View 的底部,无论 ImageView 的高度有多小。但是,似乎没有一种比例类型适合我想要做的事情。 CenterCrop 很接近,但
我创建了一个带有标题图像的 Activity 。此标题图像最初是使用 ImageView 在 Activity 的布局 xml 中创建的,其中 scaleType 设置为 centerCrop。这就是
我在 Android Studio 中添加了一个 background-image,我使用 center-crop 将其居中。 我遇到的问题是图像没有正确居中。它稍微偏右,我希望它完全居中 看右边,星
我有以下布局: 图像应使用其矩形填充图像并对其进行缩放以保持其纵横比。当我不移动 View 时它工作正常: 但是,当我移动 View 时,图像被绘制到其边界之外: 我正在使用这个lib
我正在尝试让我的 UIImageView centerCrop 适合我的宽度。在我的 Android 版本中,我需要做的就是这个 android:scaleType="centerCrop",这就是结
我有一个以编程方式创建的 ImageView,并使用 Picasso 加载图像。 ImageView 的高度和宽度设置为 WRAP_CONTENT。图片很大,所以我想使用 Picasso 的 .fit
我是一名优秀的程序员,十分优秀!