Consider extending the ImageView class, demonstrated in the following example;
package com.abc.customview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.content.Context;
import android.util.AttributeSet;
class MyCanvasView extends ImageView {
public MyCanvasView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyCanvasView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCanvasView(Context context) {
super(context);
}
};
public class CustomView extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Normal XML layout would take the form;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, CustomView"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#00000000"
android:layout_gravity="center"
android:src="@drawable/background"
android:layout_weight="85"/>
</LinearLayout>
Using the extended class requires replacing the "ImageView" class specifier to our extended sub-class using full package namespace;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, CustomView"
/>
<com.abc.customview.MyCanvasView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#00000000"
android:layout_gravity="center"
android:src="@drawable/background"
android:layout_weight="85"/>
</LinearLayout>
Cheers.
No comments:
Post a Comment