您的位置:首页 > 其它

点滴:ImageView setImageResource方法对资源的复用

2014-01-20 16:09 603 查看
从源代码分析,一步一步贴代码。

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);
}






                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: