How can I flip the whole screen?

I want to be able to mirror my application so that it can be seen in the windshield of the vehicle.

My XML has several nested LinearLayout s, TextView and ImageView s. I am currently converting each of them, and although it is mirrored, the structure of the elements is not (what was on top is now below).

I searched for a few days and still tried several approaches that failed.

An animation that uses a matrix to scroll through the types of operations of the X-axis, except that it either returns or remains and is not updated, which is not suitable for interacting with the application.

I just tried to create a custom LinearLayout extending the parent, hoping I could apply the matrix in the onDraw() method, but this gives me a blank screen (I had to set setWillNotDraw(false); to click onDraw() ).

+4
source share
2 answers

In the end, I found a solution that worked well for me (until it caused problems for users).

My solution was to override dispatchDraw to scale the canvas in my custom LinearLayout. Then I just needed to flip the touch events by overriding dispatchTouchEvent :

 public class CustomContainer extends LinearLayout { public CustomContainer(Context context) { super(context); this.setWillNotDraw(false); } public CustomContainer(Context context, AttributeSet attrs) { super(context, attrs); this.setWillNotDraw(false); } public CustomContainer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.setWillNotDraw(false); } @Override protected void dispatchDraw(Canvas canvas) { canvas.save(Canvas.MATRIX_SAVE_FLAG); // Flip the view canvas if (MyHUDActivity.mHUDMode) canvas.scale(1,-1, getWidth(), getHeight()/2f); super.dispatchDraw(canvas); canvas.restore(); } @Override public boolean dispatchTouchEvent(MotionEvent event) { // If in HUD mode then flip the touch zones if (MyHUDActivity.mHUDMode) event.setLocation(event.getX(), getHeight()-event.getY()); return super.dispatchTouchEvent(event); } } 
+3
source

You can use the new api animation to handle the revert back after a horizontal flip.

0
source

Source: https://habr.com/ru/post/1386734/


All Articles