以编程方式将SurfaceView添加到位于ImageView下方的FrameLayout中

5

编辑2a:如有需要,请跳至底部以查看简明问题。

我可以通过xml 绘制SurfaceView。在我的情况下,我正在创建一本电子书,每一页都会有不同的动画在SurfaceViews上运行。

我有一个名为@+id/animation_layoutFrameLayout.xml布局。

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    >
        <fi.harism.curl.CurlView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/curl"
        >
        </fi.harism.curl.CurlView>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/animation_layout"
        />
        <include layout="@layout/narration" 
        />
    </RelativeLayout>

根据显示的书籍页面,我想要添加一组继承了SurfaceView类的不同实例中的一个。
Page01SurfaceView extends PageAniSurfaceView {
    //
    // code here includes onDraw() definition
    //
}

Page02SurfaceView extends PageAniSurfaceView {
    //
    // code here includes onDraw() definition
    //
}

PageAniSurfaceView基本上在实例化时创建一个线程,并在其视图创建时启动该线程。
public class PageAniSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private final String TAG = this.getClass().getSimpleName();
private TutorialThread _thread;

public PageAniSurfaceView(Context context) {
    super(context);
    init();
}

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

public void setBackground(int bg_id)
{
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);
    // make the PageAniSurfaceView focusable so it can handle events
    setFocusable(true);

}

protected void init()
{
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    _thread = new TutorialThread(getHolder(), this);
    _thread.setRunning(true);
    _thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    _thread.setRunning(false);
    while (retry) {
        try {
            _thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // we will try it again and again...
        }
    }
}

protected void draw_bitmaps(Canvas canvas)
{
         // will be overridden by child classes
}

@Override
protected void onDraw(Canvas canvas) {
    if(this.getVisibility() == View.VISIBLE)
    {
        if(canvas != null)
        {
            draw_bitmaps(canvas);
        }
    }
}

public void update_bitmaps() 
{
         // will be overridden by child classes
}

public void elementStarted(PageElement _pageElement) {
    // Nothing in parent class
}

public void elementFinished(PageElement mElement) {
    // Nothing in parent class
}
}

我有一个名为PageDisplayer的类,它跟踪我们所在的页面,并应该addView()包含该页面所需的特定SurfaceView类。
public void displayPage()
{
    page = sSystemRegistry.pageSystem.getCurrentPage();
    mBookReader.mAnimationLayout.removeAllViews();

    PageAniSurfaceView _ani = null;

    switch(page.index)
    {
    case 1:
        _ani = new Page01SurfaceView(mBookReader);
        break;
    case 2:
        _ani = new Page02SurfaceView(mBookReader);
        break;
    case 3:
        _ani = new Page03SurfaceView(mBookReader);
        break;

    }

    if(_ani != null)
    {
        _ani.setWillNotDraw(false);
                    // mBookReader.mAnimationLayout is a FrameLayout in my .xml
        mBookReader.mAnimationLayout.addView(_ani);
        mElementDisplayer.setElementListener(_ani);
    }
}

通过断点或LogCat,我可以看出线程正在运行,并且onDraw被调用。例如,在Page01SurfaceView中定义和显示的位图只绘制一次,但在update_bitmaps()更改位图的(x,y)坐标时不会重新绘制。

为什么每次调用onDraw(Canvas)时位图都没有被绘制?

编辑:如果一个视图中有动画位于位图上方,则SurfaceView上的位图将被显示。

编辑2:简明问题:

ImageView是否Z-ordered位于SurfaceView上方会阻止SurfaceView自行绘制?

我应该只使用View而不是SurfaceView吗? 我将尝试并报告结果。

1个回答

0

我只是使用了一个View,而不是SurfaceView。

Dianne Hackborn说:“SurfaceView非常特殊,不是真正的视图(表面是一个单独的窗口,按照自己的Z顺序排列),它们的Z顺序与您的视图不匹配。SurfaceView是一个庞大而沉重的对象;您不应该以这种方式将SurfaceView视为常规视图。”

https://groups.google.com/d/topic/android-developers/COffLpanlz0/discussion


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接