Android 以编程方式将SurfaceView添加到ImageView下按Z顺序排列的FrameLayout
编辑2a:请随意跳到底部,以获得简洁的问题 我可以。在我的例子中,我正在创建一本电子书,书中的每一页都会在SurfaceView上运行不同的动画 我有一个Android 以编程方式将SurfaceView添加到ImageView下按Z顺序排列的FrameLayout,android,surfaceview,Android,Surfaceview,编辑2a:请随意跳到底部,以获得简洁的问题 我可以。在我的例子中,我正在创建一本电子书,书中的每一页都会在SurfaceView上运行不同的动画 我有一个.xml布局,它有一个名为@+id/animation\u布局的FrameLayout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an
.xml
布局,它有一个名为@+id/animation\u布局的FrameLayout
<?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>
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,我可以知道线程正在运行,并且正在调用onDraws。在例如Page01SurfaceView中定义和显示的位图只绘制一次,但在update_bitmaps()更改位图的(x,y)坐标时不会重新绘制
为什么每次调用onDraw(Canvas)
时都没有绘制位图
编辑:如果位图上方的视图中有动画,则会显示SurfaceView上的位图
编辑2:简明问题:
ImageView
Z顺序排列在SurfaceView
上方是否会使SurfaceView
远离图形本身
我应该只使用一个而不是SurfaceView
?我要试试看,然后再报告。我只是在使用视图,而不是SurfaceView
Dianne Hackborn说:“曲面视图非常特殊,不是真正的视图(曲面是一个单独的窗口,Z顺序与您自己的窗口),它们的Z顺序与您的视图不匹配。曲面视图是一个大而重的对象;您不打算以这种方式将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);
}
}