【问题标题】:Android - A Layout, Surface View and Canvas ConundrumAndroid - 布局、表面视图和画布难题
【发布时间】:2011-06-05 21:29:35
【问题描述】:

我可能想在这里一次提出太多要求,但任何能帮助我哪怕一点点的圣洁、耐心、Android 大师都可能会挽救我的项目免于失败。

我正在开发一个装扮娃娃应用程序,用户可以在其中将物品从“服装库存”视图拖到另一个视图中的“娃娃”。

首先,我不知道如何将娃娃和物品栏放在不同的视图中,并且仍然能够在两者之间拖动位图。 (位图的绘制和拖动本身不是问题)。

到目前为止,我所能做的就是使用一个扩展的 SurfaceView,我以编程方式将其添加到我的布局中,并使用一个线程绘制到它的 Canvas。在 Canvas 的一侧,我绘制了构成娃娃的 bitmpas。另一方面,我绘制构成库存的位图。它工作得很好,但我需要能够将它分成两个视图,因为 a) 娃娃视图的高度需要大于库存视图的高度,并且 b) 我想重新使用在不同的活动中,在别处有两种观点。

所以,最后,我的问题是:如何在两个表面视图的画布之间共享位图?或者,我可以有一个 Canvas 覆盖整个屏幕,并在其顶部放置其他视图(按钮 TextView 等)?或者,有没有其他更合乎逻辑的方式来做我正在尝试的事情?如果我有两个 SurfaceView,每个都有一个 Canvas 由一个线程控制,我将如何处理我正在拖动的位图可能同时部分“在”两个 Canvas 区域“内部”的事实? (我真的只想要一个屏幕大小的 Canvas,不是吗?)

按照下面的布局 XML,程序添加的 SurfaceView(我们称之为 DollView)被添加到一个空的、嵌套的 RelativeLayout 中。我继续通过将按钮添加到布局 XML 中来不断地捏造东西,不知何故,这些按钮确实出现在 DollView 的顶部,并且不受 DollView 画布的持续绘制和重新绘制的影响。当我拖动服装项目或玩偶的位图时,它会在这些按钮下方...并且如果我将位图拖动到 DollView 的父 RelativeLayout 的指定宽度之外,我仍然可以“保持”它,并把它带回视野,但是,它没有被绘制到屏幕上。完全被它难住了,尽管这是我现在最不重要的问题。

我可以向任何对这种疯狂行为感兴趣的人展示他们想看的代码。谢谢你陪我。我希望阅读它不会像试图解释它伤害了我一样伤害你的头! :)

这是活动的布局,在 OnCreate 中设置为内容视图:

<?xml version="1.0" encoding="utf-8"?>

 <TextView android:id="@+id/header_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:text="Header text..." />

<!-- The extended SufaceView (DollView) is programatically added to this RelativeLayout -->
<RelativeLayout android:id="@+id/doll_layout"
    android:layout_width="350dip"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header_text"
    android:layout_alignParentLeft="true" />

<TextView android:id="@+id/stats" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_below="@+id/header_text"
    android:layout_toRightOf="@+id/doll_layout"
    android:layout_alignParentRight="true"
    android:text="Stats text..." />     

<TableLayout android="@+id/stats_table"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/stats"
    android:layout_toRightOf="@+id/doll_layout"
    android:layout_alignParentRight="true">

<TableRow>
<TextView android:id="@+id/stat_one" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="stat one..." />

<TextView android:id="@+id/stat_one_score" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="0" />

<TextView android:id="@+id/stat_one_mod" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text=" " />

</TableRow>
<TableRow>
<TextView android:id="@+id/stat_two" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="stat two..." />

<TextView android:id="@+id/stat_two_score" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="0" /> 

<TextView android:id="@+id/stat_two_mod" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text=" " />
</TableRow>

<TableRow>
<Button android:id="@+id/stats_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="stats" />

<Button android:id="@+id/info_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="info" />

<Button android:id="@+id/help_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="help" />
</TableRow>
</TableLayout>

<!-- A "dragged" bitmap from the DollView passes underneath this TextView.  Why? -->
<TextView android:id="@+id/doll_name"
    android:textSize="16sp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header_text"
    android:layout_alignParentLeft="true"
    android:text="" />

<!-- dragged bitmaps pass under all of these buttons too, apart from the last three
     nav buttons, as they're outside the DollView's width. 
     How would I make the dragged bitmaps pass OVER these objects, instead? --> 
<Button android:id="@+id/nav_btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/nav_btn2"
    android:text="nav1" />

<Button android:id="@+id/next_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/nav_btn1"
    android:layout_centerHorizontal="true"
    android:text="Next" />

<Button android:id="@+id/prev_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/nav_btn1"        
    android:layout_toLeftOf="@+id/next_btn"
    android:text="Previous" />

<Button android:id="@+id/nav_btn2" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/nav_btn3"
    android:text="nav2" />

<Button android:id="@+id/nav_btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/nav_btn4"
    android:text="nav3" />      

<Button android:id="@+id/nav_btn4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/nav_btn5"
    android:text="nav4" />

<Button android:id="@+id/nav_btn5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/nav_btn6"
    android:text="nav5" />

<Button android:id="@+id/nav_btn6" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="nav6" />

【问题讨论】:

  • 我能做些什么来让这个问题更清楚吗?我真的很想在这里寻求帮助:)

标签: android layout canvas surfaceview


【解决方案1】:

最好的方法是将你的surfaceview 添加为RelativeLayout 的子级。那么另一个视图将是RelativeView 的另一个子视图。这样画布的绘制就不会受到干扰。

http://groups.google.com/group/android-developers/browse_thread/thread/b3917e04d47fb27f?pli=1

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    layout = new RelativeLayout(this);
    RocketView rocketView;
    rocketView = new RocketView(this);
    layout.addView(rocketView);
    setContentView(layout);
}

【讨论】:

    【解决方案2】:

    您可以尝试使用FrameLayout 将您的按钮叠加到您的 SurfaceView 上。不确定您的点击是否会发送到正确的视图。

    Romain Guy has an example 使用 FrameLayout 在图像上覆盖文本。

    【讨论】:

      【解决方案3】:

      对于 2D 游戏,我强烈建议使用 AndEngine。它是免费使用的,当您准备在市场上销售游戏时无需担心。该项目是开源的,但只需使用他们编译的库即可快速开始使用它。

      对于展示演示的视频: http://www.andengine.org/blog/

      对于他们的支持论坛: http://www.andengine.org/forums/

      andEngine 入门: http://www.andengine.org/forums/tutorials/getting-started-with-andengine-t11.html

      最初使用它对您来说会有些困难,但是一旦您了解了基础知识,使用起来就会非常棒。

      【讨论】:

      • 嗯,谢谢,我一定会仔细阅读的。然而,这是我自己非常想做和理解的事情(除了这个问题的呼救声):) 此外,我的应用程序中没有足够的“游戏”需要引擎。只有一个屏幕,您可以在其中移动图像。不过,很感激。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多