使用Picasso库将Android图片适配到全宽

13
我正在使用Picasso库来加载图片,它的效果很好,但在使用这个库后,图片不适合布局。如果我直接将图片设置到ImageView中,则图片将适合布局。是否有任何选项可以在Picasso中传递方法以适应布局?

我正在使用Picasso库来加载图片,它的效果很好,但在使用这个库后,图片不适合布局。如果我直接将图片设置到ImageView中,则图片将适合布局。是否有任何选项可以在Picasso中传递方法以适应布局?

Picasso.with(getContext())
                    .load(R.drawable.dddd).
                    .into(lavdateimageView);

即使我尝试调用以下方法但无用

fit()croptinside()

XML文件

<?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="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:background="#ffffff"
        >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#ffffff"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="260dp"
                android:layout_marginBottom="3dp"
                android:id="@+id/lavdate_image"
                android:orientation="vertical"

                >
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    android:id="@+id/lavdateimageView" />
            </LinearLayout>
            <TextView

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="New Text to"
                android:padding="3dp"
                android:background="#ffffff"
                android:textColor="#000000"
                android:layout_margin="3dp"
                android:textStyle="bold"
                android:id="@+id/sss"  />

        </LinearLayout>
    </ScrollView>



</LinearLayout>
4个回答

17

我也有同样的问题,所以尝试了这个方法,它能很好地解决问题。

Picasso.get()
       .load(IMAGE_URL)
       .fit()
       .centerCrop()
       .into(imageView);

@iCoders 尝试按原样使用...即同时适应和居中裁剪,它将完美运行。 - Hanzala
@Fetal,如果我使用这个,它就不会显示我的图片。我猜我需要更改我的XML。 - Vision Coderz
@Fetal。谢谢。我使用了android:scaleType="centerCrop"来修复它。 - Vision Coderz

13

最后我明白了我的错误。我使用了android:scaleType="centerCrop",现在我的图片适应了布局。

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scaleType="centerCrop"
      android:id="@+id/myimageView" />

1
<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"/>

myPicassoProvider.get(activity).load(getImageUrl()).into(image);

以这种方式,您的图像将以您想要的宽度呈现,并保持纵横比。

1
尝试像这样更改您的imageview属性
<ImageView 
                android:id="@+id/lavdateimageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
            />

然后

Picasso.with(getContext()).load(R.drawable.dddd)
            .fit()
            .centerCrop()
            .into(lavdateimageView)

@shuvro,谢谢。我已经尝试了android:scaleType="centerCrop",现在它运行得很好。你可以看到我的答案。 - Vision Coderz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接