1

I have a ViewPager where I override onPageScrolled to receive the amount a user has scrolled. I then need to flip a view according to the positionOffsetreceived from the scroll method. I want to achieve something like this. Flipping all views beside the selected one.

enter image description here

In my PageLayout's onDraw method I have the following code trying to skew the canvas according to the offset received.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Matrix matrix = canvas.getMatrix();
    camera.save();
    camera.rotateX(skew);
    camera.getMatrix(matrix);
    camera.restore();
    canvas.concat(matrix);
}

It looks like this only skewing the from the left:

enter image description here

How to I achieve the same effect from the right? Would be good if it not only skews it but also changing the height so it gives the illusion of being flipped slightly.

nilsi
  • 10,351
  • 10
  • 67
  • 79

1 Answers1

0

Changing the viewing perspective like this made it work.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Matrix matrix = canvas.getMatrix();
    camera.save();
    camera.rotateX(-skew);
    camera.rotateY(0);
    camera.rotateZ(0);
    camera.getMatrix(matrix);

    int centerX = canvas.getWidth() / 2;
    int centerY = canvas.getHeight() / 2;
    matrix.preTranslate(-centerX, -centerY); //This is the key to getting the correct viewing perspective
    matrix.postTranslate(centerX, centerY);

    camera.restore();
    canvas.concat(matrix);
}
nilsi
  • 10,351
  • 10
  • 67
  • 79