2014-08-27 134 views
2

那麼,我想創建一個簡單的應用程序。 即使小部件是DigitalClock或其他東西,也會顛倒佈局的外觀。 我試過用android:scaleX =「 - 1」; 但它只適用於文本和圖像。如何創建HUD(鏡像)佈局/屏幕Android?

如何創建顛倒整個屏幕的佈局,使其看起來像鏡像視圖?

提前致謝。

普通視圖:

normal view 我想要的看法:

the view I want

回答

5

只是做一個自定義的ViewGroup(或擴展現有的一個),縮放畫布它借鑑了前兒童:

public class MirrorRelativeLayout extends RelativeLayout { 
    public MirrorRelativeLayout(Context context) { 
     super(context); 
    } 

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

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

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     canvas.save(); 
     // Scale the canvas in reverse in the x-direction, pivoting on 
     // the center of the view 
     canvas.scale(-1f, 1f, getWidth()/2f, getHeight()/2f); 
     super.dispatchDraw(canvas); 
     canvas.restore(); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     canvas.save(); 
     // Scale the canvas in reverse in the x-direction, pivoting on 
     // the center of the view 
     canvas.scale(-1f, 1f, getWidth()/2f, getHeight()/2f); 
     super.dispatchDraw(canvas); 
     canvas.restore(); 
    } 
} 

然後只是使用ViewGroup a s您的佈局的根本:

<com.your.packagename.MirrorRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:text="Mirror Text!" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</com.your.packagename.MirrorRelativeLayout>