package Pifagor.android.game; import android.app.Activity; import android.os.Bundle; public class Pifagor extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pifagorView = (PifagorView) findViewById(R.id.game); } }
package Pifagor.android.game; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.view.MotionEvent; import android.view.SurfaceHolder; public class PifagorDraw extends Thread { private SurfaceHolder appScreen; private boolean appRunning; // private boolean appPaused; // private Paint appPaint; // private Bitmap clearScreen; // private PifagorPuzzle Puzzles[]; // private int activePuzzle; // private long thisTime; // private long downTime; // private PifagorTask Tasks[]; // private PifagorTask ResetPiktogram; // private int activeTask; // private int maxTasks; // private boolean prevButton, nextButton, resetButton; // // // private int PuzzleTemplate1[][][] = {......}; // private int PuzzleTemplate2[][][] = {......}; // private int PuzzleTemplate3[][][] = {......}; // private int PuzzleTemplate4[][][] = {......}; // private int PuzzleTemplate5[][][] = {......}; // "" private int Pikto1[][] = {......}; // private int Task1[][] = {......}; private int Task2[][] = {......}; private int Task3[][] = {......}; private int Task4[][] = {......}; // public PifagorDraw(SurfaceHolder surfaceHolder, Context context) { // appScreen = surfaceHolder; appRunning = false; appPaused = false; activePuzzle = 0; thisTime = 0; downTime = 0; prevButton = false; nextButton = false; resetButton = false; // appPaint = new Paint(); appPaint.setColor(Color.BLUE); appPaint.setStrokeWidth(2); appPaint.setStyle(Style.STROKE); // clearScreen = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.RGB_565); // Puzzles = new PifagorPuzzle[7]; Puzzles[0] = new PifagorPuzzle(......); Puzzles[1] = new PifagorPuzzle(......); Puzzles[2] = new PifagorPuzzle(......); Puzzles[3] = new PifagorPuzzle(......); Puzzles[4] = new PifagorPuzzle(......); Puzzles[5] = new PifagorPuzzle(......); Puzzles[6] = new PifagorPuzzle(......); PuzzlesReset(); // maxTasks = 4; activeTask = 0; Tasks = new PifagorTask[maxTasks]; Tasks[0] = new PifagorTask(......); Tasks[1] = new PifagorTask(......); Tasks[2] = new PifagorTask(......); Tasks[3] = new PifagorTask(......); // ResetPiktogram = new PifagorTask(......); } // public void setRunning(boolean status) { appRunning = status; } // public boolean Touch(MotionEvent event) { int mouseX = (int)event.getX(); int mouseY = (int)event.getY(); switch(event.getAction()) { case MotionEvent.ACTION_MOVE: // ...... break; case MotionEvent.ACTION_DOWN: // ...... break; case MotionEvent.ACTION_UP: // ...... break; } return true; // true, } // public void PuzzlesReset() { ...... } @Override public void run() { while(appRunning) { if (!appPaused) { Canvas canvas = null; try { canvas = appScreen.lockCanvas(); synchronized(appScreen) { // // : // canvas.drawBitmap(clearScreen, 0, 0, null); // for(int i=0;i<7;i++) { Puzzles[i].Update(); Puzzles[i].Draw(canvas); } ...... sleep(20); } } catch (Exception e) { } finally { if (canvas != null) { appScreen.unlockCanvasAndPost(canvas); } } } } } }
package Pifagor.android.game; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; public class PifagorView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder appScreen; private PifagorDraw appThread; public boolean surfaceCreated; // public PifagorView(Context context, AttributeSet attrs) { super(context, attrs); surfaceCreated = false; appScreen = getHolder(); appScreen.addCallback(this); appThread = new PifagorDraw(appScreen, context); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { appThread = new PifagorDraw(holder, getContext(), screenWidth, screenHeight); appThread.setRunning(true); appThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { boolean retry = true; appThread.setRunning(false); while(retry) { try { appThread.join(); retry = false; } catch (InterruptedException e){ } } } @Override public boolean onTouchEvent(MotionEvent event) { return appThread.Touch(event); // PifagorDraw, } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Pifagor.android.game.PifagorView android:id="@+id/game" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
public class PifagorView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder appScreen; private PifagorDraw appThread; public boolean surfaceCreated; private int screenWidth; private int screenHeight; // public PifagorView(Context context, AttributeSet attrs) { super(context, attrs); surfaceCreated = false; appScreen = getHolder(); appScreen.addCallback(this); appThread = new PifagorDraw(appScreen, context, screenWidth, screenHeight); setFocusable(true); } // public void terminateThread() { boolean retry = true; appThread.setRunning(false); while(retry) { try { appThread.join(); retry = false; } catch (InterruptedException e){ } } } // public void createThread(SurfaceHolder holder) { appThread = new PifagorDraw(holder, getContext()); appThread.setRunning(true); appThread.start(); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { if (surfaceCreated == false) { createThread(holder); surfaceCreated = true; } } @Override public void surfaceDestroyed(SurfaceHolder arg0) { terminateThread(); } @Override public boolean onTouchEvent(MotionEvent event) { return appThread.Touch(event); } }
public class Pifagor extends Activity { /** Called when the activity is first created. */ private PifagorView pifagorView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pifagorView = (PifagorView) findViewById(R.id.game); } @Override public void onPause() { super.onPause(); pifagorView.terminateThread(); } @Override public void onResume() { super.onResume(); if (pifagorView.surfaceCreated == true) { pifagorView.createThread(pifagorView.getHolder()); } } }
Source: https://habr.com/ru/post/114059/
All Articles