点滴:ImageView setImageResource方法对资源的复用
来源:互联网 发布:淘宝捉猫猫是什么 编辑:程序博客网 时间:2024/05/02 02:23
从源代码分析,一步一步贴代码。
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); }
0 0
- 点滴:ImageView setImageResource方法对资源的复用
- imageView的ScaleType缩放和setImageResource方法
- 对于ImageView的两个方法而言。 setImageResource 和 setBackgroundResource
- ImageView的setBackgroundResource与setImageResource区别
- imageview.setImageResource()不显示
- ImageView访问网络图片资源的方法
- 解析给ImageView设置资源的五种方法
- 安卓ImageView的几种资源调用方法
- android ImageView 赋值问题setImageResource和setImageBitmap
- ImageView里setImageResource出现OOM问题
- ImageView setImageResource 无效,记一个bug
- 整理一个用matrix对imageview移动和绽放图片的方法
- 通过drawable比对界面显示的imageview等是否和资源文件的引用一致
- Android 获取设置好的image.setImageResource(R.drawable.xxx)资源
- ImageView的几个方法
- setImageResource与setImageBitmap的区别
- setImageResource与setImageBitmap的区别
- setImageResource与setImageBitmap的区别
- c++ sort函数用法
- node.js base64
- O2O营销模式的特点
- 分表
- 循环输出对象的属性 for in
- 点滴:ImageView setImageResource方法对资源的复用
- vs2010编译vs2008工程的问题
- 给老板汇报技术规划的一些要点
- System 类 和 Runtime 类的常用用法
- Minimum Depth of Binary Tree 二叉树的最小深度
- erlang语法学习
- Oracle中关键字like的使用总结
- Android逆向分析惯用网站
- 构建自己的Linux系统记录(一)