2012-02-08 19 views
0

私は2人のプレーヤーが同時に遊ぶAndroidゲームをコーディングしています。一方のプレイヤーは通常の方法で電話を受け、もう一方のプレーヤーは逆さまになります。Androidはレイアウトを逆さまにしたり、180度回転したりします。

クイズなので、私はキャンバスやグラフィックを使用しませんでした。これには2つの線形レイアウトしか含まれておらず、そのうちの1つは上下逆さまになっています。このために、私は次のものを使用しました:

android:rotation="180" 

レイアウトの1つ。

Eclipseのmy xmlのグラフィカルビューでは逆さまになっていましたが、エミュレータや携帯電話でそれを実行すると、180度回転したり回転したりしません。

XML

<?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:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" android:clipToPadding="false" 
     android:rotation="180"> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    </LinearLayout> 

</LinearLayout 

Javaコード

public class x extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

は、どのように私は複雑化せずに、それを回転させることができます。私は現在APIレベル15を使用していますが、私はAPIレベルに制限はありません。

答えて

1

私はあなたがそれはあなたがあなたのXMLを記述する場合、代わりにあなたが作成しようとした他のレイアウトのVerticalRelativeLayoutを使うonDraw()

public class RotatedLinearLayout extends RotatedLinearLayout { 
    final boolean topDown; 

    public RotatedLinearLayout (Context context){ 
    super(context); 
    } 

    public RotatedLinearLayout (Context context, AttributeSet attrs){ 
     super(context, attrs); 
    } 

    public RotatedLinearLayout (Context context, AttributeSet attrs, int defStyle){ 
     super(context, attrs,defStyle); 
    } 


    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     super.onMeasure(heightMeasureSpec, widthMeasureSpec); 
     setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); 
    } 

    @Override 
    protected void onDraw(Canvas canvas){ 
     TextPaint textPaint = getPaint(); 
     textPaint.setColor(getCurrentTextColor()); 
     textPaint.drawableState = getDrawableState(); 

     canvas.save(); 

     if(topDown){ 
     canvas.translate(getWidth(), 0); 
     canvas.rotate(180); 
     }else { 
     canvas.translate(0, getHeight()); 
     canvas.rotate(-180); 
     } 
     canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop()); 
     getLayout().draw(canvas); 
     canvas.restore(); 
    } 
} 

だ上で回転を処理するカスタムレイアウトを作成することをお勧めします。

<?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:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <path.to.package.RotatedLinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" android:clipToPadding="false" 
     > 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </path.to.package.RotatedLinearLayout > 

</LinearLayout 
+0

私は任意の相対レイアウトを使います。 – akhil

+0

私は線形レイアウトを使用しました。 – akhil

+0

そこに私は線形レイアウトが物事をcompilcatingせずに回転させることができない方法です。 – akhil

関連する問題