官术网_书友最值得收藏!

Verifying specific gesture support for common interactions

When dealing with Android devices, touch and gestures are the main mechanisms with which the user interacts with the device. If we want to use some of the predefined gestures in Flash Player and AIR, we can do so in the following manner.

How to do it...

To discover which specific gestures are supported on a device, perform the following actions:

  1. First, import the following classes into your project:
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;
    
  2. Declare a TextField and TextFormat object to allow visible output upon the device:
    private var traceField:TextField;
    private var traceFormat:TextFormat;
    
  3. We will now set up our TextField, apply a TextFormat, and add it to the DisplayList. Here, we create a method to perform all of these actions for us:
    protected function setupTextField():void {
    traceFormat = new TextFormat();
    traceFormat.bold = true;
    traceFormat.font = "_sans";
    traceFormat.size = 44;
    traceFormat.align = "center";
    traceFormat.color = 0x333333;
    traceField = new TextField();
    traceField.defaultTextFormat = traceFormat;
    traceField.selectable = false;
    traceField.mouseEnabled = false;
    traceField.width = stage.stageWidth;
    traceField.height = stage.stageHeight;
    addChild(traceField);
    }
    
  4. Set the specific input mode for the multitouch APIs to support gestures with the following command:
    Multitouch.inputMode = MultitouchInputMode.GESTURE;
    
  5. Invoking Multitouch.supportedGestures will return a Vector of String objects naming all the supported gestured exposed to Flash on the device:
    var supportedGestures:Vector.<String> = Multitouch.supportedGestures;
    
  6. We can then look for a specific gesture or set of gestures to listen for, or fall back to other interaction events if necessary.
    for(var i:int=0; i < supportedGestures.length; ++i) {
    trace(supportedGestures[i]);
    }
    
  7. We can perform all of these necessary functions within a single method:
    protected function checkGestures():void {
    Multitouch.inputMode = MultitouchInputMode.GESTURE;
    if(Multitouch.supportedGestures){
    var supportedGestures:Vector.<String> = Multitouch.supportedGestures;
    for(var i:int=0; i <supportedGestures.length; ++i) {
    traceField.appendText(supportedGestures[i] + "\n");
    }
    }else{
    traceField.appendText("no gesture support!");
    }
    }
    
  8. The result will appear similar to the following:

How it works...

Flash player and AIR do a marvelous job of distilling information to essential details for an Android developer. Knowing which particular gestures are supported on a device will allow us to tailor event interactions on our applications and provide fallback interactions when necessary.

There's more...

In our example class, we also provide a check to be sure there are at least some gestures supported through Multitouch.supportedGestures. Chances are, if the device does provide gesture support, we will want to provide a warning to the user explaining that the application will not perform optimally because of hardware limitations.

Apart from the more common gestures such as zoom, swipe, rotate, and pan, which are included in the flash.events.TransformGestureEvent package, there are additional, yet less common gestures such as two-finger tap, found in the flash.events.GestureEvent and flash.events.PressAndTapGestureEvent classes. These will all be referenced by Multitouch.supportedGestures if available on the device.

主站蜘蛛池模板: 松潘县| 耒阳市| 娱乐| 新乡县| 无为县| 吴江市| 香港| 治县。| 西林县| 南皮县| 衡东县| 克什克腾旗| 淮阳县| 启东市| 西乌珠穆沁旗| 兴业县| 精河县| 怀远县| 定陶县| 灵川县| 宿迁市| 兴宁市| 横峰县| 岱山县| 报价| 沙雅县| 泾川县| 海宁市| 马龙县| 讷河市| 河间市| 昌都县| 宜城市| 红原县| 马龙县| 临湘市| 布拖县| 六盘水市| 象山县| 宁德市| 琼中|