curioustechizen
curioustechizen

Reputation: 10672

Referring to my custom BitmapDrawable from android:src in a resource XML

I have a FrameLayout as follows (both android:src attributes refer to .png files in /res/drawable folder):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/ivFrame"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/frame" />

    <ImageView
        android:id="@+id/ivContent"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/content" />

</FrameLayout>

Now, I need to do some debugging for the second ImageView (that referring to @drawable/content). For this purpose, I created a Custom BitmapDrawable as follows:

public class CustomDrawable extends BitmapDrawable {


    public CustomDrawable(Resources res, Bitmap bitmap) {
        super(res, bitmap);
        // TODO Auto-generated constructor stub
    }

    public CustomDrawable(Resources res, InputStream is) {
        super(res, is);
        // TODO Auto-generated constructor stub
    }

    public CustomDrawable(Resources res, String filepath) {
        super(res, filepath);
        // TODO Auto-generated constructor stub
    }

    public CustomDrawable(Resources res) {
        super(res);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        Log.d(Constants.LOG_TAG,"CustomDrawable.onBoundsChanged: "+bounds);
        super.onBoundsChange(bounds);
    }

}

Question:

  1. How do I specify my CustomDrawable in XML (and tell it to use @drawable/content)
  2. How do I then tell the ImageView in the layout XML to point to my CustomDrawable?

Upvotes: 2

Views: 1195

Answers (1)

Jin35
Jin35

Reputation: 8612

You can't do it xml file, but you can do in code:

((ImageView) findViewById(R.id.ivContent)).setImageDrawable(new CustomDrawable(...))

Upvotes: 2

Related Questions