点滴:ImageView setImageResource方法对资源的复用
2014-01-20 16:09
603 查看
从源代码分析,一步一步贴代码。
1,setImageResource 设置mResource
2,根据mResource加载Drawable
3,Resource.java getDrawable调用loadDrawable
4,Resource.java 关键的在getCachedDrawable
5,BitmapDrawable
1,setImageResource 设置mResource
public void setImageResource(int resId) { if (mUri != null || mResource != resId) { updateDrawable(null); mResource = resId; mUri = null; final int oldWidth = mDrawableWidth; final int oldHeight = mDrawableHeight; resolveUri(); if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) { requestLayout(); } invalidate(); } }
2,根据mResource加载Drawable
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; } }
3,Resource.java getDrawable调用loadDrawable
public Drawable getDrawable(int id) throws NotFoundException { TypedValue value; synchronized (mAccessLock) { value = mTmpValue; if (value == null) { value = new TypedValue(); } else { mTmpValue = null; } getValue(id, value, true); } Drawable res = loadDrawable(value, id); synchronized (mAccessLock) { if (mTmpValue == null) { mTmpValue = value; } } return res; }
4,Resource.java 关键的在getCachedDrawable
Drawable loadDrawable(TypedValue value, int id) throws NotFoundException { ... Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key); if (dr != null) { return dr; } Drawable.ConstantState cs; if (isColorDrawable) { cs = sPreloadedColorDrawables.get(key); } else { cs = sPreloadedDrawables[mConfiguration.getLayoutDirection()].get(key); } if (cs != null) { dr = cs.newDrawable(this); } else {... .... private Drawable getCachedDrawable( LongSparseArray<WeakReference<ConstantState>> drawableCache, long key) { synchronized (mAccessLock) { WeakReference<Drawable.ConstantState> wr = drawableCache.get(key); if (wr != null) { // we have the key Drawable.ConstantState entry = wr.get(); if (entry != null) { //Log.i(TAG, "Returning cached drawable @ #" + // Integer.toHexString(((Integer)key).intValue()) // + " in " + this + ": " + entry); return entry.newDrawable(this); } else { // our entry has been purged drawableCache.delete(key); } } } return null; }
5,BitmapDrawable
final static class BitmapState extends ConstantState { Bitmap mBitmap; int mChangingConfigurations; int mGravity = Gravity.FILL; Paint mPaint = new Paint(DEFAULT_PAINT_FLAGS); Shader.TileMode mTileModeX; Shader.TileMode mTileModeY; int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT; BitmapState(Bitmap bitmap) { mBitmap = bitmap; } BitmapState(BitmapState bitmapState) { this(bitmapState.mBitmap); mChangingConfigurations = bitmapState.mChangingConfigurations; mGravity = bitmapState.mGravity; mTileModeX = bitmapState.mTileModeX; mTileModeY = bitmapState.mTileModeY; mTargetDensity = bitmapState.mTargetDensity; mPaint = new Paint(bitmapState.mPaint); } @Override public Drawable newDrawable() { return new BitmapDrawable(this); } @Override public int getChangingConfigurations() { return mChangingConfigurations; } } private BitmapDrawable(BitmapState state) { mBitmapState = state; mTargetDensity = state.mTargetDensity; setBitmap(state.mBitmap); }
相关文章推荐
- 关于ImageView.setBackgroundResource()方法不显示图片
- imageView的ScaleType缩放和setImageResource方法
- Volley NetworkImageView setImageResource 不起作用 原因
- 解决Android ImageView用setImageDrawable方法图片缩小的问题
- Android中ImageView的setScaleType方法
- ImageView访问网络图片资源的方法
- Android中ImageView的setScaleType方法
- ImageView.setImageURI()方法 图片界面不更新问题解决方案
- Android中ImageView的setScaleType方法
- imageView的.setBackgroundResource 、setBackground没有效果
- 利用运行时,给UIImageView写一个分类,交换里面的setImage的方法,可以重绘图片,提高内存的利用率(要是没有重绘图片,直接使用系统提供的setImag就会造成占用大量的内存问题)
- 安卓 imageview资源图片ID获取方法
- Android 获取设置好的image.setImageResource(R.drawable.xxx)资源
- android 中遇到 imageView getWidth 始终为0 时 ,设置 setImageBitmap 的方法
- Android中ImageView的setScaleType方法属性解析
- android 中遇到 imageView getWidth 始终为0 时 ,设置 setImageBitmap 的方法
- 在代码中为ImageView引用图片之setImageDrawable和setBackgroundResource
- ImageView setImageResource 无效,记一个bug
- 安卓 ImageView使用 drawable下资源 闪退的解决方法
- Android学习札记17:ImageView中的setImageBitmap()方法