目标是:Imageview在每个角上有4个正方形。如果用户触摸并拖动其中一个,则会缩放图像
给定图像、4个正方形和触摸的正方形,我如何实现此功能
(square=Rectf类(
图像示例
如果有人需要代码,我已经找到了一个可能的解决方案:
float centerX, centerY, startScale, startX, startY;
private void startScaling(MotionEvent e) {
ImageView imageView = (ImageView) getChildAt(0);
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = e.getX();
startY = e.getY();
startScale = imageView.getScaleX();
centerX = imageView.getX() + imageView.getWidth() / 2F;
centerY = imageView.getY() + imageView.getHeight() / 2F;
break;
case MotionEvent.ACTION_MOVE:
// euclidean distance
double length1 = Point.distance(centerX, centerY, startX, startY);
double length2 = Point.distance(centerX, centerY, e.getX(), e.getY());
if(length2 > length1) {
//scale up
float scaleFactor = (float) (length2 / length1);
//scale the image
imageView.setScaleX(startScale * scaleFactor);
imageView.setScaleY(startScale * scaleFactor);
}else {
//scale down
// calculate the scale factor
float scaleFactor = (float) (length1 / length2);
// scale the image
imageView.setScaleX(startScale / scaleFactor);
imageView.setScaleY(startScale / scaleFactor);
}
float scaledWidth = imageView.getWidth() * imageView.getScaleX();
float scaledHeight = imageView.getHeight() * imageView.getScaleY();
// calculate new boubds of the image
float left = centerX - scaledWidth / 2F;
float top = centerY - scaledHeight / 2F;
float right = left + scaledWidth;
float bottom = top + scaledHeight;
break;
case MotionEvent.ACTION_UP:
break;
}
}