문제

I am trying an example having SurfaceView. I have inherited my class from SurfaceView and using as follows:

public class MySurfaceView extends SurfaceView  implements  SurfaceHolder.Callback {

    Context context;
    MySurfaceViewThread mThread;
    SurfaceHolder holder;
    Paint paint;

    int x = 20, y = 20, r = 10;

    public void init() {
        holder = getHolder();
        holder.addCallback(this);
        mThread = new MySurfaceViewThread(getHolder(), this);

        paint = new Paint();
        paint.setStyle(Style.STROKE);
        paint.setStrokeCap(Cap.ROUND);
        paint.setStrokeWidth(1);
        paint.setColor(Color.rgb(255, 255, 255));
    }

    public MySurfaceView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
        init();

    }

    public MySurfaceView(Context context, AttributeSet attr) {
        super(context,attr);
        this.context = context;
        init();
    }

    public MySurfaceView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
        this.context = context;
        init();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub


    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        mThread.isRunning = false;
        while (true) {
            try {
                mThread.join();
                break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void onDraw(Canvas canvas) {
//      super.onDraw(canvas);
//      canvas.drawColor(0, Mode.CLEAR);
        x += 2;
        y += 2;
        r += 3;
        canvas.drawColor(Color.rgb(x%255, y%255, (x+y)%255));
        canvas.drawCircle(x, y, r, paint);
        canvas.drawText("x:"+x, 100, 100, paint);
        Log.d("onDraw","onDraw()"+"x:"+x + ",y:"+y+",r:"+r);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.d("surfaceCreated", "surfaceCreated()");
        mThread.isRunning = true;
        mThread.start();
    }

}

I am updating my Canvas from a Thread which is:

public class MySurfaceViewThread extends Thread {

    SurfaceHolder holder;
    MySurfaceView surfaceView;
    boolean isRunning = false;

    public MySurfaceViewThread(SurfaceHolder holder, MySurfaceView surfaceView) {
        Log.d("thread","thread constructor");
        this.holder = holder;
        this.surfaceView = surfaceView;
    }

    @Override
    public void run() {
        Log.d("run","run()");
        while(isRunning) {

            Canvas canvas = null;
            try {
                canvas = holder.lockCanvas();

                synchronized(holder) {
                    surfaceView.onDraw(canvas);
                }
                sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                holder.unlockCanvasAndPost(canvas);
                Log.d("canvas status", "canvas unlocaked...");
            }
        }
    }

}

When application starts and its onDraw() is called, it draws circle and text as well but on further calls to onDraw() it draws nothing. Means nothing changed on screen after first update. Any idea where I am getting wrong? I am new to android and slow learner as well.

도움이 되었습니까?

해결책

I got the answer. Its very strange. I don't know why this worked. I commented the following line from my activity and it runs.

surfaceView.setBackgroundColor(Color.rgb(0, 255, 0));

the activity code is:

public class SurfaceViewTutorialActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MySurfaceView surfaceView = new MySurfaceView(this);
//        surfaceView.setBackgroundColor(Color.rgb(0, 255, 0));
        surfaceView.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        setContentView(surfaceView);
    }
}

If any one knows about this, kindly guide me to the right direction.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top