RecyclerView的标题具有错误的缩放焦点

时间:2017-07-29 17:08:21

标签: android android-recyclerview

我为缩放的RecyclerView实现了一个标题(作为itemDecoration)。但是,放大时,标题不会与视图的其余部分以相同的焦点放大。它始终使用左上角的焦点进行缩放。如何使用相同的焦点使标题放大?

缩放RecyclerView:

    @Override
protected void dispatchDraw(@NonNull Canvas canvas) {
    focusX = scaleGestureDetector.getFocusX();
    focusY = scaleGestureDetector.getFocusY();

    recyclerViewHeaderDecorator.setScaleFactor(scaleFactor);

    //scales the display, centered on where the user is touching the display
    canvas.scale(scaleFactor, scaleFactor, focusX, focusY);


    //Log.e(TAG, Integer.toString(viewCoordinateY));
    super.dispatchDraw(canvas);
}

标题装饰:

public class RecyclerViewHeaderDecorator extends ZoomRecyclerView.ItemDecoration {
private static final String TAG = RecyclerViewDividerDecorator.class.getSimpleName();
private View header;
private float scaleFactor;
private float relativeX;
private float relativeY;


public void setScaleFactor(float scaleFactor) {
    this.scaleFactor = scaleFactor;
}

public RecyclerViewHeaderDecorator(View header) {
    this.header = header;
}

@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getChildCount() > 0) {

        // Specs for parent (RecyclerView)
        int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);

        // Specs for children (headers)
        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width);
        int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height);

        header.measure(childWidthSpec, childHeightSpec);

        header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());

        //keeps header on top left of RecyclerView
        float[] values = new float[9];
        Matrix matrix;
        matrix = canvas.getMatrix();
        matrix.getValues(values);
        relativeX = values[2];
        relativeY = values[5];
        canvas.translate((-relativeX) / scaleFactor, (-relativeY) / scaleFactor);

        canvas.save();
        header.draw(canvas);
        canvas.restore();
    }
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    header.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    int height = header.getMeasuredHeight();
    super.getItemOffsets(outRect, view, parent, state);
    if (parent.getChildAdapterPosition(view) == 0) {
        outRect.top = height;
    }
}
}

0 个答案:

没有答案