Sco*_*sen 32 android android-layout android-imageview
我希望父视图设置ImageView的宽度,高度应该是高宽比例.原因是next会显示一个我想放在ImageView下面的TextView.
我可以使用正确显示图像
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Run Code Online (Sandbox Code Playgroud)
但是ImageView高度变为父高度,远远大于显示的拉伸图像的高度.一个想法是让父母垂直变小,但是......我还不知道拉伸的图像大小.
以下操作无效,因为水平未填充小图像.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)
随波逐流
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)
对于它周围的RelativeLayout一切都没有帮助.还尝试了FrameLayout和LinearLayout并失败了.
有任何想法吗?
小智 74
您必须将adjustViewBounds设置为true.
imageView.setAdjustViewBounds(true);
Run Code Online (Sandbox Code Playgroud)
Har*_*ana 30
有两种情况,如果您的实际图像大小等于或大于您的ImageView宽度和高度,那么您可以使用adjustViewBounds属性,如果您的实际图像大小小于ImageView宽度和高度,则使用scaleType属性在ImageView中显示基于您的图像需求.
1.实际图像大小等于或大于ImageView所需的宽度和高度.
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher"/>
Run Code Online (Sandbox Code Playgroud)
2.实际图像尺寸小于ImageView所需的宽度和高度.
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"/>
Run Code Online (Sandbox Code Playgroud)
因此,我不止一次遇到相同的问题,通过查看现有的stackoverflow答案已经意识到,对于解决该问题的真正困惑,没有任何答案可以给出完美的解释。因此,您在这里:
imageView.setAdjustViewBounds(true);
或(以XML格式)
android:adjustViewBounds="true"
解决了此问题,无论图像资源的实际大小如何,它都可以工作,即它将按比例放大或缩小图像到布局中所需的大小。
API 17以下的版本android:adjustViewBounds="true"
仅适用于缩小图像,而不适用于增长图像,即,如果图像源的实际高度小于您要在布局中实现的尺寸,wrap_content
则将使用该较小的高度而不是“放大”(放大)您想要的图像。
因此,对于API 17及更低版本,您别无选择,只能使用自定义ImageView来实现此行为。您可以自己编写一个自定义ImageView或使用已经完成该工作的库。
可能有多个库可以解决此问题,其中一个是:
compile 'com.inthecheesefactory.thecheeselibrary:adjustable-imageview:1.0.0'
像这样使用:
<com.inthecheesefactory.thecheeselibrary.widget.AdjustableImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/your_drawable"/>
Run Code Online (Sandbox Code Playgroud)
除了使用现有库之外,您还可以自己编写一个自定义视图,例如:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
public class ScalableImageView extends ImageView {
boolean adjustViewBounds;
public ScalableImageView(Context context) {
super(context);
}
public ScalableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScalableImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setAdjustViewBounds(boolean adjustViewBounds) {
this.adjustViewBounds = adjustViewBounds;
super.setAdjustViewBounds(adjustViewBounds);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable == null) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
if (adjustViewBounds) {
int drawableWidth = drawable.getIntrinsicWidth();
int drawableHeight = drawable.getIntrinsicHeight();
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (heightMode == MeasureSpec.EXACTLY && widthMode != MeasureSpec.EXACTLY) {
int height = heightSize;
int width = height * drawableWidth / drawableHeight;
if (isInScrollingContainer())
setMeasuredDimension(width, height);
else
setMeasuredDimension(Math.min(width, widthSize), Math.min(height, heightSize));
} else if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
int width = widthSize;
int height = width * drawableHeight / drawableWidth;
if (isInScrollingContainer())
setMeasuredDimension(width, height);
else
setMeasuredDimension(Math.min(width, widthSize), Math.min(height, heightSize));
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
private boolean isInScrollingContainer() {
ViewParent parent = getParent();
while (parent != null && parent instanceof ViewGroup) {
if (((ViewGroup) parent).shouldDelayChildPressedState()) {
return true;
}
parent = parent.getParent();
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
...,您将使用以下格式(XML):
<com.YOUR_PACKE_NAME.ScalableImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/your_drawable" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19855 次 |
最近记录: |