我有一个需要放大和缩小的RecyclerView,但视图本身需要保持在同一个地方。如何仅缩放视图的内容,而不缩放视图本身?我尝试过canvas.scale,但它可以缩放整个视图,因此它可以离开屏幕,也可以缩小。
编辑:RecyclerView代码
public class ZoomRecyclerView extends RecyclerView {
private RecyclerViewHeaderDecorator recyclerViewHeaderDecorator;
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;
private float focusX;
private float focusY;
public void setRecyclerViewHeaderDecorator(RecyclerViewHeaderDecorator recyclerViewHeaderDecorator) {
this.recyclerViewHeaderDecorator = recyclerViewHeaderDecorator;
}
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);
if (event.getPointerCount() > 1) {
//notify the scaleGestureDetector that an event has happened
scaleGestureDetector.onTouchEvent(event);
}
return true;
}
@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
focusX = scaleGestureDetector.getFocusX();
focusY = scaleGestureDetector.getFocusY();
//scales the display, centered on where the user is touching the display
canvas.scale(scaleFactor, scaleFactor, focusX, focusY);
super.dispatchDraw(canvas);
}
}
提前致谢!