简体   繁体   中英

move drawable shapes in custom relativeLayout

My onTouchevent recognizes correct which drawable shape i have clicked. And i can set the x and y parameters for it too. But at the end of the onTouchEvent i call invalidate() but the onDraw method is never called.

Why is this!?

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

}
  1. Check you set this.setWillNotDraw(false);
  2. Make sure you set relativeLayout xml as
  3. If non of them work, try derive draw(Canvas) or dispatchDraw(Canvas)

I thik you need to add: this.setWillNotDraw(false); in DrawView constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM