开发者

Android: FrameLayout subclass does not draw

开发者 https://www.devze.com 2023-02-15 08:19 出处:网络
This is, what works: a) FrameLayout with two ImageViews in main.xml <FrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"

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://s开发者_开发知识库chemas.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?


I realize that this question is old, but it was never answered properly.

Implementing a View to put into the view group is a reasonable way to go, but sometimes you don't want to do that (say you have a view group class that arranges the contained views in some way). In this case you need to draw from the layout (view group) view class.

I think that the given code should work, from reading through it, aside from the actual dispatchDraw function. You missed canvas.save() and canvas.restore(). Example that should be easy to get working quickly:

@Override
public void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    canvas.save();
    canvas.drawCircle(cX, cY, bgRadius, bgPaint);
    canvas.restore();
}

This has worked for me, at least, in my context. Hopefully it helps others who have the same problem. :)


Have you tried implementing onDraw() instead?

dispatchDraw() has to do with child Views.


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/


I'm guessing that you wanted to implement onDraw(), but that is probably not enough. To my understanding the layouts may not call onDraw if they have nothing to draw besides their children. You might need to "trick" the layout to draw itself (so you'll get the onDraw call). An easy yet not efficient way we did this (involved guesswork...) is to set the background color of the layout.

A better way is probably to read the layout code and do it properly :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号