Android 如何创建我的视图的横向版本而不实际处于横向模式?

Android 如何创建我的视图的横向版本而不实际处于横向模式?,android,rotation,Android,Rotation,我试图使一个视图控制器,是固定在纵向模式,但有一个横向版本,可以“淡入”的顶部。这意味着我需要有一个屏幕版本,它的大小适合横向,并且旋转90度(或270度,具体取决于角度)。在iPhone上,这很容易,但我正在和Android做斗争。我有一个自定义视图,其中包含我想要旋转的视图,但我似乎无法正确调整子视图的大小,或使旋转正确对齐。有没有更简单的方法?或者,我做错了什么 @Override protected void onDraw(Canvas canvas) { if (getChil

我试图使一个视图控制器,是固定在纵向模式,但有一个横向版本,可以“淡入”的顶部。这意味着我需要有一个屏幕版本,它的大小适合横向,并且旋转90度(或270度,具体取决于角度)。在iPhone上,这很容易,但我正在和Android做斗争。我有一个自定义视图,其中包含我想要旋转的视图,但我似乎无法正确调整子视图的大小,或使旋转正确对齐。有没有更简单的方法?或者,我做错了什么

@Override
protected void onDraw(Canvas canvas) {
    if (getChildCount() == 1) {
        canvas.save();
        canvas.rotate(90, canvas.getWidth() / 2, canvas.getHeight() / 2); 
        // I have no idea what my pivot point should be

        View child  = getChildAt(0);

        Bitmap bitmap = // bitmap of child
        Paint paint = new Paint();
        canvas.drawBitmap(bitmap, 0, 0, paint);

        canvas.restore();
    }
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    if (getChildCount() == 1) {
        View child  = getChildAt(0);
        child.layout(top, left, bottom, right);
    }
}
没有帮助的是建议实际更改视图控制器的方向。我需要它保持在纵向模式,以显示在同一时间与部分阿尔法透明度的纵向版本


为了清楚起见,我需要能够在旋转坐标中与视图交互,因此我需要能够按下按钮、使用滚动视图等。

从您的评论中,听起来您希望这样:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <LinearLayout
        android:id="@+id/linearlayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
    </LinearLayout>
</FrameLayout>


但是我看不出您希望如何或为什么这样做

您希望在两个视图(垂直和水平)中滚动吗
如果没有,也许你可以拍摄垂直文本的屏幕截图(请参阅),然后对布局进行实际旋转(我知道你不想这样,但我不明白为什么,如果垂直视图只是需要作为覆盖)然后将垂直屏幕的屏幕截图用作简单的透明位图覆盖…

API Level 11为
视图
s引入了
setRotationX/Y
,这似乎正是您要寻找的

假设Honeycomb不是您的目标API版本,下面是我在玩了几个小时后发现的(肯定比我当前的项目更具挑战性!):

  • 渲染当然是可行的(而且相对容易)
  • 这是一次大规模的黑客攻击,你一开始就不应该这么做
  • 基本上,主要问题不是渲染,而是处理事件。由于Android不知道视图是侧向的(您只是以这种方式渲染),因此视图将响应由原始旋转前坐标限定的区域。因此,不要点击(好吧,除非你有一个方形的按钮,它的文本就在旁边!)

    实际上,您可能应该研究一下如何将蜂窝更改进行后移植

    尽管如此,并且有一个很大的免责声明,即可能有很多情况下这不起作用,下面是一个示例应用程序:

    package com.side;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;
    import android.widget.TextView;
    
    public class TestActivity extends Activity {
        private class SidewaysGroup extends ViewGroup{
            public SidewaysGroup(Context context) {
                super(context);
            }
    
            @Override
            protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
                Log.i("Sideways", "Parent size: " + getWidth() + "x" + getHeight() + ", child size: " + child.getWidth() + "x" + child.getHeight());
    
                // Create a new canvas for the child (there's probably a way to use the original canvas but I couldn't figure out the transformations)
                Canvas childCanvas = new Canvas();
                Bitmap childBitmap = Bitmap.createBitmap(child.getWidth(), child.getHeight(), Bitmap.Config.ARGB_8888);
                childCanvas.setBitmap(childBitmap);
    
                boolean ret = super.drawChild(childCanvas, child, drawingTime);
    
                Matrix matrix = new Matrix();
    
                // rotate at the bottom left corner
                matrix.postRotate(90f, 0, childBitmap.getHeight());
    
                // after the rotation we are one `height` further down than we should be
                matrix.postTranslate(0, -childBitmap.getHeight());
    
                canvas.drawBitmap(childBitmap, matrix, new Paint());
    
                return ret;
            }
    
            @Override
            protected void onLayout(boolean changed, int left, int top, int right,
                    int bottom) {
                if(changed && getChildCount()==1)
                {
                    final View child = getChildAt(0);
    
                    // This is breaking the flow (measuring would be done twice) - should be moved to onMeasure or measure() itself
                    // notice that it inverts the dimensions
                    child.measure(MeasureSpec.makeMeasureSpec(getMeasuredHeight(),
                            MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(
                            getMeasuredWidth(), MeasureSpec.AT_MOST));
    
                    child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
                }
            }
    
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            TextView verticalView = new TextView(this);
            verticalView.setText("This is the vertical text");
            verticalView.setGravity(Gravity.CENTER);
            verticalView.setTextSize(50f);
            verticalView.setTextColor(Color.parseColor("#88ffffff")); // add a bit of transparency to the text
    
    
            SidewaysGroup group = new SidewaysGroup(this);
    
            Button horizontalButton= new Button(this);
            horizontalButton.setText("This is the horizontal button");
            horizontalButton.setGravity(Gravity.CENTER);
            horizontalButton.setTextSize(50f);
            horizontalButton.setBackgroundDrawable(null);
            horizontalButton.setTextColor(Color.WHITE); 
    
            horizontalButton.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    Log.i("Sideways", "Button click");
                }
            });
    
            group.addView(horizontalButton);
    
    
            RelativeLayout mainLayout = new RelativeLayout(this);
    
            RelativeLayout.LayoutParams relparams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    
            relparams.addRule(RelativeLayout.CENTER_IN_PARENT);
    
            mainLayout.addView(verticalView, relparams);
            mainLayout.addView(group, relparams);
    
            setContentView(mainLayout);
    
            mainLayout.requestLayout();
            
    } }
    聚焦和翻译事件是留给读者的一个练习:)

    你能添加一个iphone屏幕截图,显示你试图复制的行为吗?我必须给你一个文本屏幕截图。:)考虑在纵向模式下显示的文本页。现在考虑在风景模式下的一页文本。现在将这两种模式结合在一起,您可以看到横向模式文本隐约覆盖在纵向模式文本的顶部。这就是我拍摄的目的,这是有原因的,它只是没有进入问题本身。:)我绝对不想那样。我想要一个旋转的实际视图,而不是垂直布局。0oo所以你的意思是阅读你需要移动头部的文本。横向视图位于滚动视图中,所以它不能只是一个屏幕截图。这是非常好的东西,而且比我迄今所能得到的还要远。我发现了一些在映射事件时有用的旋转代码(),但是有太多的东西无法正常工作,例如EditText视图和按钮上的突出显示。感谢您的帮助。这并没有解决我的问题,但值得赏金。