2012-10-22 81 views
2

我有這個類: 我的問題:畫一條線

public class Example extends Activity implements OnClickListener{ 
    DrawView view1; 
    Canvas canvas = new Canvas(); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     view1 = new DrawView(this); 
     setContentView(view1); 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
    int x = (int) event.getX(); 
    int y = (int) event.getY(); 

    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      view1.setFirstCoord(x, y); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      break; 
     case MotionEvent.ACTION_UP: 
      view1.setSecondCoord(x, y); 
      view1.dispatchDraw(canvas); 
      break; 
     } 
    return true; 
    } 

    public class DrawView extends LinearLayout { 
     private Paint paint = new Paint(); 
     public int x1, x2; 
     public int y1, y2; 

     public DrawView(Context c){ 
      super(c);   
      LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      addView(inflater.inflate(R.layout.union, null)); 

      x1 = 0; 
      x2 = 0; 
      y1 = 0; 
      y2 = 0; 

      paint.setColor(Color.BLACK); 
      paint.setAntiAlias(true); 
      paint.setDither(true); 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeJoin(Paint.Join.ROUND); 
      paint.setStrokeWidth(10);   
     } 

     @Override 
     public void dispatchDraw(Canvas canvas) { 
      super.dispatchDraw(canvas); 
      canvas.drawLine(0, 50, 900,2000 , paint);//WORKS 
      canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);//NO WORKS 
     } 

     public void setFirstCoord(int e, int f){ 
      x1 = e; 
      y1 = f; 
     } 

     public void setSecondCoord(int e, int f){ 
      x2 = e; 
      y2 = f; 
     } 
    } 
} 

當下一行執行我看到在screem的「榜樣」行:

canvas.drawLine(0, 50, 900,2000 , paint); 

但是,當下面一行「執行」不繪製任何直線。爲什麼???

canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint); 

我也試圖與:

canvas.drawLine(x1, y1, x2, y2, paint); 

但是,很明顯,結果都是一樣的。

我也試圖與執行的onDraw()方法,但任何線畫,因爲onDraw有下德「UI」畫

我希望找到人誰可以幫我(佈局.xml文件的內容下) 。我認爲解決方案可以很容易,但我會瘋狂嘗試,而不會找到解決方案。

謝謝!

解決辦法:

public class Example extends Activity implements OnClickListener{ 
    DrawView view1; 
    Canvas canvas = new Canvas(); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     view1 = new DrawView(this); 
     setContentView(view1); 
     view1.setOnClickListener(this); 
    } 

    public class DrawView extends LinearLayout { 
     private Paint paint = new Paint(); 
     public int x1, x2; 
     public int y1, y2; 

     public DrawView(Context c){ 
      super(c);   
      LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      addView(inflater.inflate(R.layout.union, null)); 

      x1 = 0; 
      x2 = 0; 
      y1 = 0; 
      y2 = 0; 

      paint.setColor(Color.BLACK); 
      paint.setAntiAlias(true); 
      paint.setDither(true); 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeJoin(Paint.Join.ROUND); 
      paint.setStrokeWidth(10);   
     } 

     public boolean onTouchEvent(MotionEvent event) { 
     int x = (int) event.getX(); 
     int y = (int) event.getY(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      view1.setFirstCoord(x, y); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      break; 
     case MotionEvent.ACTION_UP: 
      view1.setSecondCoord(x, y); 
      view1.onDraw(canvas); 
      view1.postInvalidate(); 
      break; 
     } 
     return true; 
     } 

     public void setFirstCoord(int e, int f){ 
      x1 = e; 
      y1 = f; 
     } 

     public void setSecondCoord(int e, int f){ 
      x2 = e; 
      y2 = f; 
     } 

     public void onDraw(Canvas canvas){ 
     canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint); 
    } 
    } 
} 

再次感謝您的幫助和關注。我的榮幸!!!

+0

我注意到你正在實現一個OnClickListener,明白任何OnClickListener都會消耗一個觸摸事件來阻止它到達你的Activity的'onTouchEvent()'方法。你是否驗證過'onTouchEvent()'被調用? – Sam

回答

1

而不是把你的繪圖代碼放在調度繪製,嘗試把它放在onDraw。

然後修改onTouch方法是這樣的:

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int x = (int) event.getX(); 
    int y = (int) event.getY(); 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
    view1.setFirstCoord(x, y); 
    break; 
    case MotionEvent.ACTION_MOVE: 
    break; 
    case MotionEvent.ACTION_UP: 
    view1.setSecondCoord(x, y); 
    view1.postInvalidate(); 
    break; 
    } 
    return true; 
} 
1

如果你要使用這個OnClickListener接口,你需要設置你的活動作爲一個OnClickListener的觀點:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    view1 = new DrawView(this); 
    setContentView(view1); 

    // Add this: 
    view1.setOnClickListener(this); 
} 

否則,onTouchEvent將永遠不會被調用。

另外,onTouchEvent應該被命名爲onClick

+0

當我繪製一條線時,該線被繪製在Y軸上位移。 X座標是完美的。我想,它沒有解釋或意義。我必須減去一定量的Y座標。這可能是由於佈局?因爲我混合了LinearLayout和RelativeLayout。謝謝 – Adrian

1
  • 移動的onTouchEvent到drawView函數,而不是活動
  • 讓drawView函數擴展視圖,而不是LinearLayout中
  • 相反dispatchDraw的,實施方法onDraw和onTouchEvent,調用invalidate()
+1

如果你的答案只是「做X而不是Y」,如果你解釋你的斷言背後的原因,這將有所幫助。如果OP想要使用線性佈局,而不僅僅是簡單的視圖呢? – Tim

+0

如果我使DrawView擴展視圖,而不是LinearLayout,我不知道要加載我的佈局file.xml。但別擔心;)在你的幫助和其他朋友的幫助下,我解決了我的問題。我現在寫解決方案。謝謝!!! – Adrian

+0

當我繪製一條線時,該線被繪製爲在Y軸上位移。 X座標是完美的。我想,它沒有解釋或意義。我必須減去一定量的Y座標。這可能是由於佈局?因爲我混合了LinearLayout和RelativeLayout。謝謝 – Adrian

0

我認爲你的問題可能是b e您在活動中檢索到的x/y座標位於屏幕空間中,而畫布上的繪製座標位於畫布空間中。

所以您的活動中的(0,0)與您的畫布的(0,0)不是同一個位置。

您應該可以在視圖內而不是在活動中接收touchevent,那麼座標系對於touchevent和canvas繪製都是相同的。