从资源 XML 中的 android:src 引用我的自定义 BitmapDrawable
我有一个 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);
}
}
:
- 如何在 XML 中指定我的
CustomDrawable
(并告诉它使用@drawable/content
) - 问题 然后告诉布局 XML 中的
ImageView
指向我的CustomDrawable
?
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:
- How do I specify my
CustomDrawable
in XML (and tell it to use@drawable/content
) - How do I then tell the
ImageView
in the layout XML to point to myCustomDrawable
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在 xml 文件中执行此操作,但可以在代码中执行此操作:
You can't do it xml file, but you can do in code: