surfaceview setbackgrounddrawable()


Last Updated:

  1. mrqs

    mrqs Well-Known Member This Topic's Starter

    Joined:
    Sep 28, 2009
    Messages:
    1,446
    Likes Received:
    92
    Code (Text):
    1.  
    2. public class V extends SurfaceView implements SurfaceHolder.Callback {
    3.     private CThread mThread; // see next code block for class
    4.     public int x;
    5.     public int y;
    6.  
    7.     public V(Context context) {
    8.         super(context);
    9.         SurfaceHolder holder = getHolder();
    10.         holder.addCallback(this);
    11.         mThread = new CThread(holder,this);
    12.         x = 0;
    13.         y = 0;
    14.         setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
    15.     }
    16.  
    17.     @Override
    18.     public void onDraw(Canvas canvas)
    19.     {
    20.         Paint paint = new Paint();
    21.         paint.setColor(Color.WHITE);
    22.         canvas.drawLine(x, y, getWidth(), getHeight(), paint);
    23.     }
    24.  
    25.     public boolean onTouchEvent(MotionEvent event) {
    26.         switch (event.getAction()) {
    27.                 case MotionEvent.ACTION_MOVE:
    28.                 x = (int) event.getX();
    29.                 y = (int) event.getY();
    30.                 default: break;
    31.         }
    32.         return true;
    33.     }
    34.     public void surfaceCreated(SurfaceHolder holder) {
    35.         mThread.setRunning(true);
    36.         mThread.start();
    37.     }
    38. /* sufraceDestroyed etc */
    39. }
    40.  
    Code (Text):
    1.  
    2. public class CThread extends Thread {
    3.  
    4.     private SurfaceHolder mSurfaceHolder;
    5.     private V mView;
    6.     private boolean mRun = false;
    7.    
    8.     public CThread(SurfaceHolder surfaceHolder, V view) {
    9.         mSurfaceHolder = surfaceHolder;
    10.         mView = view;
    11.     }
    12.  
    13.     public void setRunning(boolean run) {
    14.         mRun = run;
    15.     }
    16.  
    17.     @Override
    18.     public void run() {
    19.         Canvas c;
    20.         while (mRun) {
    21.             c = null;
    22.             try {
    23.                 c = mSurfaceHolder.lockCanvas(null);
    24.                 synchronized (mSurfaceHolder) {
    25.                     mView.onDraw(c);
    26.                 }
    27.             } finally {
    28.                 if (c != null) {
    29.                     mSurfaceHolder.unlockCanvasAndPost(c);
    30.                 }
    31.             }
    32.         }
    33.     }
    34.  
    if i leave the setBackgroundDrawable() call out of the V constructor, i get black background with a line drawn from the lower right corner to the top left corner; and where ever i move the "cursor" afterwards, but when i have that method call, i only get the initial x=y=0 line drawn on top of the background image and moving the "cursor" doesn't do anything

    what's up with that?
     

    Advertisement
  2. mrqs

    mrqs Well-Known Member This Topic's Starter

    Joined:
    Sep 28, 2009
    Messages:
    1,446
    Likes Received:
    92
    anyone?
     

Share This Page

Loading...