【问题标题】:Rotated ( 90 degrees ) root ViewGroup旋转(90 度)根 ViewGroup
【发布时间】:2013-02-22 23:14:33
【问题描述】:

我正在尝试基于 FrameLayout 创建 ViewGroup,它可能会顺时针/逆时针旋转 90 度,但它仍然可以正常工作

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

活动布局

<?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 上占用了大量空间(这是我试图扩大旋转的主要原因
  • 似乎只有屏幕的左右两侧出界

【问题讨论】:

    标签: android


    【解决方案1】:

    这很难做,我认为不值得做。但如果你真的想这样做,你需要:

    • 向 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());
    }
    }
    

    【讨论】:

    • 您好,感谢您的提示。我已经交换了尺寸并旋转了它,尝试了各种组合,到目前为止没有成功,但目前上面的代码并没有破坏触摸行为的按钮(它仍然是可点击的)。尺寸合适的问题更多。这值得么?在较小的设备上使用占用大量横向空间的屏幕导航按钮是一个想法:/ 在 10 英寸平板电脑上,这不是这样的问题。
    • 我还没有见过 setRotation 方法......自 API11 以来。我会看看它是如何工作的。我们支持旧的 android 版本,所以我们确实使用了它。
    • 如果你想支持 RTL 布局方向,在 onMeasure() 使用类似:if (isRtl) { setTranslationX(getMeasuredWidth()); } else { setTranslationX(getMeasuredHeight()); }
    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 2021-05-24
    • 2011-03-22
    • 1970-01-01
    • 2015-03-22
    • 2022-01-13
    • 2020-04-14
    • 2011-09-24
    相关资源
    最近更新 更多