Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

一個 Activity 玩透 Android 4.0 五大新控件

編輯:關於 Android 程式設計

Android 4.0 問世的同時,Google 也補了一批新控件。其中最常用的有五個:Switch、Space、GridLayout、PopupMenu 與 TextureView。本文把這五個放進同一個 Activity 裡實作,一次看過。

一、Switch

Switch 顧名思義就是開關,有開與關兩種狀態。

關閉狀態:

開啟狀態:

在版面檔中定義:

android:textOff 是關閉時顯示的文字,android:textOn 是開啟時顯示的文字。

監聽狀態變化:

mSwitch = (Switch) findViewById(R.id._switch);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            Log.i("Switch", "打開Switch");
        } else {
            Log.i("Switch", "關閉Switch");
        }
    }
});

二、Space

顧名思義,Space 代表「空間」——它會佔據一定的版面位置,但不繪製任何內容,常用來做間距或佔位。

三、GridLayout

GridLayout 是網格版面配置,推出它的目的是補足 TableLayout 的不足:

- TableLayout 繼承 LinearLayout,無法同時在水平與垂直兩個方向上做對齊。
- TableLayout 的子元素無法跨列或跨欄,因為它不能明確指定佔幾列幾欄。

GridLayout 子元素常用屬性:

android:layout_row:固定顯示在第幾列
android:layout_column:固定顯示在第幾欄
android:layout_rowSpan:跨幾列
android:layout_columnSpan:跨幾欄



    

四、PopupMenu

PopupMenu 是彈出式選單,它會錨定在某個控件的下方顯示。

先在 menu 資源目錄建立選單檔:



    
    
    
    
    

顯示它:

public void openPopupMenu(View view) {
    // 第二個參數決定選單錨定在哪個 view 下方
    PopupMenu popupMenu = new PopupMenu(this, view);
    popupMenu.inflate(R.menu.popup_menu);
    popupMenu.show();
}

五、TextureView

TextureView 是 SurfaceView 的補充。它不像 SurfaceView 那樣另開一個特殊視窗,而是建立一個普通的 View,因此可以自由套用平移、旋轉、動畫與透明度等變換。

它的用途是顯示內容流,內容可以是影片或 OpenGL 場景,來源可以是應用自身進程,也可以是遠端進程。但有個前提:TextureView 只能用在開啟硬體加速的視窗上,若渲染走軟體,它什麼都不會畫。

使用方式很簡單——取得它的 SurfaceTexture,再把內容呈現上去。以下示範把相機預覽渲染到 TextureView(因為用到相機,記得在 manifest 補上權限):

public class MainActivity extends AppCompatActivity
        implements TextureView.SurfaceTextureListener {

    private TextureView mTexture;
    private Camera mCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTexture = (TextureView) findViewById(R.id.texture_view);
        // 設定表面紋理監聽器
        mTexture.setSurfaceTextureListener(this);
    }

    /** SurfaceTexture 準備好可用 */
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open();
        try {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
            mTexture.setAlpha(1.0f);
            mTexture.setRotation(90.0f);
        } catch (IOException ioe) {
            // 處理例外
        }
    }

    /** SurfaceTexture 尺寸改變 */
    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    }

    /** SurfaceTexture 即將銷毀 */
    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    /** SurfaceTexture 內容更新 */
    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }
}

版面檔中的定義:

注意:由於相機預覽的方向與螢幕不一致,通常需要把 TextureView 旋轉 90 度才是正常視角。

六、組合起來看效果

把這五個控件放進同一個 Activity 之後,操作順序大致如下。

程式初始畫面:

打開 Switch 開關後,點擊「打開PopupMenu」彈出選單:

接著是 TextureView 旋轉 0 度(不旋轉)、45 度、90 度三種情況:


對比這三張圖最容易看出 TextureView 與 SurfaceView 的差異所在:因為它本質上就是一個普通 View,變換矩陣可以直接套上去,畫面內容跟著一起轉;若換成 SurfaceView,由於它擁有一層獨立視窗,做旋轉或透明度動畫時常會出現內容與變換不同步的情形。這也是後來 Google 在多數場景下更推薦 TextureView 的原因。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved