我正在使用非常标准的拖放实现,以便拖动图钉并将其放置在图像视图的顶部以指向图像的特定部分:
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
FrameLayout container = (FrameLayout) v;
container.addView(view);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
int a = Math.round((event.getX() - (view.getWidth() / 2)));
int b = Math.round((event.getY() - (view.getHeight() / 2)));
lp.setMargins(a,b,0,0);
view.setLayoutParams(lp);
view.setVisibility(View.VISIBLE);
//Gets actual pixel location on image - currently unused
float[] point = new float[] {event.getX(), event.getY()};
Matrix inverse = new Matrix();
iv.getImageMatrix().invert(inverse);
inverse.mapPoints(point);
float density = getResources().getDisplayMetrics().density;
point[0] /= density;
point[1] /= density;
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
图像视图是一个可缩放的图像视图(chrisbanes / PhotoView),如果图像保持在通常的100%比例,则坐标全部正常工作并且标记正确放置。但是,如果我放大图像,将标记放在特定的位置以进行更精细的控制,而在缩小标记时获得正确的坐标不会保持在相对于图像的相同位置。只是父母的。请让我知道您希望看到的代码(没有其他内容)或者我是否需要重新解释
由于
答案 0 :(得分:0)
我做了一个应用程序,用户可以触摸移动视图,也可以缩放。
在我的情况下,包含多个视图的所有ViewGroup(RelativeLayout)都可以整体缩放。
我使用onTouch而不是onDrag,在内部我将尺寸保存在 dp 中并从 dp 转换为像素我乘以(屏幕)密度*缩放因子)
这是我从onTouch ACTION_MOVE调用的方法,xPxInitial,dpLeftInitial,yPxInitial,dpTopInitial的值先前已在ACTION_DOWN中设置。
@Override
public boolean moveControl(float xPx, float yPx) {
int newDpLeft, newDpTop;
newDpLeft = pixelsToDp((int) (xPx - xPxInitial)) + dpLeftInitial;
newDpTop = pixelsToDp((int) (yPx - yPxInitial)) + dpTopInitial;
// Limit for left & top
if (newDpLeft < 0) newDpLeft = 0;
if (newDpTop < 0) newDpTop = 0;
int oldDpLeft = dpLeft;
int oldDpTop = dpTop;
dpLeft = newDpLeft;
dpTop = newDpTop;
setDimsFromDps();
// return true is the view is really moved
return (dpLeft != oldDpLeft || dpTop != oldDpTop);
}
pixelsToDp
/// Pixels to DP's ....
public int pixelsToDp(int pixs) {
return (int) (pixs / (scrDensity * zoomFactor));
}
setDimsFromDps
protected void setDimsFromDps() {
RelativeLayout.LayoutParams layPar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// avoid left & top < 0
layPar.setMargins(Math.max(dpToPixels(dpLeft), 0), Math.max(dpToPixels(dpTop), 0), 0, 0);
layPar.width = dpToPixels(dpWidth);
layPar.height = dpToPixels(dpHeight);
setLayoutParams(layPar);
}
Hope it will help