Android 如何使用毕加索动态设置ImageView高度?
我正在使用毕加索在一个由两列组成的Android 如何使用毕加索动态设置ImageView高度?,android,xml,imageview,height,picasso,Android,Xml,Imageview,Height,Picasso,我正在使用毕加索在一个由两列组成的GridView中并排显示2个ImageView。 问题是,如果不拉伸图像,我无法找到动态设置ImageView高度的方法 代码如下: ImageListAdapter.java @Override public View getView(int position, View convertView, ViewGroup parent) { // ViewHolder pattern in play ViewHolder viewHolder;
GridView
中并排显示2个ImageView
。
问题是,如果不拉伸图像,我无法找到动态设置ImageView
高度的方法
代码如下:
ImageListAdapter.java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder pattern in play
ViewHolder viewHolder;
if (null == convertView) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.iv_button);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Picasso
.with(context)
.load(imageResources[position])
.fit()
.centerInside()
.into(viewHolder.mImageView);
return convertView;
}
activity\u picasso.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/heenok_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:layout_marginBottom="50dp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv_button"
android:layout_width="match_parent"
android:layout_height="121dp"
android:background="@null"
android:foreground="?attr/selectableItemBackgroundBorderless" />
listview\u item\u image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/heenok_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:layout_marginBottom="50dp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv_button"
android:layout_width="match_parent"
android:layout_height="121dp"
android:background="@null"
android:foreground="?attr/selectableItemBackgroundBorderless" />
我尝试在我的适配器中不使用.fit.centerInside()
,并在我的图像视图中添加android:layout\u height=“wrap\u content”
。它可以工作,但由于大量图像,我经历了巨大的滞后,我的图像混乱,应用程序崩溃
如果我将我的android:layout\u height=”“
更改为wrap\u content
,而不删除.fit.centerInside()
,屏幕将是空白的,就像没有图像一样
如果有人得到答案,我会很高兴 你也试过了吗
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:adjustViewBounds="true"
.../>
但您似乎存在性能问题:
首先,如果在ImageView上指定android:scaleType=“fitXY”,则如果ImageView与图像的比率不相同,则图像将失真。根据要实现的效果选择不同的缩放类型。如果使用另一个scaleType,则fit.centerInside()是无用的,因此只需删除它即可。也要确保使用一个回收视图,以减少使用。。。更多更少。。。内存否则,您将面临内存崩溃,图像太多。