java Android设计:在invalidate()之后未调用onDraw()
调用自定义视图(extends view)的invalidate()方法后,不会调用onDraw()方法。这是由我的应用程序设计引起的,但我不确定如何解决这个问题
我在FrameLayout中有两个布局(见下文),它应该像照片编辑软件中的图层一样工作。在代码中,我向layout1和layout2添加了不同的自定义视图。将第二个自定义视图添加到第二个布局后,在调用invalidate()后不会调用第一个自定义视图的onDraw()方法
<FrameLayout
安卓:id="@+id/main_frameLayout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<LinearLayout
安卓:id="@+id/main_layout1"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" />
<LinearLayout
安卓:id="@+id/main_layout2"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" />
</FrameLayout>
在这两个自定义视图中,我在孔屏幕大小上绘制位图,并绘制一些线。图形在两个自定义视图中都实现,例如:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (w > 0 && h > 0) {
this.bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
}
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawBitmap(this.bitmap, 0, 0, new Paint());
drawSomeLines(canvas);
}
如果我只在一个布局中添加一个自定义视图,一切都很好。有人知道我的结构/代码有什么问题吗
# 1 楼答案
我自己想出来的,也许这个解决方案对任何人都有帮助
问题是我将自定义视图添加到了不同的布局中。我删除了两个LinearLayout,并将自定义视图直接添加到FrameLayout,效果很好