eph*_*amd 1 android android-image android-layout android-imageview android-drawable
我可能有不同的drawables(大或小),我总是跨越宽度(match_parent)并按比例增加或减少高度.保持比例.
这怎么可能?
我尝试过:
<ImageView
android:id="@+id/iv_TEST"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/test" />
Run Code Online (Sandbox Code Playgroud)
问题是它增加或减少宽度,看起来很糟糕.

eph*_*amd 11
固定.要纠正此问题,您需要执行以下操作:
"android:src"从代码(imageview.setImageResource())设置drawable到通过ResizableImageView替换ImageView:
<"the name of your package".ResizableImageView
android:id="@+id/iv_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
ResizableImageView.class
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ResizableImageView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d == null) {
super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
return;
}
int imageHeight = d.getIntrinsicHeight();
int imageWidth = d.getIntrinsicWidth();
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float imageRatio = 0.0F;
if (imageHeight > 0) {
imageRatio = imageWidth / imageHeight;
}
float sizeRatio = 0.0F;
if (heightSize > 0) {
sizeRatio = widthSize / heightSize;
}
int width;
int height;
if (imageRatio >= sizeRatio) {
// set width to maximum allowed
width = widthSize;
// scale height
height = width * imageHeight / imageWidth;
} else {
// set height to maximum allowed
height = heightSize;
// scale width
width = height * imageWidth / imageHeight;
}
setMeasuredDimension(width, height);
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案:我需要以编程方式在表格行中调整imageView的大小
| 归档时间: |
|
| 查看次数: |
6918 次 |
| 最近记录: |