Search code examples
androidxmlandroid-fragmentsandroid-fragmentactivity

Rotate fragment to create a reflection on the x-axis


So basically I have defined a fragment for a card game I am creating, I wish to reuse this fragment for both player 1 and player 2 (since they both have the same layout) however I want them to be a mirror of each other. How can this be achieved, I have research rotating a fragment but no answers so far have described how to do so in code, they only describe handling when the screen rotates.

The activity for a 1v1 game

package com.PigRam.magichelper;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class OneVsOneDuelActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.one_vs_one_view);
}
}

The layout for the fragments

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:baselineAligned="true">

<fragment android:name="com.PigRam.magichelper.PlayerOneDuelFragment"
    android:id="@+id/player_one_fragment"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"/>

<fragment android:name="com.PigRam.magichelper.PlayerTwoDuelFragment"
    android:id="@+id/player_two_fragment"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp" />

</LinearLayout>

Currently the fragments layout just have a single button in their layout that I want to appear to indicate a players turn is over.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button android:text="@string/end_turn"
    android:id="@+id/end_turn_botton"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>

</LinearLayout>

Player 1's code (Player 2 is exactly the same)

package com.PigRam.magichelper;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class OneVsOneDuelActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.one_vs_one_view);
}
}

Solution

  • I'm guessing that if you're using Android v3.0+ you could reuse the same fragment and have the 2nd instance rotate it's view by 180 degrees using http://developer.android.com/reference/android/view/View.html#setRotation(float) (after setting the pivot point to be 0.5, 0.5)