Android:OnDraw并将该类与xml中的ImageView关联
我正在尝试覆盖 XML 布局文件中特定 ImageView 的默认绘图。
我见过的所有示例都是在活动中使用 setContentView 来执行此操作的。我想保留主要内容视图,只处理一个图像控件的绘图。我不知道如何将 imageview 的 id 与新实例化的类关联起来来处理 OnDraw。
我很可能会倒退,所以任何帮助都是值得赞赏的。
如下所示:
littleImage = (ImageView) findViewById(R.id.myImageView);
View myView = new MyCustomView(littleImage.getContext());
// NO! NO! I want to leave the 'main' view alone
//setContentView(myView);
public void test()
{
littleImage.invalidate();
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/label_title"
android:layout_width="320dip"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:gravity="center_horizontal"
android:background="#6B8AAD"
/>
<ImageView
android:id="@+id/image"
android:clickable="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
/>
<ImageView
android:id="@+id/myImageView"
android:clickable="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:visibility="invisible"
/>
</LinearLayout>
如您所见,我尝试了 imageViews getContext,但这没有帮助。 我怎样才能让它触发OnDraw?
I'm trying to override the default drawing for a particular ImageView in my XML layout file.
All the examples, I've seen do this using setContentView in the activity. I want to leave the main content view alone, and just handle the drawing for the one image control. I'm at a loss on how to associate the id of the imageview with the new instantiated class to handle the OnDraw.
I may very well be going about this backwards, so any help is appreciated.
as in below:
littleImage = (ImageView) findViewById(R.id.myImageView);
View myView = new MyCustomView(littleImage.getContext());
// NO! NO! I want to leave the 'main' view alone
//setContentView(myView);
public void test()
{
littleImage.invalidate();
}
the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/label_title"
android:layout_width="320dip"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:gravity="center_horizontal"
android:background="#6B8AAD"
/>
<ImageView
android:id="@+id/image"
android:clickable="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
/>
<ImageView
android:id="@+id/myImageView"
android:clickable="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:visibility="invisible"
/>
</LinearLayout>
As you see, I've tried the imageViews getContext, but that didn't help.
How can I get this to trigger OnDraw?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此目的的最佳方法是创建一个扩展 ImageView 的 LittleImage 类
,在您的 xml 布局中您可以使用:
The best way to achieve this is by creating a class LittleImage that Extends ImageView
Than, in your xml layout you can use: