【问题标题】:How to set ImageView width in android ListView?如何在 android ListView 中设置 ImageView 的宽度?
【发布时间】:2015-12-14 09:45:33
【问题描述】:

列表视图代码

<?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="fill_parent"
android:padding="6dip">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter"
    android:adjustViewBounds="false"
    android:background="#F8F8F6"
    android:layout_alignParentEnd="false" />
.....
.....
</RelativeLayout>

我正在使用picasso 进行转换(转换与在此ImageView 上添加一些新图像有关)。

ListView 适配器代码

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (null == convertView) {
        convertView = inflater.inflate(R.layout.list_view, parent, false);
    }

    ImageTransform imageTransformation = new ImageTransform(context);


    ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);


    Picasso
            .with(context)
            .load("http://i.imgur.com/rFLNqWI.jpg")
            .transform(imageTransformation)
            .into(imageView);
 return convertview;
}

ImageTransformation 中的代码

 @Override
public Bitmap transform(Bitmap bitmap) {
    // TODO Auto-generated method stub
    synchronized (ImageTransform.class) {
        if(bitmap == null) {
            return null;
        }
        Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
        Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
        Canvas canvas = new Canvas(resultBitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(50);
        paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);

        canvas.drawText("$250", 10, 500, paint);
        canvas.drawBitmap(bitmapImage, 900, 20, null);
        bitmap.recycle();
        return resultBitmap;
    }

根据上面我可以看到listView

问题

  • 如何固定每张图片的大小,无论是否正确缩放,因为将来我会使用最佳布局?
  • 如何增加图像的宽度。让图片宽度覆盖整个屏幕?
  • 每当我处于landscape 模式时,就会发生失真。如何避免?

【问题讨论】:

  • 在你的图像视图 XML 中尝试android:scaleType="centerCrop",它甚至会拉伸图像以适应 imageView 并且在图像列表中看起来更好
  • 在横向模式下,专门为横向重新定义您的 UI,一个好主意是并排显示两个图像,这将解决两件事:->利用空间->合理的宽度缩放图像不会因过多的宽度缩放/拉伸而损失操作系统质量。

标签: android android-listview imageview android-imageview picasso


【解决方案1】:

如果您希望所有图像都具有特定尺寸,请使用 Picasso 库提供的 .resize() 函数并使用 .centerCrop() 很好地适应图像

Picasso.with(context)
 .load("http://i.imgur.com/rFLNqWI.jpg")
 .resize(100, 100) //Specify whatever size you want
 .centerCrop()
 .transform(imageTransformation)
 .into(imageView)

【讨论】:

  • 当我使用 resize(size, size) 时,它没有加载任何图像。查看截图i.imgur.com/Vb9EjMQ.png
  • 您是否尝试过使用其他图片链接,
猜你喜欢
  • 1970-01-01
  • 2013-02-06
  • 2011-03-13
  • 2014-01-23
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
相关资源
最近更新 更多