您的位置:首页 > 其它

赵雅智_setImageResource和setImageBitmap

2014-06-18 10:02 183 查看
在4.0.4 r1.2中查看android.widget.ImageView源代码可以发现,setImageBitmap()方法其实是调用了setImageDrawable()方法进行重绘。

Sets a Bitmap as the content of this ImageView.  
  
Parameters:  
bm The bitmap to set  
  
@android.view.RemotableViewMethod  
public void setImageBitmap(Bitmap bm) {  
    // if this is used frequently, may handle bitmaps explicitly  
    // to reduce the intermediate drawable object  
    setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));  
}


Sets a drawable as the content of this ImageView.  
  
Parameters:  
drawable The drawable to set  
  
public void setImageDrawable(Drawable drawable) {  
    if (mDrawable != drawable) {  
        mResource = 0;  
        mUri = null;  
  
        int oldWidth = mDrawableWidth;  
        int oldHeight = mDrawableHeight;  
  
        updateDrawable(drawable);  
  
        if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeigh{  
            requestLayout();  
        }  
        invalidate();  
    }  
}


同样的布局文件,小分辨率手机:
1、使用setImageBitmap设置时,出现如下现象:




2、使用setImageResource时,图片显示正常




原因:
setImageResource(id)会根据设备分辨率进行图片大小缩放适配
setImageBitmap(BitmapFactory.decodeResource(res,id))大小需要手动调。
如果你提供了完整的各种分辨率下的图片的话,两种方法都应该不会有混乱。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: