旋转(90度)根ViewGroup

out*_*ing 1 android

我正在尝试创建基于FrameLayout的ViewGroup,它可能会旋转90度CW/CCW并且它仍然可以正常工作

到目前为止,我的结果并不那么成功.到目前为止它看起来像那样(旋转前的左侧,后面的左侧;抱歉鲜红色)

旋转的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>
Run Code Online (Sandbox Code Playgroud)

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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

一些额外的信息

  • 我不想使用动画旋转,因为按钮不能以这种方式点击
  • 我不想使用横向模式,因为在屏幕上的横向导航按钮在Nexus 7上占用了大量空间(这是我试图改变旋转的主要原因
  • 似乎屏幕的左侧和右侧都是超出范围的

Leo*_*dos 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());
}
}
Run Code Online (Sandbox Code Playgroud)

例