开发者

Android - A Layout, Surface View and Canvas Conundrum

I'm probably trying to ask too much at once here, but any saintly, patient, Android guru who can help me even a little bit is probably going to save my project from failure.

I'm working on a dress-up-doll app, where the user can drag items from a "clothing inventory" view over to a "doll", in another view.

Firstly, I can't figure out how to have the doll and the inventory in separate views and still be able to drag bitmaps between the two. (the drawing and dragging of bitmaps is not a problem in itself).

All I've been able to get working so far, is to use one extended SurfaceView, which I add programatically to my layout and use a thread to draw to its Canvas. On one side of the Canvas I draw bitmpas that make up the doll. On the other, I draw bitmaps that make up the inventory. It works fine like that, but I need to be able to split it into two views, as a) the height of the doll view needs to be greater than the height of the inventory view, and b) I want to re-use the two views elsewhere, on their own, in different activities.

So, at last, my question is: How can I share bitmaps between the Canvases of two surface views? Or, can I have a Canvas that covers the whole screen with other views (Buttons TextView, etc) placed on top 开发者_如何学Pythonof it? Or, is there some other entirely more logical way of doing what I'm attempting that has passed me by? If I've got two SurfaceViews, each with a Canvas being controlled by a thread, how would I handle the fact that the bitmap I'm dragging could be partially "inside" both Canvases' areas at once? (I really just want one screen-sized Canvas, don't I?)

The progamatically added SurfaceView, let's call it DollView is added to an otherwise empty, nested RelativeLayout as per the Layout XML below. I've gone on to continually fudge things by adding Buttons to the layout XML that, somehow, DO appear on top of the DollView, and aren't affected by the continual drawing and re-drawing of the DollView's Canvas. When I drag a clothing item or doll piece's bitmap around, it passes underneath these Buttons...and if I drag a bitmap outside of the specified width of the DollView's parent RelativeLayout I can still "keep hold" of it, and bring it back into view, but, it isn't drawn onto the screen. Totally stumped by that, too altough it's the least of my problems right now.

I can show anyone who's interested in this insanity as much code as they'd like to see. Thanks for bearing with me. I hope reading it doesn't hurt your head as much as trying to explain it has hurt mine! :)

Here's the activity's layout, which is set as the content view in the 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" />


the best way is to add your surfaceview as a child of RelativeLayout. then the other view will be the other child of RelativeView. that way the canvas drawing won't be disturbed .

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);
}


You could try using FrameLayout to overlay your buttons onto your SurfaceView. Not sure if your clicks will get sent to the right View.

Romain Guy has an example of overlaying text on an image with a FrameLayout.


for 2D games I strongly recommend using AndEngine. It's free to use and there's nothing to worry about when you are ready to sell the game on the Market. The project is open source, but just use their compiled library to quickly start using it.

for videos showing demo's: http://www.andengine.org/blog/

for their support forum: http://www.andengine.org/forums/

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

It'll be a bit of work for you to initially use it, but once you know the basics it's incredibly amazing to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜