gpt4 book ai didi

android - imageview android中图像的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:47 33 4
gpt4 key购买 nike

我在 listView 中有一个 imageView。像这样设置:主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
android:id="@+id/listView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

图片.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="center"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

这很好用,可以使用 http 请求从 url 中填充许多图像。但是我对图像的大小有疑问。无论分辨率或大小如何,我都希望它们填满屏幕。尝试了不同的布局高度、宽度和 scaleType,但无法使其正常工作。

第一张图是现在的样子,第二张图是我想要的样子。

编辑:尝试使用 scaleType="fitXY"给了我 100% 的宽度,但在较长的图像上效果不佳。

This is how it looks now This is how i want it to look

最佳答案

我遇到了同样的问题,除了创建自己的类(class)外没有找到其他解决方案。

public class Banner extends View {

private final Drawable mDrawable;

public Banner(Context context) {
this(context, null);
}

public Banner(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public Banner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Banner, 0, 0);
try {
mDrawable = a.getDrawable(R.styleable.Banner_image);
} finally {
a.recycle();
}

setBackgroundDrawable(mDrawable);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0;
int height = 0;
if(mDrawable != null) {
width = MeasureSpec.getSize(widthMeasureSpec);
height = width * mDrawable.getIntrinsicHeight() / mDrawable.getIntrinsicWidth();
}
setMeasuredDimension(width, height);
}

}

然后将属性添加到 /values 文件夹中的某个文件

<declare-styleable name="Banner">
<attr name="image" format="reference" />
</declare-styleable>

并像这样使用这个新控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:banner="http://schemas.android.com/apk/res/your.project.name.here"
android:layout_width="fill_parent"
android:layout_ height="wrap_content"
android:orientation="vertical" >

<your.project.name.here.Banner
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
banner:image="@drawable/ic_launcher" />

</RelativeLayout>

它会根据图像宽度按比例调整高度

关于android - imageview android中图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18782216/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com