2013-02-22 194 views
1

我想基於FrameLayout裏可能被旋轉90度順時針/逆時針創建ViewGroup中,它仍然會被正確旋轉(90度)的ViewGroup根

工作到目前爲止,我的成績不是很sucesful。到目前爲止,它看起來像(旋轉前左側,右後;爲鮮紅色抱歉)

Rotated ViewGroup

佈局的活動

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

    <com.example.TestProject.RotatedFrameLayout 
     android:id="@+id/container" 
     android:layout_centerInParent="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#00F"/> 

</RelativeLayout> 

RotatedFrameLayout

public class RotatedFrameLayout extends FrameLayout { 

    private boolean firstMeasure = true; 

    public RotatedFrameLayout(Context context) { 
     super(context); 
     init(); 
    } 

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

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

    private void init() { 
     setRotation(90f); 
    } 

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

部分額外信息

  • 我不想用動畫旋轉,因爲按鈕是不可點擊這樣
  • 我不想使用風景模式,因爲在屏幕上的導航按鈕景觀花了很多空間的Nexus 7 (這就是爲什麼我想馬麗娟是旋轉
  • 的主要原因似乎屏幕只有左,右側出界

回答

5

這是挺難做的,我覺得是不是值得做。但如果你真的想這樣做,你需要:

  • 傳遞給ViewGroup正確的大小尺寸(交換寬度和高度)。
  • 旋轉ViewGroup畫布90度。 在這一點上一切都應該看起來不錯,但觸摸事件無法正常工作。
  • 攔截所有觸摸事件並交換x和y。然後將固定事件傳遞給ViewGroup。

我沒有任何代碼示例,從來沒有見過任何)這種方式應該工作,我們做了縮放與片段的轉換,我們必須修復片段縮放時的觸摸事件座標。

我還沒有測試它在很大程度上,但這個工程:

public class RotatedFrameLayout extends FrameLayout { 

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

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

public RotatedFrameLayout(Context context) { 
    super(context); 
    init(); 
} 

@SuppressLint("NewApi") 
private void init() { 
    setPivotX(0); 
    setPivotY(0); 
    setRotation(90f); 
} 

@SuppressLint("NewApi") 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(heightMeasureSpec, widthMeasureSpec); 
    setTranslationX(getMeasuredHeight()); 
} 
} 

example

+0

您好,感謝提示。我交換了尺寸並旋轉了它,嘗試了各種組合,到目前爲止沒有成功,但是在上面的代碼不會中斷按鈕觸摸行爲(它仍然是可點擊的)。更多的問題與適當的大小。這值得麼?在屏幕導航按鈕上執行某些操作是很有想法的,這些按鈕在較小的設備上佔用了大量空間:/ 10英寸平板電腦上,這不是一個問題。 – outlying 2013-02-22 14:14:18

+0

自API11以來,我還沒有看到setRotation方法。我會看看它是如何工作的。我們支持舊的Android版本,所以我們確實使用它。 – Leonidos 2013-02-22 14:26:35

+0

更新我的答案。我做了幾乎和你一樣,它的工作原理... – Leonidos 2013-02-22 15:16:39