Note that there are some explanatory texts on larger screens.

plurals
  1. POButtons not working with Surface view
    primarykey
    data
    text
    <p>I am new to <code>Android</code> and I am trying to add <em>start</em> and <em>reset</em> buttons with a custom surface view. I am able to draw canvas with a circle which is moving with touch.</p> <p>Now my problem is that when I click the <em>start</em> button, the circle must take its initial position (10,10).</p> <p>My activity class</p> <pre><code>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); // Set full screen view getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); GameView = new GameView(this); btnStart = (Button) findViewById(R.id.btnStart); btnStart.setOnClickListener(this); btnReset = (Button) findViewById(R.id.btnReset); btnReset.setOnClickListener(this); GameView.setOnTouchListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.btnStart: Log.i("openGl", "play called"); GameView.setState(); // drawView.invalidate(); break; case R.id.btnReset: // GameView.play=true; // GameView.reset(); Log.i("openGl", "RESETcalled"); // drawView.invalidate(); break; } } } </code></pre> <p>Custom surfaceview class and thread class</p> <pre><code>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 &amp; 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"); } } } </code></pre> <p>main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;com.example.opengl.GameView android:id="@+id/gameView" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"&gt; &lt;Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" /&gt; &lt;Button android:id="@+id/btnReset" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Reset" /&gt; &lt;/LinearLayout&gt; &lt;/FrameLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload