search
HomeDatabaseMysql Tutorial浅谈Adobe Air滑移及其优化

Adobe Air是Adobe公司用来争夺移动设备应用开发的一个重要武器。移动设备的一个重要体验是手指滑移,但Adobe并没有为Air推出官方滑移的SDK。 所以,若打算用Air来开发移动应用,则会碰到滑移这个问题。以下是对基于Air滑移的一些基本原理及其优化方案的讨论

Adobe Air是Adobe公司用来争夺移动设备应用开发的一个重要武器。移动设备的一个重要体验是手指滑移,但Adobe并没有为Air推出官方滑移的SDK。

所以,若打算用Air来开发移动应用,则会碰到滑移这个问题。以下是对基于Air滑移的一些基本原理及其优化方案的讨论,本文最后会给出一个示例。

基本原理

scroll view

上图说明了滑移的显示列表。

ScrollBg是整个滑移的背景,手指在此进行操作,它是侦听手指滑移事件的目标

ScrollTarget是需要进行滑移的对象,其父为ScrollBg。p1,p2和p3为内部的显示对象。

ScrollMask是滑移对象的mask,用于对滑移对象形成遮罩,其父为ScrollBg。

综上,我们需要在ScrollBg上侦听用户手指事件,对ScrollTarget进行滑移,在滑移时我们只能看到被ScrollMask遮罩的那部分。

    在Adobe Air里,MouseEvent等同于手指触摸的TouchEvent,而只有在需要多点触控时,TouchEvent才有其特有的优势。所以我们侦听MouseEvent即可以侦听用户的手指操作。

  • 手指按下处理

  • 通常情况下,手指按下是整个滑移过程的开始。我们可以侦听MouseDown事件来模拟手指按下。

    在MouseDown侦听器里我们还要添加一系列事件侦听器,来构成整个滑移过程的侦听。

    我们需要知道用户的手指移动,于是需要侦听MouseMove事件。我们还需要知道用户手指的释放,于是需要侦听RollOut和MouseUp事件。此外,我们还可能需要侦听EnterFrame事件,来做一些内部的更新逻辑。

    综上,我们在MouseDown侦听器里处理滑移的开始,并为手指移动,手指释放及每帧更新等事件添加侦听器

  • 手指滑移处理

  • 在MouseMove事件侦听器里,我们需要让滑移对象跟随玩家的手指进行移动。

    所以在添加MouseMove侦听器之前,我们需要事先记录滑移对象当前的位置信息(诸如mouseX,mouseY),并在MouseMove侦听器对滑移对象的位置进行记录,通过两个位置的差异,我们可以计算出滑移对象需要移动的距离。

    若需要移动的距离在合理的范围之内(如:不超过边界值的一半大小),我们将对滑移对象的位置进行改变,并进行更新。

    有时候,我们可能会对滑移对象内的对象添加点击事件,但是我们不希望在滑移释放手指的时候触发这些点击事件。这时候我们可以在MouseMove侦听器里通过计算滑移对象的移动距离,来判断是否应该触发点击事件。

    比如若用户的手指只移动了1-2个像素,用户很可能只是希望点击,而非滑移,所以此时我们不进行处理;但是用户的手指移动了10个以上像素,则我们可以认为用户是在进行滑移操作,此时我们将滑移对象的mouseChildren和mouseEnabled属性禁用,则点击事件则不会在滑移时触发了。

    综上,我们在MouseMove侦听器里记录手指的位置,更新滑移对象的位置以跟随手指移动,做一些特殊处理以避免滑移时让用户触发点击事件

  • 手指离开处理

  • 我们可以将RollOut事件和MouseUp事件视为用户手指离开了滑移触发区域,从而为这两个事件添加一个共同的侦听器MouseLeaveHandler。

    在ios和android的原生滑移里,当用户滑移时手指离开屏幕,系统会对滑移对象进行自动滑移,这类似于物理中的惯性。

    所以我们需要对滑移对象进行一个类似“惯性”的缓动,并在缓动结束的时候,做一些滑移结束的处理。

    同时释放MouseMove和MouseLeaveHandler的侦听,并重新添加MouseDown事件的侦听,来侦听新的滑移。

    综上,我们在MouseLeaveHandler里移除MouseMove和手指移开的事件侦听,并对滑移对象做一个”惯性”的缓动,在缓动结束时,做滑移结束的处理。

优化方案

相对于其他移动开发的引擎,Flash的渲染效率较低,在弱CPU的移动设备上体现尤其明显。

滑移需要移动大块显示区域,可能会造成卡顿和跳帧,所以优化是必须要进行的一项工作。

以下是一些我在实践总结出来的优化方案。

  • 切换为GPU模式

  • 经试验,将大面积滑移从cpu模式切换到gpu模式后,滑移的效率显著的提升了,基本可以达到原生滑移的效果。 只需要修改-app.xml里面的
gpu
GPU模式的优点是处理大块渲染区域移动的效率会显著提升。 缺点是不能使用滤镜,不能使用混合模式,在某些android机型(基本是低端机)上可能会跑不起来。 其中滤镜可以用位图替代,但是可能会带来内存上升的问题。
  • 在滑移时降低显示质量

  • 相对于PC屏幕,移动设备屏幕的ppi值要高很多。这就意味着,当我们在移动设备上降低显示质量时,实际感觉画质效果并没有PC上丢失的那么多。 所以在滑移开始时,我们可以先降低舞台显示质量。

    stage.quality = StageQuality.LOW;

    等滑移结束时,再恢复舞台显示质量。

    此方案带来的效率提升也是显著的。

    但是会显著的降低显示质量,所以需要在效率和效果方面做取舍。

  • 减少渲染区域

  • 减少渲染区域可以从根本上解决渲染压力的问题。

    实际上,在滑移时,有许多区域都在遮罩区域之外,但这些人眼不可见的区域的位置仍然会被CPU计算,从而浪费CPU周期,降低了效率。

    所以,我们可以在EnterFrame事件里,将在遮罩区域之外的滑移对象的visible设为false。从而避免了它们占用CPU周期。

    以下代码片段说明以上的方法。

    var i:int;
    var numChildren:int = _target.numChildren;
    var child:DisplayObject;
    for(i = 0; i  _bounds.bottom + MAX_VISIBLE_DIST )
    		{
    			child.visible = false;
    		}
    		else
    		{
    			child.visible = true;
    		}
    	}
    	else if( _direct == ScrollDirection.HORIZONTAL || _direct == ScrollDirection.BOTH )
    	{
    		if( child.x + child.width + _target.x  _bounds.right + MAX_VISIBLE_DIST )
    		{
    			child.visible = false;
    		}
    		else
    		{
    			child.visible = true;
    		}
    	}
    }
  • 尽可能使用位图而非矢量

  • 矢量需要CPU实时进行计算,所以尽可能避免在移动设备上使用矢量,尽可能用位图来替代。
  • 减少事件侦听

  • 事件侦听会消耗帧时间,而且不能恰当的销毁事件侦听会带来恐怖的内存泄露。 所以尽可能减少不必要的事件侦听。 比如在滑移时,将目标的mouse相关属性禁用,等待滑移结束时再启用。

    示例

    示例:http://wuzhiwei.net/opensrc/ScrollAs3/ScrollAs3.html
    源码:https://github.com/iWoz/AirScroll
    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

    Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

    How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

    The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

    What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

    The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

    How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

    The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

    What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

    MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

    What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

    MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

    How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

    Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

    How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

    TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.