简体   繁体   中英

How to set ImageView width in android ListView?

ListVeiw Code

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

ListView Adapter code

@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

 @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

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. 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.with(context)
 .load("http://i.imgur.com/rFLNqWI.jpg")
 .resize(100, 100) //Specify whatever size you want
 .centerCrop()
 .transform(imageTransformation)
 .into(imageView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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