Flex 实现Android图案解锁功能

看见Andorid系统里面有图案解锁的功能,试想能用Flex的移动开发实现吗?答案是:完全可以!

环境:Flex 4.6(air3.2)

先看我的包结构:

第一视图Sample_Locked2View:

View Code

<?xml version="1.0" encoding="utf-8"?>
<!--TEST APPLICATION 's first view-->
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="主页视图"xmlns:view="com.yyf.view.*" actionBarVisible="false"><s:layout><s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/></s:layout><fx:Script><![CDATA[import com.yyf.model.MainDataModel;import com.yyf.util.Tools;[Bindable]private var mainModel:MainDataModel = MainDataModel.getInstance();]]></fx:Script><view:LockedView id="lock" horizontalCenter="0" verticalCenter="0" creationComplete="mainModel.hotArea = (new Tools()).getHotArea(lock);"/><s:Label id="notice" text="Your Password : {mainModel.password}" visible="{mainModel.successCreated}"/><!--display the HOT_AREA-->
<!--    <s:Label backgroundAlpha="0.3" backgroundColor="0xFED913" x="{mainModel.hotArea.min_x}" y="{mainModel.hotArea.min_y}"width="{mainModel.hotArea.max_x - mainModel.hotArea.min_x}"height="{mainModel.hotArea.max_y - mainModel.hotArea.min_y}"/>-->
</s:View>

核心逻辑类Circle,用于处理绘图相关计算:

View Code

package com.yyf.element
{import com.yyf.event.PasswordCreateEevent;import com.yyf.model.MainDataModel;import com.yyf.util.Config;import com.yyf.view.LockedView;import flash.display.BitmapData;import flash.display.Sprite;import flash.events.FocusEvent;import flash.events.MouseEvent;import flash.geom.Point;import mx.core.FlexGlobals;import mx.core.UIComponent;import mx.events.FlexEvent;import spark.components.Application;/***<br>Circle UI</br>* @author yao yf* 2012-04-19* */public class Circle extends UIComponent{/**radius of the circle**/public var radius:Number;/**color of the circle**/public var color:uint;/**index ofd the circle**/public var index:int = 0;/*** @private**///selected colorprivate var selectedColor:uint;//current Circle targetprivate var target:Circle;//lineprivate var line:Line = Line.getInstance();//temp lineprivate var linetmp:LineTemp = LineTemp.getInstance();//current start point for draw temp lineprivate var currentStaPoint:Point = new Point();//has excute the FUNCTION  drawLine()private var hasLine:Boolean = false;//has excute the FUNCTION circleChange()private var hasCircleChanged:Boolean = false;//model dataprivate var mainModel:MainDataModel = MainDataModel.getInstance();//main applicationprivate static const MAIN_APP:Application = FlexGlobals.topLevelApplication as Application;/*** Constructor* @param radius:Number* @param color:uint* **/public function Circle(radius:Number,color:uint){super();this.radius = radius;this.color = color;this.draw();addListener();mainModel.notice = Config.DEFUALT;}//drawprivate function draw():void{this.graphics.clear();this.graphics.beginFill(color);this.graphics.drawCircle(this.radius,this.radius,this.radius);}//add Event Listenerprivate function addListener():void{this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);}//onMouseDown Handlerprivate function onMouseDown(event:MouseEvent):void{    mainModel.notice = Config.DURING;target = event.target as Circle;circleChange(target);currentStaPoint = new Point(target.x + radius,target.y + radius);mainModel.dispatched = false;MAIN_APP.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);MAIN_APP.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);}//onMouseMove Handlerprivate function onMouseMove(event:MouseEvent):void{target = event.target as Circle;if(target){circleChange(target);currentStaPoint = new Point(target.x + radius,target.y + radius);}else{if(event.target is Application)return;//                var stagePoint:Point = new Point(event.stageX,event.stageY);//                trace(stagePoint.x+" =,= "+stagePoint.y);
                drawTmpLine(target);}}//onMouseUp Handlerprivate function onMouseUp(event:MouseEvent):void{MAIN_APP.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);MAIN_APP.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);mainModel.enable = false;linetmp.clear();if(!mainModel.dispatched){mainModel.dispatchEvent(new PasswordCreateEevent(PasswordCreateEevent.EVT_PASSWORD_CREATED,mainModel.password));}mainModel.dispatched = true;}//circleChange//add a orange small circle on this centerprotected function circleChange(target:Circle):void{if(!target)return;if(!target.hasCircleChanged){target.graphics.beginFill(0xFF5D18);target.graphics.drawCircle(this.radius,this.radius,this.radius/2);if(!target.hasLine){drawLine(target);}target.hasCircleChanged = true;}}/*** draw line path with mouse move* @param target:Circle* **/protected function drawLine(target:Circle):void{linetmp.clear();line.graphics.lineStyle(Line.THINKNESS,Line.COLOR);var c_point:Point;if(target == this){//                c_point = localToGlobal(new Point(this.x,this.y));line.graphics.moveTo(this.x + radius,this.y + radius);}else{//                c_point = localToGlobal(new Point(target.x,target.y));line.graphics.lineTo(target.x + radius,target.y + radius);}target.hasLine = true;mainModel.password += target.index + "";}/*** draw temp line path with mouse move* @param target:Circle* @param stagePoint:Point* **/protected function drawTmpLine(target:Circle):void{//            var currentMovPoint:Point = localToGlobal(new Point(owner.mouseX,owner.mouseY));//            trace(currentMovPoint.x + " =,= " + currentMovPoint.y);//            if(currentMovPoint.x > mainModel.hotArea.min_x && currentMovPoint.x < mainModel.hotArea.max_x //                && currentMovPoint.y > mainModel.hotArea.min_y && currentMovPoint.y < mainModel.hotArea.max_y)//            {//            trace(owner.mouseX + " , " + owner.mouseY);//            trace(owner.width + " , " + owner.height);if(owner.mouseX > 6 && owner.mouseX < (owner.width - 6)&& owner.mouseY > 6 && owner.mouseY < (owner.height - 6)){linetmp.clear();linetmp.graphics.lineStyle(Line.THINKNESS,Line.COLOR);linetmp.graphics.moveTo(currentStaPoint.x , currentStaPoint.y);linetmp.graphics.lineTo(owner.mouseX,owner.mouseY);}}//get color String from ower pixelprivate function getColorString():String{var tmpDta:BitmapData = new BitmapData(owner.width, owner.height);tmpDta.draw(owner);selectedColor = tmpDta.getPixel(owner.mouseX,owner.mouseY);var colorDisplay:String = selectedColor.toString(16).toLocaleUpperCase();if(colorDisplay.length < 6){switch(colorDisplay.length){case 1 : colorDisplay = "00000" + colorDisplay;break;case 2 : colorDisplay = "0000" + colorDisplay;break;case 3 : colorDisplay = "000" + colorDisplay;break;case 4 : colorDisplay = "00" + colorDisplay;break;case 5 : colorDisplay = "0" + colorDisplay;break;default : break;}}return colorDisplay;}}
}

线对象Line(SingleTon):

View Code

package com.yyf.element
{import mx.core.UIComponent;/***<br>Line UI</br>* SingleTon* @author yao yf* 2012-04-19* */public class Line extends UIComponent{private static var model:Line;public static const THINKNESS:Number = 25;public static const COLOR:uint = 0xFF5D18;public function clear():void{this.graphics.clear();}public function Line(singletonclass:SingleTonClass){if(model != null){throw new Error("SingleTon!");}}public static function getInstance():Line{if(model == null){model = new Line(new SingleTonClass());}return model;}}
}class SingleTonClass{}

临时线对象LineTemp(SingleTon)此对象为绘制过程中的临时线路径,绘制完成后将被清除:

View Code

package com.yyf.element
{import mx.core.UIComponent;/***<br>Temp Line UI</br>* SingleTon* @author yao yf* 2012-04-19* */public class LineTemp extends UIComponent{private static var model:LineTemp;public static const THINKNESS:Number = 25;public static const COLOR:uint = 0xFF5D18;public function clear():void{this.graphics.clear();}public function LineTemp(singletonclass:SingleTonClass){if(model != null){throw new Error("SingleTon!");}}public static function getInstance():LineTemp{if(model == null){model = new LineTemp(new SingleTonClass());}return model;}}
}class SingleTonClass{}

核心视图类LockedView,用于呈图案解锁图面板:

View Code

<?xml version="1.0" encoding="utf-8"?>
<!--
Locked Component
==========================
Explain:custom component for Locked
==========================
Code by yyf 2012-04-19
-->
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"width="275" height="350"creationComplete="creationCompleteHandler(event)"><fx:Script><![CDATA[import com.yyf.element.Circle;import com.yyf.element.Line;import com.yyf.element.LineTemp;import com.yyf.event.PasswordCreateEevent;import com.yyf.model.MainDataModel;import com.yyf.util.Config;import mx.collections.ArrayCollection;
//            import mx.controls.Alert;import mx.events.FlexEvent;/*** @prvate* **/private const RADIUS:Number = 30;private const COLOR:uint = 0xFED913;//store the Circlesprivate static var circleData:ArrayCollection;//line pathprivate var line:com.yyf.element.Line = com.yyf.element.Line.getInstance();//temp line pathprivate var linetmp:LineTemp = LineTemp.getInstance();//model data[Bindable]private var mainModel:MainDataModel = MainDataModel.getInstance();/**init**/private function creationCompleteHandler(event:FlexEvent):void{mainModel.addEventListener(PasswordCreateEevent.EVT_PASSWORD_CREATED,onCreated);displayCircle();}/**display nine Circles**/private function displayCircle():void{circleData = new ArrayCollection();line.includeInLayout = false;linetmp.includeInLayout = false;this.circles.addElement(line);this.circles.addElement(linetmp);for(var i:int = 0 ; i < 9 ; i++){var c:Circle = new Circle(RADIUS,COLOR);c.index = (i+1);this.circles.addElement(c);circleData.addItem(c);}}/**remove all of the circles group's elements**/private function removeCircle():void{circleData.removeAll();circleData.refresh();circleData = null;var i:int = this.circles.numElements;while(i >= 1){this.circles.removeElementAt(0);i--;}}/**RESET button handler**/private function reset_clickHandler(event:MouseEvent):void{mainModel.successCreated = false;mainModel.enable = true;mainModel.password = "";line.clear();linetmp.clear();removeCircle();displayCircle();}/**GO buton handler**/private function go_clickHandler(event:MouseEvent):void{
//                Alert.show("Your Password : " + mainModel.password);trace("Your Password : " + mainModel.password);}//PasswordCreateEevent.EVT_PASSWORD_CREATED call back handlerprivate function onCreated(event:PasswordCreateEevent):void{var pw:String = event.password;if(pw.length < 4){mainModel.notice = Config.ERROR;mainModel.successCreated = false;trace("Your Password's lenth can not less than 4 !");}else{mainModel.notice = Config.SUCCESS;mainModel.successCreated = true;}}]]></fx:Script><fx:Declarations></fx:Declarations><s:VGroup width="100%" height="100%" horizontalAlign="center" verticalAlign="middle" paddingBottom="5" paddingTop="5" paddingLeft="5" paddingRight="5"><s:Label id="noticelbl" text="{mainModel.notice}"/><s:Line xFrom="0" xTo="260"><s:stroke><s:SolidColorStroke color="{COLOR}" weight="2" caps="square"/></s:stroke></s:Line><s:Group id="circles" width="100%" height="100%" enabled="{mainModel.enable}"><s:layout><s:TileLayout horizontalGap="90" verticalGap="90" requestedColumnCount="3" requestedRowCount="3"paddingLeft="10" paddingTop="10"/></s:layout></s:Group><s:Line xFrom="0" xTo="260"><s:stroke><s:SolidColorStroke color="{COLOR}" weight="2" caps="square"/></s:stroke></s:Line><s:HGroup height="35"><s:Button id="reset" label="Reset" width="70" height="35" click="reset_clickHandler(event)"/><s:Button id="go" label="Go" width="70" height="35" click="go_clickHandler(event)" enabled="{mainModel.successCreated}"/></s:HGroup></s:VGroup>
</s:BorderContainer>

下面是效果图:

废话少说,本人一贯作风——崇尚开源,附源码下载地址:

http://files.cnblogs.com/loveFlex/Sample_Locked2.rar

欢迎大家留言~

转载于:https://www.cnblogs.com/loveFlex/archive/2012/04/24/2467564.html

Flex【原创】模拟Android图案解锁相关推荐

  1. android 解锁图案代码,Android图案解锁code.docx

    Android图案解锁code Main_Acitivity.javapackage com.example.lackpatternview;import android.os.Bundle;impo ...

  2. 快给你的app上锁吧(android图案解锁)

    序言:前两天因为项目的原因,去做了一下仿ios的数字解锁功能,然后写了那篇快给你的app上锁吧(android数字解锁),后来想到应用中常见的还有另外一种解锁就是绘制图案解锁,这两种解锁的布局看起来是 ...

  3. Android 图案解锁 9宫格密码解锁

    序言  第一次写Android技术博客,不知道该如何下手. 背景  现在人们越来越重视自己的隐私,对于一些涉及用户隐私的应用,用户可能会希望在应用启动时必须先输入密码.传统的数字式密码记忆繁琐.容易破 ...

  4. android图案解锁功能的实现

    我们经常会在app中看到图案解锁的功能,所以寻思做一个,在某客视频上看到了教程,自己跟着做了一遍,记录一下,顺便理清一下思路. 思路讲解: 首先自定义一个图案的view,其中实现onDraw方法,以及 ...

  5. Android 图案解锁

    首先还是看效果图. 图案解锁的功能在许多应用中都有用过,它比起数字解锁,带给用户的体验要好,今天就来一步一步实现这个功能. 一.初始化 初始化放在onDraw方法中,因为onDraw方法在绘制过程中会 ...

  6. android 图案解锁忘记了,安卓手机忘记图形解锁、锁屏密码的解决方法

    Android 手机的图形解锁倒是真的好用了,主要是方便新颖,并且便于记忆,自从有了图形解锁,很多人都不再使用密码屏幕锁了,图形解锁倒是好玩,但是经常换来换去的话就会造成一时间想不起哪个图形解锁图案才 ...

  7. android图案解锁忘了怎么解,安卓手机解锁图案忘了怎么办?手机解锁密码忘了的解决办法...

    现在的智能手机解锁方法有很多,有的朋友喜欢几天换一个密码.但是有的人就会把自己设的手机解锁图案密码忘记了,今天小编就带着大家一起解决这个问题,赶紧和小编一起看看吧 一共有三种办法: 1.双清(恢复出厂 ...

  8. android图案解锁忘了怎么解,手机图案解锁忘了怎么办 三种方法轻松解决【图文】...

    随着智能机的普及,手机上锁方法也有了新的方式,除了以前传统的密码锁之外,手机还添加了极富趣味的图案锁.九宫格形式的图案锁屏,看上去不仅新颖,而且锁屏更加方面,这种锁屏很快就受到不少用户的喜爱!可是在使 ...

  9. android 图案解锁密码,关于Android手机图案解锁总密码数,个人写的一个递归版求解...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 没有注释,测试运行一下就行了,大部分人还是能看懂的. import java.util.Vector; public class TestLock { p ...

最新文章

  1. java类的修饰词有哪些_Java类与对象及访问控制修饰词解析
  2. router OS (ROS)命令中文手册
  3. 下载Nacos源码并运行
  4. h5移动端设备像素比dpr介绍
  5. 音频剪切_音频编辑入门指南:剪切,修剪和排列
  6. Linux问题分析或解决_samba无法连接
  7. 【算法学习】整体二分
  8. cognos10 安装部署
  9. 解决:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal
  10. Linux—程序包安装与管理
  11. eclipse svn 忽略 target/.project /.classpath /.settings等 目录
  12. 博文视点大讲堂第18期:从草根到巨人——互联网时代的LAMP开源架构
  13. 转:集群、分布式、负载均衡区别与联系
  14. My first App Encrypt Wheel is Ready to Download!
  15. apache tuscany(一)
  16. html5妇女节游戏,2020三八妇女节趣味游戏大全_妇女节可以举办哪些活动
  17. PS给人物添加阴影和高光
  18. VS+Qt应用开发-设置软件图标
  19. php java扩展模块_php扩展模块装安装
  20. Error: docker-ce conflicts with 2:docker-1.13.1-209.git7d71120.el7.centos.x86_64

热门文章

  1. PID控制器的传递函数推导
  2. 基于python的音乐数据分析_Python对QQ音乐进行爬取并进行数据分析
  3. 关于itouch/ipad 等设备的飞行模式
  4. Qt5教程(九):Qt多线程
  5. 【transformer】【pytorch】DeiT的数据增强
  6. 对于ALV LVC 单元格的控制
  7. 瓶口分配器的安装使用与维护技巧大公开
  8. 为什么能在国际学校里发展是从面试中开始呢?
  9. 流浪宠物救助网站前端页面_基于SSM(spring+spring mvc+mybatis)开发流浪宠物(猫狗)救助系统,项目为maven项目,后台可配置化,系统可学习性高。...
  10. 如何进行有效的会议管理