加载图片中默认图适配

前段时间项目让调ui,所有图片的默认图统一,但是大大小小的图片有好多,来回改比较麻烦。所以把我的思路在这里记一下。

主要实现方式就是利用layer-list
small_image.xml

<?xml version="1.0" encoding="utf-8"?>
<level-list
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/small_image_failed" android:maxLevel="0" />
    <item android:drawable="@color/image_loading_bg" android:maxLevel="1" />
</level-list>

small_image_failed.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/image_loading_bg" />
        </shape>
    </item>
    <item>
        <bitmap android:gravity="center" android:src="@drawable/logo" />
    </item>
</layer-list>

然后设置给对应的imageview就ok了
image_test.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:orientation="vertical"

      <ImageView
          android:background="@drawable/small_image"
          android:scaleType="centerCrop"
          android:layout_width="100dp"
          android:layout_height="50dp" />

      <ImageView
          android:background="@drawable/small_image"
          android:layout_width="200dp"
          android:layout_height="100dp"
          android:layout_marginTop="10dp"
          android:scaleType="fitXY" />

      <ImageView
          android:background="@drawable/small_image"
          android:scaleType="centerCrop"
          android:layout_width="300dp"
          android:layout_marginTop="10dp"
          android:layout_height="150dp" />

      <ImageView
          android:background="@drawable/small_image"
          android:layout_width="match_parent"
          android:layout_height="250dp"
          android:layout_marginTop="10dp"
          android:scaleType="fitXY" />

</LinearLayout>

这里要说明一下,如果给imageview设置了scaleType就会影响图片的加载方式(src),当然默认图也会影响,而且一般的图片加载框架都是默认设置src,所以想要默认图的大小尺寸不变但同时可能给加载后的图片设置加载方式,最好是把默认图设置成background。一般的图片加载控件有提供加载默认图片的方案,但一样在上面的case中,就会出现默认图拉伸。这种方案适用于默认图片相同,背景颜色相同,但是imageview大小不同的情况,大家视情况而定。
效果如下:
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值