以编程方式将SurfaceView添加到在ImageView下按Z顺序排列的FrameLayout

Thu*_*bit 5 android surfaceview

编辑2a:随意跳到底部以获得简洁的问题.

我可以通过xml绘制SurfaceView.就我而言,我正在创建一个电子书,它将在SurfaceViews上为本书的每个页面运行不同的动画.

我有一个.xmlFrameLayout调用的布局@+id/animation_layout.

<?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>
Run Code Online (Sandbox Code Playgroud)

根据正在显示的书的哪个页面,我想在我的一组类中添加其中一个类的不同实例SurfaceView.

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

Page02SurfaceView extends PageAniSurfaceView {
    //
    // code here includes onDraw() definition
    //
}
Run Code Online (Sandbox Code Playgroud)

PageAniSurfaceView基本上在实例化时创建一个Thread,并在创建View时启动该线程.

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
}
}
Run Code Online (Sandbox Code Playgroud)

我有一个叫做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);
    }
}
Run Code Online (Sandbox Code Playgroud)

通过断点或LogCat,我可以告诉线程正在运行,并且正在调用onDraws.在例如Page01SurfaceView中定义和显示的位图被绘制一次,但是当update_bitmaps()改变位图的(x,y)坐标时不重绘.

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

编辑:如果位图上方的视图中有动画,则会显示SurfaceView上的位图.

编辑2:简洁问题:

ImageView上面的Z订单是否会SurfaceView阻止SurfaceView自己绘制?

我应该只使用View而不是SurfaceView?我要尝试并报告回来.

Thu*_*bit 0

我只是使用 View,而不是 SurfaceView。

Dianne Hackborn 说“表面视图非常特殊,而不是真正的视图(表面是一个与你自己的 Z 排序的单独窗口),它们的 Z 排序与你的视图不匹配。表面视图是一个又大又重的对象;你是无意以这种方式将 SurfaceView 视为常规视图。”

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