简体   繁体   English

如何在Android ListView中设置ImageView宽度?

[英]How to set ImageView width in android ListView?

ListVeiw Code ListVeiw代码

<?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>

I am using picasso for the transformation (Transformation is related to add some new image on this ImageView ). 我正在使用picasso进行转换(转换与在此ImageView上添加一些新图像有关)。

ListView Adapter code 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;
}

code in ImageTransformation 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;
    }

According to above I can see following listView 根据上面我可以看到下面的listView

Questions 问题

  • How to make size fixed of every image whether it is scaled properly or not because in future I will use best layout? 无论以后是否使用最佳布局,如何正确缩放每个图像的大小?
  • How to increase the width of image. 如何增加图像的宽度。 So that the image width will cover whole part of the screen? 这样图像宽度将覆盖整个屏幕?
  • Whenever I am in landscape mode distortion take place. 每当我处于landscape模式时,都会发生失真。 How to avoid it? 如何避免呢?

在此处输入图片说明

If you want all image to be specific size use .resize() function provided by Picasso library and use .centerCrop() to fit the image nicely 如果您希望所有图像都是特定大小,请使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM