2011-12-25 43 views
0

我的onTouchevent可以識別出我點擊了哪個可繪製的形狀。 我也可以爲它設置x和y參數。 但在onTouchEvent結束時,我調用invalidate(),但從不調用onDraw方法。在自定義的相對佈局中移動可繪製的形狀

這是爲什麼!?

public class DrawView extends RelativeLayout { 
    private ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>(); 
    private int rectId = 0; 
    private int width; 
    private int height; 

    public DrawView(Context context) { 
     super(context);  

     this.setLayoutParams(new RelativeLayout.LayoutParams(width, height));  

     Rectangle rect1 = new Rectangle(this.getContext()); 
     rect1.setId(1); 
     rectangles.add(rect1); 
     this.addView(rect1); 

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 10, 0, 0); 
     Rectangle rect2 = new Rectangle(this.getContext()); 
     rect2.setId(2); 
     params.addRule(RelativeLayout.BELOW, rect1.getId());  
     rect2.setLayoutParams(params); 
     rectangles.add(rect2); 
     this.addView(rect2); 


    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     Log.i("ondraw","yes"+rectangles.size()); 

     for(Rectangle v: rectangles){ 
      Paint paint = new Paint(); 
      paint.setColor(Color.GREEN); 
      Log.i("draw", ""+v.getTop()); 
      Log.i("draw", ""+v.getLeft()); 
      canvas.drawRect((float)v.getX(),(float) v.getY(),(float) v.getX()+150, (float)v.getY()+50, paint); 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     int x = (int) event.getX(); 
     int y = (int) event.getY(); 

     switch(event.getAction()){ 
      case MotionEvent.ACTION_DOWN: 

       rectId = 0; 

       for(int i=0; i < rectangles.size(); i++){ 
        View child = rectangles.get(i); 
        if(x > child.getLeft() && x < child.getLeft()+150 && y > child.getTop() && y < child.getTop()+50){ 
         rectId = child.getId(); 
         rectangles.get(rectId-1).setTop(child.getTop()); 
         rectangles.get(rectId-1).setLeft(child.getLeft()); 
         break; 
        }    
       } 
       break; 
      case MotionEvent.ACTION_MOVE: 

       if(rectId > 0){ 
        rectangles.get(rectId-1).setBackgroundRessource(true); 
        rectangles.get(rectId-1).setTop(y-25); 
        rectangles.get(rectId-1).setLeft(x-25); 
        Log.i("MOVE X", rectId+" "+rectangles.get(rectId-1).getX()); 
        Log.i("MOVE Y", rectId+" "+rectangles.get(rectId-1).getY()); 
       } 

       break; 
      case MotionEvent.ACTION_UP: 
       Log.i("rectid Cancel", ""+rectId); 
       if(rectId > 0){ 
        rectangles.get(rectId-1).setBackgroundRessource(true); 
       } 

       break; 
     } 

     invalidate(); 
     return true; 

    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     width = View.MeasureSpec.getSize(widthMeasureSpec); 
     height = View.MeasureSpec.getSize(heightMeasureSpec); 

     setMeasuredDimension(width, height); 
    } 

} 

回答

0

我THIK您需要添加:this.setWillNotDraw(false);在drawView函數構造

0
  1. 檢查設置this.setWillNotDraw(假);
  2. 確保您設置的RelativeLayout XML作爲
  3. 如果他們不工作,嘗試獲得平局(畫布)或dispatchDraw(畫布)