本文介绍了编程添加SurfaceView到的FrameLayout这是Z-命令下的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑2A:随意跳到底部简洁问题

EDIT 2a: feel free to jump to the bottom for succinct questions.

我可以绘制SurfaceView。就我而言,我创建一个电子图书这将对SurfaceViews运行本书的每一页不同的动画。

I can Draw a SurfaceView via xml. In my case, I am creating an e-book which will have different animations running on SurfaceViews for each page of the book.

我有,有一个的FrameLayout 名为 @ + A 的.xml 布局ID / animation_layout

I've got a .xml layout that has a FrameLayout called @+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>

根据正在显示它的书页,我想在我的一套延伸 SurfaceView 类添加的一个类的不同实例。

Depending on which page of the book is being displayed, I would like to add a different instance of one of the classes in my set of classes which extend SurfaceView.

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

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

PageAniSurfaceView时,它的实例化和关闭时会创建其观点,即线程踢基本上创建一个线程。

PageAniSurfaceView basically creates a Thread when it's instantiated and kicks off that thread when its View is created.

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类我需要包括该页面。

I have a class called PageDisplayer which keeps track of which page we're on, and should addView() the specific SurfaceView class I need to include for that page.

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中,我可以告诉正在运行的线程,并且onDraws正在调用。在例如定义和显示的位图,Page01SurfaceView绘制一次,但是当update_bitmaps()改变第(x,y)坐标的位图的不重画。

Via breakpoints OR LogCat, I can tell the Threads are running, and the onDraws are being called. The bitmaps defined and displayed in e.g., Page01SurfaceView are drawn once, but not redrawn when update_bitmaps() changes the (x,y) coordinates of the bitmap.

为什么位图不被绘制在每次调用的onDraw(Canvas)的

Why are the bitmaps not being drawn on each call to onDraw(Canvas)?

编辑:如果有在视图中的以上的动画的位图,然后显示在SurfaceView位图

edit: if there's an animation in a View above the bitmaps, then the bitmaps on the SurfaceView are displayed.

编辑2:简洁的问题:

请问一个的ImageView Z-有序以上 SurfaceView 保存 SurfaceView 从图纸本身?

Will an ImageView Z-ordered above a SurfaceView keep the SurfaceView from drawing itself?

如果我只是使用一个<一个href=\"http://groups.google.com/group/android-developers/browse_thread/thread/d754f034c96c6293/5705e8e63a28dc4e?show_docid=5705e8e63a28dc4e&fwc=1&pli=1\"相对=nofollow>视图,而不是一个 SurfaceView ?我要去尝试,并汇报。

Should I just be using a View and not a SurfaceView? I'm going to try that and report back.

推荐答案

我只是用一个视图,而不是SurfaceView。

I'm just using a View, not a SurfaceView.

戴安娜Hackborn说,表面意见是非常特殊的,而不是真正的意见(表面上是一个独立的窗口Z-下令用自己的),它们的Z排序不符合您的观点。从表面上看是一个大的,重的物体;你并不旨在治疗SurfaceView像这样一个常规视图

Dianne Hackborn said "surface views are very special and not really views (the surface is a separate window Z-ordered with your own), their Z-ordering does not match your views. A surface view is a big, heavy object; you are not intended to treat SurfaceView like a regular view in this way."

这篇关于编程添加SurfaceView到的FrameLayout这是Z-命令下的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:26