Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySurfaceview extends SurfaceView implements SurfaceHolder.Callback {
- private MySurfaceThread thread;
- public MySurfaceview(Context context) {
- this(context,null);
- }
- public MySurfaceview(Context context, AttributeSet attrs) {
- super(context,attrs);
- mContext = context;
- //retreivig bitmaps from resources
- getHolder().addCallback(this);
- thread = new MySurfaceThread(getHolder(),this);
- setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if(event.getAction() == MotionEvent.ACTION_DOWN) {
- .........
- .........
- }
- else if(event.getAction() == MotionEvent.ACTION_MOVE) {
- .......
- ......
- }
- else if(event.getAction() == MotionEvent.ACTION_UP) {
- .......
- ......
- }
- return true;
- }
- });
- }
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawRGB(0, 0, 0);
- // Drawing bitmaps on to the view here
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- thread.setRunning(true);
- thread.start();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- boolean retry = true;
- thread.setRunning(false);
- while (retry) {
- try {
- thread.join();
- retry = false;
- }
- catch (InterruptedException e) {
- }
- }
- }
- public class MySurfaceThread extends Thread {
- private SurfaceHolder myThreadSurfaceHolder;
- private MySurfaceview myThreadSurfaceView;
- private boolean myThreadRun = false;
- public MySurfaceThread(SurfaceHolder surfaceHolder, MySurfaceview surfaceView) {
- myThreadSurfaceHolder = surfaceHolder;
- myThreadSurfaceView = surfaceView;
- }
- public void setRunning(boolean b) {
- myThreadRun = b;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while(myThreadRun){
- // myThreadSurfaceView.re
- Canvas c = null;
- try{
- c = myThreadSurfaceHolder.lockCanvas(null);
- synchronized (myThreadSurfaceHolder){
- // sleep(100);
- myThreadSurfaceView.onDraw(c);
- }
- //
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- 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) {
- myThreadSurfaceHolder.unlockCanvasAndPost(c);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement