我正在尝试使用缩放和滚动创建图像视图。通过互联网检查了许多解决方案,但我没有得到准确的结果。我当前的imageview有滚动属性。需要添加缩放。
public class MapView extends ImageView {
private GestureDetectorCompat gestureDetector;
private OverScroller overScroller;
private final int screenW;
private final int screenH;
private int positionX = 0;
private int positionY = 0;
public MapView(Context context) {
super(context);
// We need screen dimensions to make sure we don't over scroll the image
DisplayMetrics dm = getResources().getDisplayMetrics();
screenW = dm.widthPixels;
screenH = dm.heightPixels;
gestureDetector = new GestureDetectorCompat(context, gestureListener);
overScroller = new OverScroller(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
@Override
public void computeScroll() {
super.computeScroll();
// computeScrollOffset() returns true only when the scrolling isn't
// already finished
if (overScroller.computeScrollOffset()) {
positionX = overScroller.getCurrX();
positionY = overScroller.getCurrY();
scrollTo(positionX, positionY);
} else {
// when scrolling is over, we will want to "spring back" if the
// image is overscrolled
overScroller.springBack(positionX, positionY, 0, getMaxHorizontal(), 0, getMaxVertical());
}
}
private int getMaxHorizontal() {
return (Math.abs(getDrawable().getBounds().width() - screenW));
}
private int getMaxVertical() {
return (Math.abs(getDrawable().getBounds().height() - screenH));
}
private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
overScroller.forceFinished(true);
ViewCompat.postInvalidateOnAnimation(MapView.this);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
overScroller.forceFinished(true);
overScroller.fling(positionX, positionY, (int) -velocityX, (int) -velocityY, 0, getMaxHorizontal(), 0,
getMaxVertical());
ViewCompat.postInvalidateOnAnimation(MapView.this);
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
overScroller.forceFinished(true);
// normalize scrolling distances to not overscroll the image
int dx = (int) distanceX;
int dy = (int) distanceY;
int newPositionX = positionX + dx;
int newPositionY = positionY + dy;
if (newPositionX < 0) {
dx -= newPositionX;
} else if (newPositionX > getMaxHorizontal()) {
dx -= (newPositionX - getMaxHorizontal());
}
if (newPositionY < 0) {
dy -= newPositionY;
} else if (newPositionY > getMaxVertical()) {
dy -= (newPositionY - getMaxVertical());
}
overScroller.startScroll(positionX, positionY, dx, dy, 0);
ViewCompat.postInvalidateOnAnimation(MapView.this);
return true;
}
};
}
我可以得到任何指示吗?
答案 0 :(得分:0)
您可能希望查看此开源库:https://github.com/ozeetee/AndroidImageZoomCrop
它可以满足您的需求和方式,但很容易减少您需要的内容