How to read android: src in my custom component

I am trying to create a custom component that inherits a RelativeLayout.

In my xml layout file, I have:

<Mycomponent 
    android:src="@drawable/my_test_image">
      <TestView>
</Mycomponent>

My question is how to create a Drawable class in Mycomponent constructor?

I tried to read the source code of ImageView, but it looks like it tried to use some android Internal.R file.

In any case, I can do this in my code.

Thank.

+3
source share
2 answers

I think Luksprog is wrong, I have a simple solution to access your custom src components without an erasable, just calling attribute:

attrs.getAttributeResourceValue( "http://schemas.android.com/apk/res/android", "src", 0);

, , jeje.

public CustomView(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));
}

- xml, :

<com.example.com.jfcogato.mycomponent.CustomView
    android:id="@+id/tAImageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:src="@drawable/big_image_example"/>
+14

, ...

    int attributeIds[] = { android.R.attr.src };
    TypedArray a = context.obtainStyledAttributes(attributeIds);
    int resourceId = a.getResourceId(0, 0);
    a.recycle();

, 0 .

, ... jfcogato .

+1

All Articles