我可能想在这里一次提出太多要求,但是任何可以帮助我一点点的圣洁、耐心、Android 大师都可能会让我的项目免于失败。
我正在开发一个装扮娃娃应用程序,用户可以在其中将项目从“服装库存”视图拖到另一个视图中的“娃娃”。
首先,我不知道如何将娃娃和库存放在不同的视图中,并且仍然能够在两者之间拖动位图。(位图的绘制和拖动本身不是问题)。
到目前为止,我所能做的就是使用一个扩展的 SurfaceView,我以编程方式将其添加到我的布局中,并使用一个线程绘制到它的 Canvas。在 Canvas 的一侧,我绘制了构成娃娃的 bitmpas。另一方面,我绘制构成库存的位图。它像那样工作得很好,但我需要能够将它分成两个视图,因为 a) 娃娃视图的高度需要大于库存视图的高度,并且 b) 我想重新使用在别处,在不同的活动中,有两种观点。
所以,最后,我的问题是:如何在两个表面视图的画布之间共享位图?或者,我可以有一个 Canvas 覆盖整个屏幕,并在其顶部放置其他视图(按钮 TextView 等)?或者,有没有其他更合乎逻辑的方式来做我正在尝试的事情?如果我有两个 SurfaceView,每个都有一个 Canvas 由一个线程控制,我将如何处理我正在拖动的位图可能同时部分“在”两个 Canvas 区域“内部”的事实?(我真的只想要一个屏幕大小的 Canvas,不是吗?)
按照下面的 Layout 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" />