解析给ImageView设置资源的五种方法

Android开发中几乎每天都要接触ImageView这个控件。这个控件可以作为加载图片的容器。ImageView给我们提供了五种设置图片资源的方法:

  • setImageBitmap(Bitmap bm)
  • setImageDrawable(Drawable drawable)
  • setImageResource(int resId)
  • setImageURI(Uri uri)
  • setImageIcon(Icon icon)

下面就通过源码来看看这5个方法的区别。

1. setImageBitmap(Bitmap bm)

setImageBitmap方法中,先把bitmap放在一个BitmapDrawable中,然后调用的setImageDrawable方法。

    public void setImageBitmap(Bitmap bm) {
        // Hacky fix to force setImageDrawable to do a full setImageDrawable
        // instead of doing an object reference comparison
        mDrawable = null;
        if (mRecycleableBitmapDrawable == null) {
            mRecycleableBitmapDrawable = new BitmapDrawable(mContext.getResources(), bm);
        } else {
            mRecycleableBitmapDrawable.setBitmap(bm);
        }
        setImageDrawable(mRecycleableBitmapDrawable);
    }
2. setImageDrawable(Drawable drawable)

setImageDrawable方法里面先调用了updateDrawable()方法,然后判断了drawable的宽高是否有改变,如果发生了改变则会调用requestLayout()方法,最后调用invalidate()方法,让新设置的drawable生效。

  • updateDrawable()
    这个方法里用于更新drawable的可见性、宽高、边界、颜色等。

  • requestLayout()
    这个方法会根据View树逐级向上传递,最后由ViewRootImpl#requestLayout方法处理该事件。ViewRootImpl#requestLayout中会调用scheduleTraversals()方法,这是一个异步方法,会调用performTraversals()方法。在performTraversals()方法中会先后调用performMeasure()和performLayout()方法。然后视条件调用performDraw()方法。

  • invalidate()
    该方法会不断向父容器请求刷新并计算需要重绘的区域,最后会传递到ViewRootImpl中触发performTraversals方法,然后调用performDraw()进行重绘。

    /**
     * Sets a drawable as the content of this ImageView.
     *
     * @param drawable t
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值