gpt4 book ai didi

android - 旋转(90 度)根 ViewGroup

转载 作者:行者123 更新时间:2023-11-29 15:23:53 30 4
gpt4 key购买 nike

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

到目前为止,我的结果还不是很成功。到目前为止它看起来像那样(旋转前的左侧,旋转后的右侧;对不起,亮红色)

Rotated ViewGroup

Activity 的

布局

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

旋转框架布局

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 上占用了很多空间(这是我试图增加旋转的主要原因
  • 好像只有屏幕左右两边是越界的

最佳答案

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

  • 传递给 ViewGroup 正确的尺寸尺寸(交换宽度和高度)。
  • 将 ViewGroup Canvas 旋转 90 度。此时一切看起来都正常,但触摸事件无法正常工作。
  • 拦截所有触摸事件并交换 x 和 y。然后将固定事件传递给 ViewGroup。

我没有任何代码示例,也从未见过任何代码示例)这种方式应该可行,我们对 fragment 进行了缩放转换,在缩放 fragment 时我们必须修复触摸事件坐标。

我还没有对它进行大量测试,但它确实有效:

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

example

关于android - 旋转(90 度)根 ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15025658/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com