在自定义relativeLayout中移动可绘制的形状

时间:2011-12-25 11:00:54

标签: android

我的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);
    }

}

2 个答案:

答案 0 :(得分:0)

我认为您需要在DrawView构造函数中添加this.setWillNotDraw(false);

答案 1 :(得分:0)

  1. 检查你设置this.setWillNotDraw(false);
  2. 确保将relativeLayout xml设置为
  3. 如果它们不起作用,请尝试派生绘图(Canvas)或dispatchDraw(Canvas)