【问题标题】:fill server Image to NetworkImageview without Stretching it android将服务器图像填充到 NetworkImageview 而不拉伸它android
【发布时间】:2016-02-26 06:11:32
【问题描述】:

我有什么

我有来自服务器的ImageUri,我将它设置为我的NetworkImageview Volley 库

我想要什么

我希望图片占据NetworkImageview的全部width和height(希望类似于facebook)

我的问题

我看到我的NetworkImageview 中的width 是白色的colour 左边

我的代码

xml 布局

<RelativeLayout
        android:id="@+id/relative_profile_hotot"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_below="@+id/relative_profile_header"
        android:padding="0dp">

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/img_post_photo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="center"
            android:src="@drawable/image_palce" />

        <RelativeLayout
            android:id="@+id/relative_profile_footer"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:background="@color/profile_footer_bg">

            <TextView
                android:id="@+id/tv_profile_photo_location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:drawableLeft="@drawable/ic_home_place"
                android:drawablePadding="5dp"
                android:paddingLeft="5dp"
                android:paddingTop="5dp"
                android:textColor="@color/grey_dark"
                android:textSize="@dimen/txt_small"
                tools:text="Paris France" />

            <TextView
                android:id="@+id/tv_profile_photo_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/tv_profile_photo_location"
                android:layout_marginLeft="20dp"
                android:ems="40"
                android:padding="2dp"
                android:singleLine="true"
                android:textColor="@color/grey_light"
                android:textSize="@dimen/txt_very_small"
                tools:text="Effel Tower,...Read more" />
            />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img_foot"
                android:src="@drawable/ic_home_footstep_color"
                android:padding="5dp"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/tv_profile_comment" />

            <TextView
                android:id="@+id/tv_profile_foot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:paddingTop="0dp"
                android:textColor="@color/grey_light"
                android:textSize="@dimen/txt_small"
                tools:text="150"
                android:layout_alignBottom="@+id/tv_profile_photo_description"
                android:layout_alignEnd="@+id/tv_profile_photo_description" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img_cheers"
                android:src="@drawable/ic_home_cheers_color"
                android:padding="5dp"
                android:layout_alignParentTop="true"
                android:layout_toStartOf="@+id/img_comment"
                android:layout_marginEnd="14dp" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img_comment"
                android:src="@drawable/ic_home_coments"
                android:padding="5dp"
                android:layout_alignParentTop="true"
                android:layout_alignStart="@+id/tv_profile_comment" />

            <TextView
                android:id="@+id/tv_profile_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:paddingTop="0dp"
                android:textColor="@color/grey_light"
                android:textSize="@dimen/txt_small"
                tools:text="264"
                android:layout_alignBottom="@+id/tv_profile_photo_description"
                android:layout_toStartOf="@+id/tv_profile_foot" />


            <TextView
                android:id="@+id/tv_profile_cheers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingRight="10dp"
                android:paddingTop="0dp"
                android:textColor="@color/grey_light"
                android:textSize="@dimen/txt_small"
                tools:text="3164"
                android:layout_alignBottom="@+id/tv_profile_photo_description"
                android:layout_toStartOf="@+id/img_comment" />

        </RelativeLayout>

    </RelativeLayout>

Adapter.java

myBucketListViewHolder.imgPostPic.setImageUrl(model.Uri, imageLoader);
        myBucketListViewHolder.imgPostPic.setDefaultImageResId(R.drawable.im_default_cover);

屏幕截图:(

【问题讨论】:

    标签: android layout imageview android-volley


    【解决方案1】:

    替换

    android:scaleType="center"
    

    与

    android:scaleType="centerCrop"
    

    在您的 NetworkImageView 中

    【讨论】:

    • 您没有清楚地发布您的问题。改写你的问题,没有多大意义
    • 如果我使用 centerCrop,我的完整图像将不可见。
    • 您希望正方形图像适合矩形而不修改纵横比?或者如果图像拉伸可以吗? (与 facebook 不同)
    • 图像不应拉伸。我有stackoverflow.com/questions/6202000/…
    • 如果您提到这个问题,我会立即回答。在我看来,这是最干净的解决方案:github.com/solusoftinnova/app_android_carnavalea/blob/master/… 只是从 NetworkImageView 而不是 ImageView 扩展它
    【解决方案2】:

    我使用 NetworkImageView 发生了同样的事情,问题是它的行为与 ImageView 有点不同,当屏幕的密度大于您要插入的图像的宽度时,android: scaleType = "centerCrop" 不足以填充宽度. 试试这个代码,在运行时改变图像的宽度以适应屏幕的密度。

    在你的 layout.xml 中

    <com.android.volley.toolbox.NetworkImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:background="@android:color/transparent"
            android:id="@+id/imagePagerView"/>
    

    在你的活动中(我在适配器中)

        Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        float scWidth = outMetrics.widthPixels;
        float scHeigh = outMetrics.heightPixels;
        mImageView.getLayoutParams().width = (int) scWidth;
        mImageView.getLayoutParams().height = (int) scHeigh;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-17
      • 2023-03-17
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      相关资源
      最近更新 更多