我有一个包含以下内容的框:
Button
:触摸以折叠或展开下方图片。
CustomImageView
:崩溃时设为GONE
,展开时设为VISIBLE
。调整图像大小以填充屏幕宽度,如下代码所示:
公共类CustomImageView扩展了ImageView {
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable != null)
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int diw = drawable.getIntrinsicWidth();
if (diw > 0) {
int height = width * drawable.getIntrinsicHeight() / diw;
setMeasuredDimension(width, height);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
首次运行默认设置时,图片会展开(VISIBLE
)。
+-------------+
|-----(+)-----|
===============
| ----------- |
| | | |
| | (IMG) | |
| | | |
| ----------- |
===============
按下按钮+
后,图片为GONE
,
+-------------+
|-----(-)-----|
===============
接下来,按下按钮-
,图像设置为VISIBLE
,
+-------------+
|-----(+)-----|
===============
| ----------- |
| | (IMG) | |
===============
问题出在这里,图像只显示其中的一部分,而不是全高。
如果我使用默认支持ImageView
类,它肯定会正确显示,但是,它不会调整到屏幕的全宽(高度按宽度比调整大小)。
有人能指出我这个问题吗? 谢谢!