Android:FrameLayout子类不绘制

发布于 2024-10-20 02:40:07 字数 2185 浏览 3 评论 0原文

这是有效的:

a) main.xml 中带有两个 ImageView 的 FrameLayout

<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,而前景部分保持不变

因为我需要对背景图像做更多操作,所以我将其放入 FrameLayout 子类中并相应地更改了 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">

    <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>

这是新的 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);
    }
}

会发生什么:

a) 在 setContentView(R.layout.main) 期间调用构造函数;

b) 调用了dispatchDraw 覆盖

c) 图像没有出现在屏幕上...

有人看到吗,为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长不大的小祸害 2024-10-27 02:40:07

我意识到这个问题很老了,但从未得到正确的回答。

实现一个视图并将其放入视图组中是一种合理的方法,但有时您不想这样做(假设您有一个视图组类以某种方式排列所包含的视图)。
在这种情况下,您需要从布局(视图组)视图类中进行绘制。

我认为除了实际的dispatchDraw函数之外,给定的代码应该可以通过阅读它来工作。您错过了 canvas.save() 和 canvas.restore()。
应该很容易快速工作的示例:

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

至少在我的环境中,这对我有用。希望它可以帮助其他有同样问题的人。 :)

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. :)

转角预定愛 2024-10-27 02:40:07

您是否尝试过实现 onDraw() 代替?

dispatchDraw() 与子视图有关。

Have you tried implementing onDraw() instead?

dispatchDraw() has to do with child Views.

墨落画卷 2024-10-27 02:40:07

我认为您应该扩展 View 类并重写 onMeasure()onDraw() 方法。

更多信息请参见:
http://developer.android.com/guide/topics/ui/custom -components.html

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

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/

白日梦 2024-10-27 02:40:07

我猜您想实现 onDraw(),但这可能还不够。据我了解,如果布局除了孩子之外没有什么可绘制的,则可能不会调用 onDraw。您可能需要“欺骗”布局来绘制自身(这样您将得到 onDraw 调用)。我们这样做的一个简单但效率不高的方法(涉及猜测......)是设置布局的背景颜色。

更好的方法可能是阅读布局代码并正确执行:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文