import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TestSurfaceView(this));
}
class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
byte i = 0; // если i = 0 - красный, = 1 - зелёный, = 2 - синий;
MyThread draw;
public TestSurfaceView(Context context) {
super(context);
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
draw = new MyThread(getHolder(),i);
draw.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
draw.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (i == 0) i = 1;
if (i == 1) i = 2;
if (i == 2) i = 0;
return super.onTouchEvent(event);
}
}
class MyThread extends Thread {
private SurfaceHolder sh;
private byte i;
private Paint paint = new Paint();
MyThread(SurfaceHolder sh1, byte i1){
sh = sh1;
i = i1;
}
@Override
public void run() {
paint.setStyle(Paint.Style.FILL);
Canvas can = sh.lockCanvas(null);
try{
synchronized (sh){
try{
if(i == 0){
paint.setColor(Color.RED);
can.drawPaint(paint);
}
if(i == 1){
paint.setColor(Color.GREEN);
can.drawPaint(paint);
}
if(i == 2){
paint.setColor(Color.BLUE);
can.drawPaint(paint);
}
}catch (Exception e){
if (can != null){
sh.unlockCanvasAndPost(can);
}
}
}
}catch (Exception e){}
}
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TestSurfaceView(this));
}
class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
byte i = 0; // если i = 0 - красный, = 1 - зелёный, = 2 - синий;
MyThread draw;
public TestSurfaceView(Context context) {
super(context);
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
draw = new MyThread(getHolder(),i);
draw.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
draw.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (i == 0) i = 1;
if (i == 1) i = 2;
if (i == 2) i = 0;
return super.onTouchEvent(event);
}
}
class MyThread extends Thread {
private SurfaceHolder sh;
private byte i;
private Paint paint;
MyThread(SurfaceHolder sh1, byte i1){
sh = sh1;
i = i1;
}
@Override
public void run() {
Canvas can = sh.lockCanvas();
try{
synchronized (sh){
try{
if(i == 0){
paint.setColor(Color.RED);
can.drawPaint(paint);
}
if(i == 1){
paint.setColor(Color.GREEN);
can.drawPaint(paint);
}
if(i == 2){
paint.setColor(Color.BLUE);
can.drawPaint(paint);
}
}catch (Exception e){
if (can != null) sh.unlockCanvasAndPost(can);
}
}
}catch (Exception e){}
}
}
}