2012-02-13 46 views
2

我有一個FrameLayout如下(兩者的android:SRC屬性是指在/res/drawable文件夾.png文件):參考我的自定義BitmapDrawable從機器人:SRC在資源XML

<?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. 如何指定我CustomDrawable在XML(並告訴它使用@drawable/content
  2. 我如何再告訴ImageView在佈局XML中指向我的CustomDrawable

回答

2

你不能做到這一點的XML文件,但你可以在代碼中完成:

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

感謝。我真的希望用XML來做到這一點。但我想這是必須要做的。在接受這個答案之前,請等待看看我是否得到了更好的答案:D。順便說一句,這是我做到這一點:'drContent = new CustomDrawable(getResources(),BitmapFactory.decodeResource(getResources(),R.drawable.content)); ivContent.setImageDrawable(drContent);' – curioustechizen 2012-02-13 13:05:57