Tôi đã có một yêu cầu tương tự; trong trường hợp của tôi, tôi muốn hình ảnh là hình vuông và muốn ImageView khớp với tỷ lệ khung hình để tôi có thể sử dụng nền và phần đệm của nó để vẽ đường viền.
Tôi đã đọc câu trả lời ở đây nhưng thay vì ghi đè ImageView, tôi quyết định tạo Bố cục đảm bảo nội dung của nó (chỉ nên là một chế độ xem) là hình vuông. Bằng cách đó tôi có thể sử dụng một ImageView chuẩn bên trong nó. (Và bạn không bao giờ biết, tôi có thể muốn làm một cái gì đó khác sau này. Mặc dù có thể không.)
Trong trường hợp nó hữu ích cho bất cứ ai khác, đây là mã (cảm thấy tự do để sao chép). Có thể có lỗi như tôi vừa làm cho nó hoạt động cho ứng dụng của tôi sau đó dừng lại. :)
public class SquareLayout extends ViewGroup
{
public SquareLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public SquareLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SquareLayout(Context context)
{
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// Work out width and height, and square size.
int width = r - l;
int height = b - t;
int size, xOffset, yOffset;
if(width < height)
{
size = width;
xOffset = 0;
yOffset = (height - size)/2;
}
else
{
size = height;
xOffset = (width - size)/2;
yOffset = 0;
}
for(int i = 0; i < getChildCount(); i++)
{
View child = getChildAt(i);
child.layout(xOffset, yOffset, size + xOffset, size + yOffset);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// Get width and height.
int w = -1, h = -1;
switch(MeasureSpec.getMode(widthMeasureSpec))
{
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
w = MeasureSpec.getSize(widthMeasureSpec);
break;
case MeasureSpec.UNSPECIFIED:
break;
}
switch(MeasureSpec.getMode(heightMeasureSpec))
{
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
h = MeasureSpec.getSize(heightMeasureSpec);
break;
case MeasureSpec.UNSPECIFIED:
break;
}
// If only one of width/height is unspecified, set them both the same.
if(w == -1 && h != -1)
{
w = h;
}
else if(h == -1 && w != -1)
{
h = w;
}
// Either they're both specified or both unspecified.
int childMeasureSpec;
if(w == -1)
{
childMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
else
{
childMeasureSpec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
}
// Pass through to children.
int maxDimension = 1;
for(int i = 0; i < getChildCount(); i++)
{
View child = getChildAt(i);
child.measure(childMeasureSpec, childMeasureSpec);
maxDimension = Math.max(maxDimension, child.getMeasuredWidth());
maxDimension = Math.max(maxDimension, child.getMeasuredHeight());
}
if(w == -1)
{
w = maxDimension;
h = maxDimension;
}
setMeasuredDimension(w, h);
}
}
Điều đó thường hoạt động; bạn có thể cung cấp đoạn mã chính xác không? Ngoài ra cha mẹ của ImageView là gì? Có lẽ cha mẹ của nó đang buộc một kích thước nhất định, không cho phép ImageView thay đổi kích thước. –
Tôi đã thêm mã. – fhucho
Ah Tôi đã tìm thấy vấn đề, sẽ cung cấp câu trả lời ngay bây giờ. –