Я пытаюсь переопределить чертеж по умолчанию для конкретного ImageView в моем файле макета XML.
Все примеры, я видел это, используя setContentView в активности. Я хочу оставить только основной контент и просто обработать чертеж для одного элемента управления изображением. Я не понимаю, как связать идентификатор изображения с новым экземпляром класса для обработки 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>
Как вы видите, я пробовал getViewonViewViews, но это не помогло. Как я могу заставить это вызвать OnDraw?
Лучший способ достичь этого - создать класс LittleImage, который расширяет ImageView
public class LittleImage extends ImageView
{
public LittleImage(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas)
{
// Do you custom drawing here...
}
}
Чем в вашем макете xml вы можете использовать:
<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"
/>
<com.yourpackage.LittleImage
android:id="@+id/myImageView"
android:clickable="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:visibility="invisible"
/>
</LinearLayout>