【问题标题】:unable to set the size of image using UIL无法使用 UIL 设置图像大小
【发布时间】:2014-07-14 07:59:20
【问题描述】:

我有两个自定义列表视图,一个是垂直的,另一个是水平的。水平在垂直列表内。垂直列表的一部分在下面

    <com.devsmart.android.ui.HorizontalListView
        android:id="@+id/hlistview"
        android:layout_width="match_parent"
        android:layout_height="155dp"
        android:layout_margin="6dp"
        android:background="#fff"
    />

我使用这个库来处理水平列表视图。水平列表视图的行模板是这个。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:padding="6dip"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtDescription"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#000"
    android:textSize="12sp"
    android:gravity="center_horizontal"
    android:text="likes"
/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="6dip"
    android:orientation="vertical" >


<ImageView
    android:id="@+id/IVImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/stalker"
/>
</LinearLayout>

</LinearLayout>

当我使用 UIL 库在我的数组适配器内的水平列表中填充数据时,它会向我显示小图像,并且图像宽度会根据 textview 的大小而变化。意思是当我在 textview 中写长文本时,它显示的图像宽度等于 textview 的宽度。

这是我的水平列表视图的自定义适配器。

public class HomeHoriLVAdapter extends ArrayAdapter<HomeSliderClassForAdapter> {

Context context;
public ImageLoader imageLoader; 
DisplayImageOptions options;

public HomeHoriLVAdapter(Context context, int resource,
        List<HomeSliderClassForAdapter> objects) {
    super(context, resource, objects);

    File cacheDir = StorageUtils.getOwnCacheDirectory(context, "MyFolderCache");

    imageLoader = ImageLoader.getInstance();
     // Create configuration for ImageLoader (all options are optional)
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                  // You can pass your own memory cache implementation
                 .diskCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
                 .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())                    
                 .build();
     // Initialize ImageLoader with created configuration. Do it once.
     imageLoader.init(config);
        //imageLoader.init(ImageLoaderConfiguration.createDefault(a));
       // imageLoader=new ImageLoader(activity.getApplicationContext());
        options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.stalker_logo_imageload)
        .cacheInMemory(true)
        .cacheOnDisk(true)
        .displayer(new RoundedBitmapDisplayer(20))
        .build();

//          https://github.com/nostra13/Android-Universal-Image-    Loader/blob/master/library/src/com/nostra13/universalimageloader/core/DisplayImageOptions.java
//          link for more functions of DisplayImageOptions();
    this.context = context;

}

private class ViewHolder {
    TextView txtDescription;
    ImageView IVImage;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    HomeSliderClassForAdapter rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.horizontal_listitem, null);
        holder = new ViewHolder();
        holder.txtDescription = (TextView) convertView.findViewById(R.id.txtDescription);
        holder.IVImage = (ImageView) convertView.findViewById(R.id.IVImage);
        holder.IVImage.getLayoutParams().width = LayoutParams.MATCH_PARENT;
        holder.IVImage.getLayoutParams().height = LayoutParams.MATCH_PARENT;
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    Session asd = new Session(getContext());
    asd.getAccessToken();


    holder.txtDescription.setText(rowItem.Description);
    display(holder.IVImage, rowItem.PhotoURL.concat("?access_token=" + asd.getAccessToken()));




    return convertView;

}





public void display(ImageView img, String url, final ProgressBar spinner)
{
    img.getLayoutParams().width = LayoutParams.MATCH_PARENT;
    img.getLayoutParams().height = LayoutParams.MATCH_PARENT;
    imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            spinner.setVisibility(View.VISIBLE);
            }
        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            spinner.setVisibility(View.GONE);
        }
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            spinner.setVisibility(View.GONE);
        }
        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }

});
}

public void display(ImageView img, String url)
{
    imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {

            }
        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

        }
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

        }
        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }

});
}



}

我希望我的图像宽度与父级匹配,平均值等于我的屏幕宽度,高度等于水平列表视图的高度,在我的例子中是 155dp。

【问题讨论】:

  • UIL 始终保持纵横比。也许你应该使用android:scaleType="centerCrop".
  • 即使在设置 centerCorp 之后,它也没有显示完整的图像。我不认为这是由于 UIL。我希望每个项目的水平列表视图的宽度等于我的水平列表视图的宽度
  • 我现在也添加了图片

标签: android listview universal-image-loader custom-lists horizontallist


【解决方案1】:

使用下面的代码

LayoutParams param=(LayoutParams) holder.IVImage.getLayoutParams();
        param.width=image_dim;
        param.height=image_dim;
holder.Places_iv.setLayoutParams(param);

【讨论】:

  • 我希望每个项目的水平列表视图宽度等于我的水平列表视图宽度
  • 您是说您希望项目本身等于水平列表视图的宽度以使用上述方法,但不要为图像视图设置宽度和高度,而是将其设置在包含图像视图和文本视图的布局上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
相关资源
最近更新 更多