移动绘制形状定制的RelativeLayout

我onTouchevent识别正确的我已经点击其中绘制形状。 我可以设置x和y参数这一点。 但在onTouchEvent结束时,我调用无效(),但的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); } }

--------------解决方案-------------

我THIK你需要添加: this.setWillNotDraw(false);在DrawView构造

  1. 检查您设置this.setWillNotDraw(假);
  2. 请确保您设置RelativeLayout的XML作为
  3. 如果他们不工作,尝试派生平局(画布)或dispatchDraw(画布)
分类:安卓 时间:2012-04-18 人气:0
本文关键词: 安卓
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.594 (s). 9 q(s)