我有一个视图,它有自定义的实现 - 我可以在上面画东西,然后我将这个视图在运行时添加到一个RelativeLayout
布局中,方法如下:
RelativeLayout paintRootView = (RelativeLayout) findViewById(R.id.root_view);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
paintView.setLayoutParams(params);
PaintView paintView = new PaintView(context);
paintRootView.addView(paintView);
我有一些控件,它们指定了何时onTouch
方法应在视图上绘制/拖动视图或调整视图大小。绘制和平移视图正常工作,但当我尝试缩放视图时,它会闪烁。我错过了什么吗?
使用的代码:
private int mode = NONE;
private float oldDist;
private static final float MIN_ZOOM = 0.5f;
private static final float MAX_ZOOM = 3.0f;
@Override
public boolean onTouch(View view, MotionEvent event) {
if (IS_IN_MOOVING_STATE) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
// some code for drag
mode = DRAG;
break;
}
case MotionEvent.ACTION_POINTER_DOWN: {
oldDist = getSpacing(event);
mode = ZOOM;
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP: {
mode = NONE;
break;
}
case MotionEvent.ACTION_MOVE: {
if (mode == DRAG) {
// some code for drag
} else if (mode == ZOOM) {
float newDist = getSpacing(event);
float scale = newDist / oldDist;
PointF pointF = getMidPoint(event);
if (scale >= MIN_ZOOM && scale < MAX_ZOOM) {
zoomPaintView(scale, scale, pointF);
}
}
break;
}
}
paintRootView.invalidate();
return true;
}
return false;
}
RecyclerView
的项目大小。 - DolDurma