Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 06:04, 1 August 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Nokia SDK 2.0 for Java: 如何实现横竖屏切换

From Wiki
Jump to: navigation, search
WP Metro Icon Chinese.png
Article Metadata

兼容于
文章
Amazing110 在 21 Jun 2012 创建
最后由 hamishwillee 在 01 Aug 2013 编辑


Contents

Introduction

在Nokia SDK 2.0 for Java平台的Series 40手机(Nokia 3050, 3060 等)中首次引入了方向传感器,也提供开发用来侦测,控制程序方向的API。

这些API被放在了com.nokia.mid.ui.orientation 中, 通过他们你可以:

  • 改变程序的方向。
  • 侦测当前程序的方向。
  • 当显示方向改变时,获取到一个系统通知。


但是需要注意,当一个MIdlet应用的横竖屏方向改变时,它必须重新redraw所使用到的低级UI组件,然而对于高级UI组件都是自动重画的,不需要我们担心。

Nokia Orientation API 支持下面四种显示方向:

  • ORIENTATION_LANDSCAPE

Full touch orientation landscape.jpg

  • ORIENTATION_LANDSCAPE_180
    Full touch orientation landcape 180.jpg
  • ORIENTATION_PORTRAIT
    Full touch orientation portrait.jpg
  • ORIENTATION_PORTRAIT_180
  • Full touch orientation portrait 180.jpg


需要注意的是,在Nokia3050 等2.0SDK 的手机中仅仅支持的是ORIENTATION_LANDSCAPE 和ORIENTATION_PORTRAIT 两种方向。

相关API

Header text Header text
OrientationListener 横竖屏切换监听接口, displayOrientationChanged(int newDisplayOrientation)
Orientation 用来获取,设置程序的UI方向,主要函数有:getAppOrientation() ,setAppOrientation()



如何使用

  • 1. 首先我们必须判断当前手机是否支持Orientation API,那么怎么来判断呢?

我们可以通过系统属性"com.nokia.mid.ui.orientation.version" 的返回值是否null来判断,一般来说,该API是在Nokia UI API 1.6以后才引入的,使用时请注意。

  • 2.默认情况下,Orientation相关属性是关闭,要使用它我们必须在JAD文件中添加如下属性:

Nokia-MIDlet-App-Orientation: manual

manual: 当程序启动后,它的UI方向会跟随手机的方向 而对应变化。

Nokia-MIDlet-App-Orientation NB.png


  • 3. 注册OrientationListener以便监听手机方向变化。

Orientation.addOrientationListener(orientationListener); 是用上面的方法进行注册。

  • 4. 如何处理Orientation改变事件。

示例代码:

  public void displayOrientationChanged( int newDisplayOrientation ){
 
/** Check display orientation */
switch( newDisplayOrientation ){
case Orientation.ORIENTATION_PORTRAIT:
case Orientation.ORIENTATION_PORTRAIT_180:
/** Change MIDlet UI orientation to portrait */
Orientation.setAppOrientation(Orientation.ORIENTATION_PORTRAIT);
break;
 
case Orientation.ORIENTATION_LANDSCAPE:
case Orientation.ORIENTATION_LANDSCAPE_180:
/** Change MIDlet UI orientation to landscape */
Orientation.setAppOrientation(Orientation.ORIENTATION_LANDSCAPE);
break;
}
}


可以从上面代码看出我们使用Orientation.setAppOrientation(Orientation.ORIENTATION_PORTRAIT) 来改变MIDlet的显示方向。


示例代码-1

The following code example shows how to use the Orientation API to handle the display orientation changes for a CustomItem.


下面的代码演示了如何使用Orientation API 在一个CustomItem中处理显示方向改变事件:

import com.nokia.mid.ui.orientation.Orientation;
import com.nokia.mid.ui.orientation.OrientationListener;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.CustomItem
 
public class CustomItemOrientationChanges extends CustomItem implements OrientationListener{
 
public CustomItemOrientationChanges(){
super(“CustomItemOrientationTest”);
Orientation.addOrientationListener( this );
}
 
/** Orientation listener callback */
public void displayOrientationChanged( int newDisplayOrientation ){
 
/** Check display orientation */
switch( newDisplayOrientation ){
case Orientation.ORIENTATION_PORTRAIT:
case Orientation.ORIENTATION_PORTRAIT_180:
/** Change MIDlet UI orientation to portrait */
Orientation.setAppOrientation(Orientation.ORIENTATION_PORTRAIT);
break;
 
case Orientation.ORIENTATION_LANDSCAPE:
case Orientation.ORIENTATION_LANDSCAPE_180:
/** Change MIDlet UI orientation to landscape */
Orientation.setAppOrientation(Orientation.ORIENTATION_LANDSCAPE);
break;
}
}
 
/** This is called by Java platform when the UI orientation has been changed */
protected void sizeChanged(int w, int h){
/** handle new dimensions */
}
 
/** Inherited from CustomItem */
protected int getMinContentHeight(){
 
/** CustomItem height depends on the app orientation */
if ( Orientation.getAppOrientation() == Orientation.ORIENTATION_PORTRAIT ){
/** return height for portrait layout */
}else{
/** return height for landscape layout */
}
}
 
/** Inherited from CustomItem */
protected int getMinContentWidth(){
 
/** CustomItem width depends on the app orientation */
if ( Orientation.getAppOrientation() == Orientation.ORIENTATION_LANDSCAPE ){
/** return width for landscape layout */
}else{
/** return width for portrait layout */
}
}
 
/** Inherited from CustomItem */
protected int getPrefContentHeight( int width ){
return getMinContentHeight();
}
 
/** Inherited from CustomItem */
protected int getPrefContentWidth( int height ){
return getMinContentWidth();
}
 
/** Inherited from CustomItem */
protected void paint( Graphics g, int width, int height ){
/** Render screen content */
}
}


相关链接

This page was last modified on 1 August 2013, at 06:04.
405 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×