Search code examples
androidperformancecustom-viewdrawbitmap

Android: FrameLayout subclass does not draw


This is, what works:

a) FrameLayout with two ImageViews in main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/frameLayout1"
    android:layout_height="wrap_content">

    <ImageView android:src="@drawable/radar_background"
        android:id="@+id/background" android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ImageView>

    <ImageView android:src="@drawable/radar_sector" android:id="@+id/sector"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</FrameLayout>

b) RotateAnimation on the background, whereas the foreground sector remains unchanged

Because I need to do a bit more on the background image I put this into a FrameLayout subclass and changed main.xml accordingly:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/frameLayout1"
    android:layout_height="wrap_content">

    <com.decades.SensorTest.RadarView
        android:id="@+id/background" android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
    <ImageView android:src="@drawable/radar_sector" android:id="@+id/sector"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</FrameLayout>

This is the new RadarView.java:

public class RadarView extends FrameLayout {

    private Bitmap mRadar;

    public RadarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadarView(Context context) {
        super(context);
        init();
    }
    private void init() {
        mRadar = BitmapFactory.decodeResource(getResources(), R.drawable.radar_background);
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawBitmap(mRadar, 0, 0, null);
    }
}

What happens:

a) The constructor is called during setContentView(R.layout.main);

b) The dispatchDraw override is called

c) The image does not appear on the screen...

Does anybody see, why?


Solution

  • I think you should extend the View class and override the onMeasure() and onDraw() methods.

    More information here:
    http://developer.android.com/guide/topics/ui/custom-components.html

    Example:
    http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/