我想指定这两个分钟。宽度/高度和最大值ImageView的宽度/高度。图像应缩放以适合。
这样做:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:maxWidth="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:src="@drawable/ic_launcher" />
分钟。宽度和高度按照应有的方式执行,但最大值宽度和高度被忽略。
如果我设置android:adjustViewBounds =“true”:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:minWidth="100dp"
android:maxWidth="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:src="@drawable/ic_launcher" />
比,最大。宽度和高度按照应有的方式执行,但最小值。宽度和高度被忽略。
有两种方法可以同时使用吗?
答案 0 :(得分:11)
最后,我创建了自己的视图,它可以访问ImageView并覆盖onMeasure():
public class ResizingImageView extends ImageView {
private int mMaxWidth;
private int mMaxHeight;
public ResizingImageView(Context context) {
super(context);
}
public ResizingImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ResizingImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setMaxWidth(int maxWidth) {
super.setMaxWidth(maxWidth);
mMaxWidth = maxWidth;
}
@Override
public void setMaxHeight(int maxHeight) {
super.setMaxHeight(maxHeight);
mMaxHeight = maxHeight;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Drawable drawable = getDrawable();
if (drawable != null) {
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
if (wMode == MeasureSpec.EXACTLY || hMode == MeasureSpec.EXACTLY) {
return;
}
// Calculate the most appropriate size for the view. Take into
// account minWidth, minHeight, maxWith, maxHeigh and allowed size
// for the view.
int maxWidth = wMode == MeasureSpec.AT_MOST
? Math.min(MeasureSpec.getSize(widthMeasureSpec), mMaxWidth)
: mMaxWidth;
int maxHeight = hMode == MeasureSpec.AT_MOST
? Math.min(MeasureSpec.getSize(heightMeasureSpec), mMaxHeight)
: mMaxHeight;
int dWidth = Helpers.dipsToPixels(drawable.getIntrinsicWidth());
int dHeight = Helpers.dipsToPixels(drawable.getIntrinsicHeight());
float ratio = ((float) dWidth) / dHeight;
int width = Math.min(Math.max(dWidth, getSuggestedMinimumWidth()), maxWidth);
int height = (int) (width / ratio);
height = Math.min(Math.max(height, getSuggestedMinimumHeight()), maxHeight);
width = (int) (height * ratio);
if (width > maxWidth) {
width = maxWidth;
height = (int) (width / ratio);
}
setMeasuredDimension(width, height);
}
}
}
此视图现在可以像:
一样使用<my.package.ResizingImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:maxWidth="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:src="@drawable/ic_launcher" />
这可以按照我的意愿运作,但是不应该有更简单的解决方案吗?
答案 1 :(得分:1)
过去,仅使用XML几乎是不可能的,但是现在使用ConstraintLayout
,它非常简单。
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_min="100dp"
app:layout_constraintWidth_max="200dp"
app:layout_constraintHeight_min="100dp"
app:layout_constraintHeight_max="200dp"
app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"
android:scaleType="centerCrop" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以使用scaleType
或fitXY
中的centerCrop
;其他人不会强迫图像适合ImageView
。
答案 2 :(得分:0)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dp"
android:minHeight="150dp"
>
<RelativeLayout
android:id="@+id/imageLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_gravity="center_vertical"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/trump"
/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
请参阅将最小高度和宽度设置为容器布局,并将adjustViewBounds放在Imageview中。这样,您的任务就可以通过简单的步骤完成。