我有一个缩放的自定义RecyclerView,但是当我放大时,整个RecyclerView会放大,我无法滚动到视图的边框,因为它们不在屏幕上。我如何只缩放视图的内容而不缩放视图本身?
ZoomRecyclerView的代码:
public class ZoomRecyclerView extends RecyclerView {
private ScaleGestureDetector scaleGestureDetector;
private static final String TAG = ZoomRecyclerView.class.getSimpleName();
private float scaleFactor = 1.f;
private static final float minScale = 1.0f;
private static final float maxScale = 3.0f;
public ZoomRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
//makes sure user does not zoom in or out past a certain amount
scaleFactor = Math.max(minScale, Math.min(scaleFactor, maxScale));
//refresh the view and compute the size of the view in the screen
invalidate();
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
//notify the scaleGestureDetector that an event has happened
scaleGestureDetector.onTouchEvent(event);
return true;
}
@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
//scales the display, centered on where the user is touching the display
canvas.scale(scaleFactor, scaleFactor, scaleGestureDetector.getFocusX(), scaleGestureDetector.getFocusY());
super.dispatchDraw(canvas);
}
}