我无法弄清楚如何获得自定义视图的动态集合,以在画布中显示为可移动的矩形。帆布是一个带酒吧凳的酒吧,我希望餐厅经理能够创建其他页面可以使用的他/她的酒吧布局。
我有它工作,但不是很好,个人“BarSpots”将不会移动,ViewBarSpot的onTouchEvent不会触发 - 但是 - ViewBarCanvas的onTouchEvent正在触发。我错过了什么?
ActivityBarLayout:
private FrameLayout flCanvas;
List<BarSpot> dbBarSpots;
...
private void LoadBar(List<BarSpot> barSpots) {
try{
flCanvas.removeAllViews();
View viewBarCanvas = new ViewBarCanvas(getApplicationContext(), barSpots);
flCanvas.addView(viewBarCanvas);
}
catch(Exception e){
LogUtil.log(e.getMessage());
}
}
ViewBarCanvas:
Context mContext;
public List<ViewBarSpot> mBoxs;
public List<BarSpot> mBarSpots;
public ViewBarCanvas(Context context, List<BarSpot> barSpots) {
super(context);
mContext = context;
mBarSpots = barSpots;
mBoxs = new ArrayList<>();
for (BarSpot barSpot : barSpots) {
mBoxs.add(new ViewBarSpot(context, barSpot));
}
init(null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (ViewBarSpot viewBarSpot : mBoxs) {
canvas.drawRect(viewBarSpot.mRectangle, viewBarSpot.mPaintBackground);
canvas.drawRect(viewBarSpot.mRectangle, viewBarSpot.mStrokePaint);
canvas.drawText(viewBarSpot.mText, viewBarSpot.mStart, viewBarSpot.mStart + viewBarSpot.mNumberOfCharacters, viewBarSpot.mRectangle.exactCenterX(), viewBarSpot.mRectangle.exactCenterY(), viewBarSpot.mPaintText);
}
}
ViewBarSpot:
public float mPosX;
public float mPosY;
public float mLastTouchX;
public float mLastTouchY;
@Override
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN: {
// Remember where we started
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_MOVE: {
// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
// Move the object
if (mRectangle.left < x && mRectangle.right > x && mRectangle.top < y && mRectangle.bottom > y) {
mPosX += dx;
mPosY += dy;
// Invalidate to request a redraw
postInvalidate();
}
// Remember this touch position for the next move event
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP:
/*realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
mBarSpot.setPositionLeft((int)mPosX - SQUARE_SIZE/2);
mBarSpot.setPositionRight((int)mPosX + SQUARE_SIZE/2);
mBarSpot.setPositionTop((int)mPosY - SQUARE_SIZE/2);
mBarSpot.setPositionBottom((int)mPosY + SQUARE_SIZE/2);
}
});*/
}
postInvalidate();
return true;
}
public ViewBarSpot(Context context, BarSpot barSpot) {
super(context);
mContext = context;
mBarSpot = barSpot;
mText = barSpot.getDescription();
mRectangle = new Rect(mBarSpot.getPositionLeft(), mBarSpot.getPositionTop(), mBarSpot.getPositionLeft() + SQUARE_SIZE, mBarSpot.getPositionTop()+ SQUARE_SIZE);
mPaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBackground.setColor(mContext.getResources().getColor(R.color.themeColor1));
mPaintBackground.setStyle(Paint.Style.FILL);
mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setColor(mContext.getResources().getColor(R.color.themeColor3));
mStrokePaint.setStrokeWidth(2);
mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setColor(mContext.getResources().getColor(R.color.themeColor3));
mPaintText.setTextSize(TEXT_SIZE);
mPaintText.setTextAlign(Paint.Align.CENTER);
mNumberOfCharacters = mPaintText.breakText(mText,true, SQUARE_SIZE,null);
mStart = (mText.length()- mNumberOfCharacters)/2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}