2014-08-27 13 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 레이아웃의 근원 :

<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>