[英]How to set ImageView width in android ListView?
<?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
上添加一些新图像有关)。
@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;
}
@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
模式时,都会发生失真。 如何避免呢? 如果您希望所有图像都是特定大小,请使用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.