title: Custom View In Android using SurfaceView
body: <p>I'm trying to create custom view which fill and empty by certain color but it is not working. I'm new to creating custom views programmatically, so if there may be some "common sense" mistakes.</p>
  
  <p>code: </p>
  
  <p>public class PitchView extends SurfaceView implements SurfaceHolder.Callback {</p>
  
  <pre><code>private int width, height;
  private final Paint paint = new Paint();
  private int cx;
  private int cy;
  
  public PitchView(Context context) {
      super(context);
  }
  
  public PitchView(Context context, AttributeSet attrs) {
      super(context, attrs);
  }
  
  public PitchView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
  }
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      // TODO Auto-generated method stub
      width = MeasureSpec.getSize(widthMeasureSpec);
      height = MeasureSpec.getSize(heightMeasureSpec);
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      cx = 0;
      cy = 0;
  }
  
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      super.onSizeChanged(w, h, oldw, oldh);
      width = w;
      height = h;
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
      paint.setStrokeWidth((float) width);
      paint.setColor(Color.WHITE);
      canvas.drawLine(0, 0, width, height, paint);
      paint.setColor(Color.BLUE);
      canvas.drawLine(0, 0, cx, cy, paint);
  }
  
  Thread t = new Thread(new Runnable() {
  
      private boolean moveUp = true;
  
      @Override
      public void run() {
          // TODO Auto-generated method stub
          try {
              while (true) {
                  if (cx &gt;= width) {
                      moveUp = false;
                  } else if (cx &lt;= 0) {
                      moveUp = true;
                  }
                  if (moveUp)
                      cy++;
                  else
                      cy--;
                  Thread.sleep(500);
                  postInvalidate();
              }
          } catch (Exception e) {
              // TODO Auto-generated catch block
              Log.e("PitchView Error", e.getMessage(), e);
          }
      }
  });
  
  @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
      t.start();
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      // TODO Auto-generated method stub
      t.interrupt();
  }
  </code></pre>
  
  <p>}</p>