I'm new to Android , and I'm trying to add start and reset buttons with a custom surface view. I can draw a canvas with a circle that moves with a touch.
Now my problem is that when I press the "Start" button, the circle should take its initial position (10.10).
My activity class
public class OpenGlActivity extends Activity implements OnClickListener { GameView GameView; FrameLayout Frame; LinearLayout canvas; Button btnStart, btnReset; TutorialThread GameThread; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Custom surface class class and stream class
class GameView extends SurfaceView implements SurfaceHolder.Callback { String TAG = "GameView"; private TutorialThread _thread; Paint paint = new Paint(); Paint red = new Paint(); Paint black = new Paint(); int x = 20; int y = 20; public GameView(Context context) { super(context); // TODO Auto-generated constructor stub getHolder().addCallback(this); _thread = new TutorialThread(getHolder(), this); // TODO Auto-generated constructor stub paint.setColor(Color.WHITE); paint.setAntiAlias(true); red.setColor(Color.RED); red.setAntiAlias(true); black.setColor(Color.BLACK); black.setAntiAlias(true); setFocusable(true); } public GameView(Context context, AttributeSet attrs) { super(context, attrs); getHolder().addCallback(this); _thread = new TutorialThread(getHolder(), this); // TODO Auto-generated constructor stub paint.setColor(Color.WHITE); paint.setAntiAlias(true); red.setColor(Color.RED); red.setAntiAlias(true); black.setColor(Color.BLACK); black.setAntiAlias(true); setFocusable(true); } @Override public boolean onTouchEvent(MotionEvent event) { x = (int) event.getX(); y = (int) event.getY(); return true; } public void setState() { Log.i(TAG, "in setState"); _thread.play(); } @Override public void onDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); canvas.drawCircle(x, y, 10, red); } @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) { // simply copied from sample application LunarLander: // we have to tell thread to shut down & wait for it to finish, or else // it might touch the Surface after we return and explode boolean retry = true; _thread.setRunning(false); while (retry) { try { _thread.join(); retry = false; } catch (InterruptedException e) { // we will try it again and again... } } } } class TutorialThread extends Thread { String TAG = "TutorialThread"; private SurfaceHolder _surfaceHolder; private GameView _panel; private boolean _run = false; public TutorialThread(SurfaceHolder surfaceHolder, GameView panel) { _surfaceHolder = surfaceHolder; _panel = panel; } public void setRunning(boolean run) { _run = run; } @Override public void run() { Canvas c; while (_run) { c = null; try { c = _surfaceHolder.lockCanvas(null); synchronized (_surfaceHolder) { _panel.onDraw(c); } } finally { // do this in a finally so that if an exception is thrown // during the above, we don't leave the Surface in an // inconsistent state if (c != null) { _surfaceHolder.unlockCanvasAndPost(c); } } } } public void play() { synchronized (_surfaceHolder) { _panel.x = 10; _panel.y = 10; Log.i(TAG, "in Play"); } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.opengl.GameView android:id="@+id/gameView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" /> <Button android:id="@+id/btnReset" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Reset" /> </LinearLayout> </FrameLayout>
source share