我需要一个ImageView
缩放图像,直到水平填充父级。
如果图像视图背景为红色,而其余内容(图像下方)为绿色,则我正在寻找图片1所示的结果。如果图像宽度大于屏幕宽度,则会自动获得该结果。
但是如果是如图2所示的小图片,则我可以获得的最佳结果是图片3(将ImageView的宽度和高度设置为fill_parent,并将scaleType设置为FitStart
通过设置height= wrap_content, width= fill_parent
,scaleType= CenterCrop
获得图片4。它应该垂直缩放以显示整个图像,但是如scaleType所说,它会对其进行裁剪。
即使图像很小,获得照片1的任何想法吗?
将向工作答案奖励50英镑
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8yRmZiNC5wbmcifQ==” alt =“在此处输入图像描述”>
我找到了一个简单的解决方案,对我真的很有效。根据需要设置宽度和高度,例如:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
然后您还设置此设置:
android:adjustViewBounds="true"
“ adjustViewBounds”默认为false(我觉得很奇怪),但是设置它可以很容易地将图像填充到imageview中。
没有自定义类就可以。这是我用小图像获得的结果:
大图:
您必须使用RelativeLayout
才能起作用,但是您可以将其与LinearLayout
结合使用以实现所需的一切。这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ImageView
android:src="@drawable/cat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_above="@id/button_container"/>
</RelativeLayout>
</ScrollView>
诀窍是您将其设置为填满整个屏幕,但它必须位于其他布局的上方。这样您就可以实现所需的一切。
尝试这个:
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class CustomImageView extends android.widget.ImageView {
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void fitYUniformly() {
final Drawable drawable = getDrawable();
if (drawable == null) return;
final int dwidth = drawable.getIntrinsicWidth();
final int dheight = drawable.getIntrinsicHeight();
if (dwidth == -1 || dheight == -1) return;
int vheight = this.getHeight();
float scale = (float) vheight / (float) dheight;
final int vwidth = (int) (dwidth * scale);
scale(scale, vwidth, vheight);
}
public void fitXUniformly(int parentWidth) {
final Drawable drawable = getDrawable();
if (drawable == null) return;
final int dwidth = drawable.getIntrinsicWidth();
final int dheight = drawable.getIntrinsicHeight();
if (dwidth == -1 || dheight == -1) return;
int vwidth = parentWidth;// here,you need to pass the width of parentview
// int vwidth = this.getWidth();
float scale = (float) vwidth / (float) dwidth;
final int vheight = (int) (dheight * scale);
scale(scale, vwidth, vheight);
}
private void scale(float scale, int newWidth, int newHeight) {
final ViewGroup.LayoutParams params = this.getLayoutParams();
params.width = newWidth;
params.height = newHeight;
this.setLayoutParams(params);
this.setScaleType(ScaleType.MATRIX);
final Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
this.setImageMatrix(matrix);
}
}
注意:
不要忘了打电话给fitXUniformly(parentWidth);
。在这里,parentWidth
将是CustomImageView
父级的宽度。
希望对您有所帮助!