3

さて、簡単なアプリを作りたいと思います。ウィジェットが DigitalClock などであっても、レイアウトの外観を反転させます。私は android:scaleX="-1"; で試しました。ただし、テキストと画像でのみ機能します。

画面全体を反転させて鏡像のように見せるレイアウトを作成するにはどうすればよいですか?

先に感謝します。

通常のビュー:

通常のビュー 私が望むビュー:

私が望む景色

4

1 に答える 1

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>
于 2014-08-27T20:05:51.150 に答える