I need to find out the screen resolution and divide it in half to draw a square.
Implemented as follows:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new DrawView(this)); } class DrawView extends SurfaceView implements SurfaceHolder.Callback { private DrawThread drawThread; Paint p; Rect rect; public DrawView(Context context) { super(context); getHolder().addCallback(this); p = new Paint(); rect = new Rect(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { drawThread = new DrawThread(getHolder()); drawThread.setRunning(true); drawThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; drawThread.setRunning(false); while (retry) { try { drawThread.join(); retry = false; } catch (InterruptedException e) { } } } class DrawThread extends Thread { private boolean running = false; private SurfaceHolder surfaceHolder; private int height; private int width; public DrawThread(SurfaceHolder surfaceHolder) { this.surfaceHolder = surfaceHolder; } public void setRunning(boolean running) { this.running = running; } @SuppressWarnings("deprecation") public void MyScreenSize() { Point size = new Point(); WindowManager w = getWindowManager(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { w.getDefaultDisplay().getSize(size); width = size.x; height = size.y; } else { Display d = w.getDefaultDisplay(); width = d.getWidth(); height = d.getHeight(); } } public void run() { Canvas canvas; while (running) { canvas = null; try { canvas = surfaceHolder.lockCanvas(null); if (canvas == null) continue; canvas.drawColor(Color.GREEN); // настройка кисти // красный цвет p.setColor(Color.WHITE); // толщина линии = 10 p.setStrokeWidth(10); // рисуем точку (50,50) canvas.drawPoint(50, 50, p); // рисуем линию от (100,100) до (500,50) canvas.drawLine(100,100,500,50,p); // рисуем круг с центром в (100,200), радиус = 50 canvas.drawCircle(100, 200, 50, p); // рисуем прямоугольник // левая верхняя точка (200,150), нижняя правая (400,200) canvas.drawRect(height, 150, width, 200, p); // настройка объекта Rect // левая верхняя точка (250,300), нижняя правая (350,500) rect.set(250, 300, 350, 500); // рисуем прямоугольник из объекта rect canvas.drawRect(rect, p); } finally { if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } } } } } }
When changing specific coordinates of points (as was the case in the textbook), I get nothing from the data of this method, namely, the square is not drawn at all. Eclipse does not detect errors. It seems to me that the error is in this method.