android 获取自定义组件 Image src
来源:互联网 发布:thinkphp5 源码分析 编辑:程序博客网 时间:2024/06/06 22:04
创建一个自定义组件,继承 ImageView。
在我的 xml 布局文件中是这样设置的:
<Mycomponent android:src="@drawable/my_test_image"></Mycomponent>
如何在 Mycomponent 的 constructor 里创建一个 Bitmap类?首先想查看 ImageView 的源代码,但是 android 的内部代码,我们无法这样使用。
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ImageView, defStyle, 0); Drawable d = a.getDrawable(com.android.internal.R.styleable.ImageView_src);后来发现有2种解决方案。
1:定义过一个自己的属性(例:img),再用下面的方法得到
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ButtonPreference, defStyle, 0); //button的名称Drawable drawable = a.getDrawable(R.styleable.MyComponent_img);a.recycle();
2:通过默认属性获得:public Mycomponent (Context context, AttributeSet attrs) { super(context, attrs); int src_resource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0); this.setImageBitmap(getDrawable(getResources(),src_resource));}public static Bitmap getDrawable(Resources res, int id){ return BitmapFactory.decodeStream(res.openRawResource(id));}
- android 获取自定义组件 Image src
- 【Android】Android自定义组件
- vc image src
- image src base64 svg
- Android组件Android-Universal-Image-Loader学习
- 自定义组件 获取属性值
- 自定义组件属性定义获取
- 自定义android RadioPreference组件
- android自定义组件
- Android自定义组件
- Android自定义Button组件
- Android-构造自定义组件
- android 自定义view组件
- android 自定义组件
- android自定义组件
- Android-创建自定义组件
- android 绘图、自定义组件
- android自定义分页组件
- PHP面向对象之private权限控制
- WinXP如何设置WiFi热点
- 圆圈游戏(扫描线-圆的包含关系)
- C 语言中Const限定符的作用!
- Editplus 3.4 注册码 keycode
- android 获取自定义组件 Image src
- 膏肓痛的自疗法-------为久坐者
- Oracle左连接,右连接
- SD Card命令 - CMD8
- MySQL主从备份
- LeetCode: Best Time to Buy and Sell Stock III
- 数据结构试验 顺序表
- 自动化测试随想(一)
- 单点登录