调整imageView大小后调整图像大小

时间:2017-12-21 15:03:37

标签: android imageview picasso

我正在实现一个名为InApp消息的东西,现在我正在尝试在调整大小后使ImageView适合图像大小。

这是我的imageView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="40dp"
    android:padding="6dp">

   --other components--

    <ImageView
        android:id="@+id/middleLayoutImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:padding="5dp"
        app:layout_constraintBottom_toTopOf="@+id/middleLayoutText"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topWrapper"
        app:layout_constraintVertical_chainStyle="packed" />

   --other components--

</android.support.constraint.ConstraintLayout> 

要下载img并放入此视图,我正在使用Picasso:

        Picasso.with(getApplicationContext())
            .load(inputurl)
            .into(imageView);

现在我的活动看起来像这样: Before fitting

现在我想用纵横比调整父布局的宽度来调整它。我尝试了很多组合而没有成功:

与android更多组合:adjustViewBounds scaleTypes等。

我想实现这样的目标:Resized correctly - 在这种情况下,我使用过picasso:.resize(2000,500).centerInside() - 但它只适用于这种情况。如何自动完成? 在此先感谢您的帮助:)

编辑:

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

    String inputurl = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSQtqf1EUqH4eQNNqYFbSawpiR5bypfOhvlatgS0mC3Ve1nk2adbw"; //http://www.gatesms.eu/img/android.png";
    Picasso.with(getApplicationContext())
            .load(inputurl)
            .resize(2000,500)
            .centerInside()
            .into(imageView);

1 个答案:

答案 0 :(得分:0)

要让Picasso正确适合您的Image,您应该知道Image的大小。但是对于这种情况你不知道图像的大小然后为了良好的UI设计你应该有一个占位符(当毕加索正在加载另一个时显示的图像)将该图像保留在drawable文件夹中让我们调用它{{ 1}}并将其设置为placeholder或者像这样使用毕加索本身:

src

现在是什么?

现在因为你在Picasso.with(getActivity()) .load(imageUrl) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .resize(screenWidth, imageHeight) .fit() .centerCrop() .into(imageView); 毕加索中有另一个Image,现在知道它的高度和宽度,以便它可以很好地适合你的形象。但是如果你没有提供任何图像,那么结果就是非常小的图像或不正确的图像。我还添加了一个可选的错误图像,如果Picasso无法提出请求就会被取代,如果你没有网络连接,你可以省略它。

不要忘记XML中的imageView

相关问题