imageview设置在最顶层_Android ImageView设置图片原理(上)

This does Bitmap reading and decoding on the UI

* thread, which can cause a latency hiccup.  If that's a concern,

* consider using {@link #setImageDrawable(android.graphics.drawable.Drawable)} or

* {@link #setImageBitmap(android.graphics.Bitmap)} and

* {@link android.graphics.BitmapFactory} instead.

*

* @param resId the resource identifier of the the drawable

*

* @attr ref android.R.styleable#ImageView_src

*/

@android.view.RemotableViewMethod

public void setImageResource(int resId) {

if (mUri != null || mResource != resId) {

updateDrawable(null);

mResource=resId;

mUri=null;

resolveUri();

requestLayout();

invalidate();

}

}

private void resolveUri() {

if (mDrawable != null) {

return;

}

Resources rsrc=getResources();

if (rsrc== null) {

return;

}

Drawable d=null;

if (mResource != 0) {

try {

d=rsrc.getDrawable(mResource);

} catch (Exception e) {

Log.w("ImageView", "Unable to find resource: " + mResource, e);

// Don't try again.

mUri=null;

}

} else if (mUri != null) {

从源码上看,mResource不为空,而mUri不为空,所以下面的方法咱们只贴出来一部分,可以看到,图片drawable值是通过Resource对象在UI线程中完成。方法上面的解释也可佐证,同时引出下面两个问题,由于SetImageResource会使UI线程延迟,所以可以考虑下面两种做法

imageView.setImageDrawable(drawable);获得图片资源运行在子线程

/**

* Sets a drawable as the content of this ImageView.

*

* @param drawable The drawable to set

*/

public void setImageDrawable(Drawable drawable) {

if (mDrawable != drawable) {

mResource=0;

mUri=null;

int oldWidth&#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值