android 获取自定义组件 Image src
2013-06-06 14:15
381 查看
创建一个自定义组件,继承 ImageView。
在我的 xml 布局文件中是这样设置的:
如何在 Mycomponent 的 constructor 里创建一个 Bitmap类?
首先想查看 ImageView 的源代码,但是 android 的内部代码,我们无法这样使用。
1:定义过一个自己的属性(例:img),再用下面的方法得到
2:通过默认属性获得:
在我的 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】利用自定义View的重绘实现拖动移动,获取组件的尺寸
- Android自定义组件获取本地图片和相机拍照图片
- Android自定义组件添加组件的属性
- Android进阶学习-复合组件自定义Topbar(2)
- Android自定义dialog并实现失去焦点,activity获取焦点的功能
- Android四大组件之ContentProvider自定义
- android自定义日期组件之双DatePicker
- android自定义组件之TopMenu
- Android自定义组件系列【15】——四个方向滑动的菜单实现
- Android 使用自定义组件和自定义属性
- android 在Activity下获取getWindowManager()的屏幕尺寸和自定义View下的获取尺寸
- Android 往自定义View组件里传值
- Android自定义组件(1)
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
- Android自定义组件(2)
- android自定义滑动开关组件【转】
- Android Activity 无法获取组件尺寸
- 在一个组件中调用其他组件的内容||android中怎样在activity中获取fragment中的控件
- [Android] 直接获取组件的宽高:Activity.onWindowFocusChanged()
- dorado自定义组件虚拟属性的设置和获取