2011-12-25 10 views
0

私のonToucheventは、クリックした描画可能なシェイプを正しく認識します。 そして、xとyのパラメータも設定できます。 しかし、onTouchEventの最後では、私はinvalidate()を呼び出しますが、onDrawメソッドは呼び出されません。カスタムrelativeLayoutで描画可能なシェイプを移動

これはなぜですか?

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:DrawViewコンストラクタでthis.setWillNotDraw(false);

0
  1. あなたはthis.setWillNotDraw(false)を設定し確認してください。
  2. それらの非が動作している場合
  3. 、ドロー(キャンバス)またはdispatchDraw(キャンバス)
を導き出してみて、あなたがrelativeLayout XMLを設定していることを確認します
関連する問題