我正在尝试制作一个以纵向模式修复的视图控制器,但它有一个可以在顶部“淡入”的横向版本。这意味着我需要能够拥有适合横向大小的屏幕版本,并旋转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);
}
}
建议实际更改视图控制器的方向是没有用的。我需要它保持纵向模式,以便在部分Alpha透明度的同时显示肖像版本。
要清楚,我需要能够以旋转的坐标与视图进行交互,因此我需要能够按下按钮,使用滚动视图等。
答案 0 :(得分:1)
API Level 11为setRotationX/Y
引入了View
,这似乎正是您正在寻找的。
假设Honeycomb不是你的目标API版本,这是我在玩了几个小时之后发现的东西(绝对比我当前的项目更具挑战性)!:
基本上,主要问题不是渲染而是处理事件。由于Android不知道视图是侧面的(您只是以这种方式渲染),因此视图将响应由原始预旋转坐标限定的区域。因此,没有点击(好吧,除非你有一个方形按钮,只是侧面有文字!)。
真的,你应该考虑向后移植Honeycomb的变化。
有了这个说法并且有一个大的免责声明,可能有很多情况下这不起作用,这是一个示例应用程序:
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();
}
}
聚焦和翻译事件留给读者练习:)
答案 1 :(得分:0)
是否要在两个视图中滚动(垂直和水平?) 如果没有,也许您可以截取垂直文本的屏幕截图(请参阅here)然后对您的布局进行实际旋转(我知道您不希望这样但我不明白为什么,如果是垂直视图只需要作为叠加层,然后使用垂直屏幕的屏幕截图作为简单的透明位图叠加...
答案 2 :(得分:-1)
从你的评论中听起来你想要这个:
<?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>
但我看不出你想要这个怎么或为什么