在资源XML中引用我自定义的来自android:src的BitmapDrawable

时间:2012-02-13 11:37:01

标签: android bitmap drawable android-resources

我有一个FrameLayout如下(两个android:src属性引用/res/drawable文件夹中的.png文件):

<?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>

现在,我需要为第二个ImageView(指@drawable/content)进行一些调试。为此,我创建了一个自定义BitmapDrawable,如下所示:

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);
    }

}

问题:

  1. 如何在XML中指定我的CustomDrawable(并告诉它使用@drawable/content
  2. 如何告诉布局XML中的ImageView指向我的CustomDrawable

1 个答案:

答案 0 :(得分:2)

您无法执行xml文件,但您可以在代码中执行此操作:

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