2011-04-27 47 views
0

我正在試圖製作一個視圖控制器,它是在縱向模式下修復的,但是它的橫向版本可以在頂部「淡入」。這意味着我需要能夠獲得適合風景的正確大小的屏幕版本,並且旋轉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); 
    } 
} 

不建議實際改變視圖控制器的方向。我需要它保持肖像模式,以便在部分透明度的同時顯示肖像版本。

要清楚,我需要能夠在其旋轉座標的視圖交互,所以我需要能夠按按鈕,使用滾動型等

+0

你可以添加一個iPhone屏幕截圖,顯示你試圖複製的行爲? – 2011-04-27 14:59:49

+0

我將不得不給你一個文本截圖。 :)考慮以縱向模式顯示的一頁文字。現在考慮橫向模式下的一頁文本。現在將這兩個圖片都畫在一起,在那裏您可以看到橫向模式文本微微疊加在縱向模式文本的頂部。這就是我所追求的,這是一個原因,它只是沒有進入問題本身。 :) – 2011-04-27 15:13:32

回答

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

,但我看不出爲什麼你會想這

+0

我絕對不想那樣。我想旋轉實際視圖,而不是垂直佈局。 – 2011-05-03 20:23:43

+0

0oo所以你的意思是讀你需要移動頭部的文字。 – Blundell 2011-05-03 21:20:51

+0

是的,這是正確的。 – 2011-05-03 21:25:52

0

你要在兩個視圖(垂直和水平?) 滾動如果沒有,也許你可以採取垂直文本的屏幕截圖(請參閱here)然後做一個佈局的實際旋轉(我知道你不想這樣做,但我不明白爲什麼,如果只需要垂直視圖作爲疊加層),然後使用垂直屏幕的屏幕截圖作爲一個簡單透明的位圖覆蓋...

+0

橫向視圖位於滾動視圖中,因此它不能是截圖。 – 2011-05-03 20:42:13

1

API級別11介紹setRotationX/YView s,這似乎正是你要找的。

假設蜂窩是不是你的目標API的版本,這裏是我發現了幾個小時,這種玩弄後(肯定比我當前的項目更具挑戰性!):

  1. 渲染是肯定是可行的(且相對容易)
  2. 這是一個龐大的黑客,你不應該擺在首位

基本上,主要的問題是沒有渲染,但處理事件那樣做。由於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(); 
     

    } 
} 

聚焦和平移事件被遺留作爲一個練習閱讀器:)

+0

這是相當不錯的東西,並且比我迄今能夠獲得的更多。我發現了一些循環代碼(http://groups.google.com/group/android-developers/msg/4c0388feef7bd3d2),它在映射事件時非常有用,但是有太多東西不能正常工作,比如EditText視圖和按鈕上的高亮顯示。 – 2011-05-04 13:57:24

+0

感謝您的幫助。它沒有解決我的問題,但它是值得的賞金。 – 2011-05-09 14:03:18