Hello,
at the moment I'm not that much familiar with Android and I try to
teach it myself. My target is it to control a graphic with android
buttons, so at first I designed the layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:stretchColumns="*"
android:layout_alignParentBottom="true">
<TableRow>
<Button
android:id="@+id/btnUp"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="Up"
/>
<Button
android:id="@+id/btnEnter"
android:layout_width="70px"
android:layout_height="wrap_content"
android:text="Enter"
/>
<Button
android:id="@+id/btnDown"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="Down"
/>
</TableRow>
</TableLayout>
</FrameLayout>
This seems to be what I want. Then I created a view and a thread:
--------------------------------------------
package my.domain.test;
public class GameThread extends Thread {
private SurfaceHolder surfaceHolder;
private GamePanelView gamePanel;
private boolean run = false;
public GameThread(SurfaceHolder surfaceHolder, GamePanelView
panel) {
this.surfaceHolder = surfaceHolder;
this.gamePanel = panel;
}
public void setRunning(boolean run) {
this.run = run;
}
@Override
public void run() {
Canvas c;
while (run) {
c = null;
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
gamePanel.updatePosition();
gamePanel.onDraw(c);
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
-----------------------------
package my.domain.test;
public class GamePanelView extends SurfaceView implements Callback {
private GameThread thread;
private Bitmap ball;
private int posX = 10, posY = 10;
public GamePanelView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
thread = new GameThread(getHolder(), this);
createImages();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int
width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
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) {
//
}
}
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(ball, posX, posY, null);
}
public GameThread getThread() {
return thread;
}
private void createImages() {
ball = BitmapFactory.decodeResource(getResources(),
R.drawable.ball);
}
public void updatePosition() {
//update Position
}
}
---------------------------
Drawing the ball itself works and drawing the buttons itself works,
but I neighter know how to combine that nor I know how to listen to
these buttons (it's probably not onKeyDown, because I used this for
the d-pad). In earlier examples, I just used this xml as my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<my.domain.test
android:id="@+id/game"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
I can additionally define this in my previous xml file. This seems to
work too, no exceptions. But what do I have to do when I want to draw
graphics and those android buttons I defined in the xml file at the
same time? And what is the best approach to control the graphic with
those buttons? I hope that anybody can give my some useful hints.
Best Regards.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
To unsubscribe from this group, send email to
android-developers+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.