`

【翻译】(72)硬件加速

 
阅读更多

【翻译】(72)硬件加速

 

see

http://developer.android.com/guide/topics/graphics/hardware-accel.html

 

原文见

http://developer.android.com/guide/topics/graphics/hardware-accel.html

 

-------------------------------

 

Hardware Acceleration

 

硬件加速

 

-------------------------------

 

In this document

 

本文目录

 

* Controlling Hardware Acceleration 控制硬件加速

* Determining if a View is Hardware Accelerated 确定一个View是否被硬件加速

* Android Drawing Models Android绘画模型

* Software-based drawing model 基于软件的绘画模型

* Hardware accelerated drawing model 硬件加速绘画模型

* Unsupported Drawing Operations 不支持的绘画操作

* View Layers 视图层

* View Layers and Animations 视图图层和动画

* Tips and Tricks 提示和技巧

 

See also

 

另见

 

* OpenGL with the Framework APIs 使用框架API的OpenGL

* RenderScript

 

-------------------------------

 

Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline is designed to better support hardware acceleration. Hardware acceleration carries out all drawing operations that are performed on a View's canvas using the GPU. Because of the increased resources required to enable hardware acceleration, your app will consume more RAM.

 

从Android 3.0(API级别11)中开始,Android的二维渲染管线被设计为更好地支持硬件加速。硬件加速使用GPU(注:图形处理器)实现执行在一个View的画布的所有绘画操作。因为需要增加的资源来使能硬件加速,你的应用将消耗更多RAM(注:Random-access memory,随机存取存储器,指存取快速但内容易失的内存)。

 

The easiest way to enable hardware acceleration is to turn it on globally for your entire application. If your application uses only standard views and Drawables, turning it on globally should not cause any adverse drawing effects. However, because hardware acceleration is not supported for all of the 2D drawing operations, turning it on might affect some of your applications that use custom views or drawing calls. Problems usually manifest themselves as invisible elements, exceptions, or wrongly rendered pixels. To remedy this, Android gives you the option to enable or disable hardware acceleration at the following levels:

 

使能硬件加速的最简单方式是全局地为你的整个应用程序打开它。如果你的应用程序只使用标准视图和Drawable,那么全局地打开它应该不会导致任何负面绘画影响。然而,因为对于二维绘画操作来说硬件加速并不全都被支持,打开它可能应先你的使用了自定义视图或绘画调用的某些应用程序。问题通常表现它自己为不可见的元素,异常,或错误渲染的像素。为了修正它,Android给你选择以在以下层面上使能或屏蔽硬件加速。

 

* Application 应用程序

* Activity 活动

* Window 窗口

* View 视图

 

If your application performs custom drawing, test your application on actual hardware devices with hardware acceleration turned on to find any problems. The Unsupported drawing operations section describes known issues with drawing operations that cannot be hardware accelerated and how to work around them.

 

如果你的应用程序执行自定义绘画,那么测试你的应用程序在打开硬件加速的实际硬件设备上以找到任何问题,不支持的绘画操作章节描述不能被硬件加速的绘画操作的已知问题以及如何避免使用它们。

 

-------------------------------

 

Controlling Hardware Acceleration

 

控制硬件加速

 

You can control hardware acceleration at the following levels:

 

你可以控制硬件加速在以下层面上:

 

* Application 应用程序

* Activity 活动

* Window 窗口

* View 视图

 

Application level 

 

应用程序层面

 

In your Android manifest file, add the following attribute to the <application> tag to enable hardware acceleration for your entire application:

 

在你的Android清单文件中,添加以下属性到<application>标签以为你的整个应用程序使能硬件加速:

 

-------------------------------

 

<application android:hardwareAccelerated="true" ...>

 

-------------------------------

 

Activity level 活动层面

 

If your application does not behave properly with hardware acceleration turned on globally, you can control it for individual activities as well. To enable or disable hardware acceleration at the activity level, you can use the android:hardwareAccelerated attribute for the <activity> element. The following example enables hardware acceleration for the entire application but disables it for one activity:

 

如果你的应用程序对全局打开的硬件加速作出不正确的行为,那么你也可以针对单独的活动来控制它。为了在活动层面上使能或屏蔽硬件加速,你可以使用用于<activity>元素的android:hardwareAccelerated属性。以下示例为整个应用程序使能硬件加速但对于一个活动却屏蔽它:

 

-------------------------------

 

<application android:hardwareAccelerated="true">

    <activity ... />

    <activity android:hardwareAccelerated="false" />

</application>

 

-------------------------------

 

Window level 

 

窗口层面

 

If you need even more fine-grained control, you can enable hardware acceleration for a given window with the following code:

 

如果你甚至需要更细粒度的控制,你可以使用以下代码对一个给定的窗口使能硬件加速:

 

-------------------------------

 

getWindow().setFlags(

    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,

    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

 

-------------------------------

 

-------------------------------

 

Note: You currently cannot disable hardware acceleration at the window level.

 

注意:你当前无法在窗口层面上屏蔽硬件加速。

 

-------------------------------

 

View level 视图层面

 

You can disable hardware acceleration for an individual view at runtime with the following code:

 

你可以在运行时用以下代码对一个单独的视图屏蔽硬件加速:

 

-------------------------------

 

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

 

-------------------------------

 

-------------------------------

 

Note: You currently cannot enable hardware acceleration at the view level. View layers have other functions besides disabling hardware acceleration. See View layers for more information about their uses.

 

注意:你当前无法在视图层面上使能硬件加速。视图层拥有除屏蔽硬件加速外的其他函数。参见视图层以获得关于它们的用途的更多信息。

 

-------------------------------

 

-------------------------------

 

Determining if a View is Hardware Accelerated

 

确定一个View是否被硬件加速

 

It is sometimes useful for an application to know whether it is currently hardware accelerated, especially for things such as custom views. This is particularly useful if your application does a lot of custom drawing and not all operations are properly supported by the new rendering pipeline.

 

对于一个应用程序,知道它是否当前是被硬件加速有时是有用的,特别对于一些东西诸如自定义视图。这特别有用,如果你的应用程序做了很多自定义绘画并且不是所有操作被新的渲染管线正确地支持。

 

There are two different ways to check whether the application is hardware accelerated:

 

存在两种不同的方式检查应用程序是否被硬件加速:

 

* View.isHardwareAccelerated() returns true if the View is attached to a hardware accelerated window.

 

* View.isHardwareAccelerated()返回true如果View被依附到一个硬件加速的窗口。

 

* Canvas.isHardwareAccelerated() returns true if the Canvas is hardware accelerated

 

* Canvas.isHardwareAccelerated()返回true如果Canvas被硬件加速

 

If you must do this check in your drawing code, use Canvas.isHardwareAccelerated() instead of View.isHardwareAccelerated() when possible. When a view is attached to a hardware accelerated window, it can still be drawn using a non-hardware accelerated Canvas. This happens, for instance, when drawing a view into a bitmap for caching purposes.

 

如果你必须在你的绘画代码中作出这个检查,请使用Canvas.isHardwareAccelerated()而非View.isHardwareAccelerated() ,当可能时。当一个视图被依附到一个硬件加速窗口时,仍可以使用一个非硬件加速的Canvas绘画它。这发生在,例如,当出于缓存目的而绘画一个视图进一个位图的时候。

 

-------------------------------

 

Android Drawing Models

 

Android绘画模型

 

When hardware acceleration is enabled, the Android framework utilizes a new drawing model that utilizes display lists to render your application to the screen. To fully understand display lists and how they might affect your application, it is useful to understand how Android draws views without hardware acceleration as well. The following sections describe the software-based and hardware-accelerated drawing models.

 

当硬件加速被使能时,Android框架利用一个新的绘画模型,它利用显示列表(注:或称为display file,是OpenGL的概念,指一组被存储或编译的图形命令,稍后会使用它们快速渲染一个三维对象或图片。不同于AS3,它是一组命令而非一组对象)来渲染你的应用程序到屏幕。为了完全理解显示列表和它们可能如何影响你的应用程序,同时也理解Android如何在没有硬件加速的情况下绘画视图是有用的。以下章节描述基于软件和硬件加速的绘画模型。

 

Software-based drawing model

 

基于软件的绘画模型

 

In the software drawing model, views are drawn with the following two steps:

 

在软件渲染模型中,视图用以下两个步骤来绘画:

 

1. Invalidate the hierarchy

 

1. 无效化层级

 

2. Draw the hierarchy

 

2. 绘画层级

 

Whenever an application needs to update a part of its UI, it invokes invalidate() (or one of its variants) on any view that has changed content. The invalidation messages are propagated all the way up the view hierarchy to compute the regions of the screen that need to be redrawn (the dirty region). The Android system then draws any view in the hierarchy that intersects with the dirty region. Unfortunately, there are two drawbacks to this drawing model:

 

每当一个应用程序需要更新它的用户界面的一部分时,它在曾经改变内容的任意视图上调用invalidate()(或它的其中一个变种)。无效化消息被一直在视图层级中向上传播以计算需要被重绘的屏幕区域(脏区域)。然后Android系统绘画层级中与脏区域相交的任意视图。不幸的是,对于这个绘画模型存在两个缺点:

 

* First, this model requires execution of a lot of code on every draw pass. For example, if your application calls invalidate() on a button and that button sits on top of another view, the Android system redraws the view even though it hasn't changed.

 

* 首先,这个模型需要在每个绘画轮次(注:传递)上执行大量代码。例如,如果你的应用程序在一个按钮上调用invalidate()而且那个按钮坐在另一个视图的上方,那么Android系统重绘该视图,即便它不曾改变。

 

* The second issue is that the drawing model can hide bugs in your application. Since the Android system redraws views when they intersect the dirty region, a view whose content you changed might be redrawn even though invalidate() was not called on it. When this happens, you are relying on another view being invalidated to obtain the proper behavior. This behavior can change every time you modify your application. Because of this, you should always call invalidate() on your custom views whenever you modify data or state that affects the view’s drawing code.

 

* 第二个问题是绘画模型可以隐藏你的应用程序中的缺陷。因为当视图与脏区域相交时Android系统重绘它们,所以一个你改变其内容的视图可能被重绘即便没有在它上面调用invalidate()。当这种情况发生时,你正在依赖于正在被无效化的另一个视图以获得正确的行为。每当你修改你的应用程序时这个行为可以改变。因此,无论你何时修改影响视图绘画代码的数据或状态,你应该总是在你的自定义视图上调用invalidate()。

 

-------------------------------

 

Note: Android views automatically call invalidate() when their properties change, such as the background color or the text in a TextView.

 

注意:Android视图自动地调用invalidate()当它们的属性改变时,诸如背景颜色或TextView中的文本。

 

-------------------------------

 

Hardware accelerated drawing model

 

硬件加速的绘画模型

 

The Android system still uses invalidate() and draw() to request screen updates and to render views, but handles the actual drawing differently. Instead of executing the drawing commands immediately, the Android system records them inside display lists, which contain the output of the view hierarchy’s drawing code. Another optimization is that the Android system only needs to record and update display lists for views marked dirty by an invalidate() call. Views that have not been invalidated can be redrawn simply by re-issuing the previously recorded display list. The new drawing model contains three stages:

 

Android系统仍然使用invalidate()和draw()请求屏幕更新并去渲染视图,但有区别地处理实际的绘画。不是立即执行绘画命令,Android系统记录它们在显示列表中,它包含视图层级的绘画代码的输出。另一个优化是Android系统只需要对于被一个invalidate()调用标记为脏的视图而记录和更新显示列表。还没有被无效化的视图可以通过重新发送之前被记录的显示列表来简单地被重绘。新的绘画模型包含三个阶段:

 

1. Invalidate the hierarchy

 

1. 无效化层级

 

2. Record and update display lists

 

2. 记录和更新显示列表

 

3. Draw the display lists

 

3. 绘画显示列表

 

With this model, you cannot rely on a view intersecting the dirty region to have its draw() method executed. To ensure that the Android system records a view’s display list, you must call invalidate(). Forgetting to do so causes a view to look the same even after changing it, which is an easier bug to find if it happens.

 

使用这个模型,你无法依赖于与脏区域相交的一个视图来让它的draw()方法执行。为了确保Android系统记录一个视图的显示列表,你必须调用invalidate()。忘记做这件事会导致即使在改变一个视图之后它看起来仍相同,这是一个较容易发现的缺陷如果它发生。

 

Using display lists also benefits animation performance because setting specific properties, such as alpha or rotation, does not require invalidating the targeted view (it is done automatically). This optimization also applies to views with display lists (any view when your application is hardware accelerated.) For example, assume there is a LinearLayout that contains a ListView above a Button. The display list for the LinearLayout looks like this:

 

使用显示列表还有利于动画性能,因为设置特定的属性,诸如alpha(注:透明度)或rotation(注:旋转),不需要无效化目标视图(它被自动完成)。这个优化还应用于带有显示列表的视图(当你的应用程序被硬件加速时的任意视图)。例如,假设存在一个LinearLayout包含一个ListView在一个Button上方。该LinearLayout的显示列表看起来像这样:

 

* DrawDisplayList(ListView)

* DrawDisplayList(Button)

 

Assume now that you want to change the ListView's opacity. After invoking setAlpha(0.5f) on the ListView, the display list now contains this:

 

现在假设你希望改变该ListView的不透明度。在ListView上调用setAlpha(0.5f)后,现在的显示列表包含它:

 

* SaveLayerAlpha(0.5)

* DrawDisplayList(ListView)

* Restore

* DrawDisplayList(Button)

 

The complex drawing code of ListView was not executed. Instead, the system only updated the display list of the much simpler LinearLayout. In an application without hardware acceleration enabled, the drawing code of both the list and its parent are executed again.

 

ListView的复杂绘画代码不被执行。取而代之,系统只更新简单得多的LinearLayout的显示列表。在一个不使能硬件加速的应用程序中,列表和它的父对象两者的绘画代码被再次执行。

 

-------------------------------

 

Unsupported Drawing Operations

 

不支持的绘画操作

 

When hardware accelerated, the 2D rendering pipeline supports the most commonly used Canvas drawing operations as well as many less-used operations. All of the drawing operations that are used to render applications that ship with Android, default widgets and layouts, and common advanced visual effects such as reflections and tiled textures are supported. The following list describes known operations that are not supported with hardware acceleration:

 

当被硬件加速时,二维渲染管线支持最普遍使用的Canvas绘画操作还有很多较少使用的操作。所有被用于渲染停靠在Android上(注:待考)的应用程序、默认部件和布局,以及一般高级可视化效果诸如反射和砖块纹理的绘画操作是支持的。以下列表描述不被硬件加速支持的已知操作。

 

* Canvas

* clipPath()

* clipRegion()

* drawPicture()

* drawPosText()

* drawTextOnPath()

* drawVertices()

* Paint

* setLinearText()

* setMaskFilter()

* setRasterizer()

 

In addition, some operations behave differently with hardware acceleration enabled:

 

另外,一些操作对于被使能的硬件加速作出不同的行为:

 

* Canvas

 

* Canvas类

 

* clipRect(): XOR, Difference and ReverseDifference clip modes are ignored. 3D transforms do not apply to the clip rectangle

 

* clipRect():异或,差分和反向差分剪裁模式被忽略。三维变换不被应用到剪裁矩形。

 

* drawBitmapMesh(): colors array is ignored

 

* drawBitmapMesh():颜色数组被忽略

 

* drawLines(): anti-aliasing is not supported

 

* drawLines():反锯齿不被支持

 

* setDrawFilter(): can be set, but is ignored

 

* setDrawFilter():可以被设置,但被忽略

 

* Paint

 

* Paint类

 

* setDither(): ignored

 

* setDither(): 被忽略

 

* setFilterBitmap(): filtering is always on

 

* setFilterBitmap():滤镜总是打开

 

* setShadowLayer(): works with text only

 

* setShadowLayer():只对于文本是工作的

 

* ComposeShader

 

* ComposeShader类

 

* ComposeShader can only contain shaders of different types (a BitmapShader and a LinearGradient for instance, but not two instances of BitmapShader )

 

* ComposeShader只可以包含不同类型的着色器(例如一个BitmapShader和一个LinearGradient,但不能是两个BitmapShader实例)

 

* ComposeShader cannot contain a ComposeShader

 

* ComposeShader无法包含一个ComposeShader

 

If your application is affected by any of these missing features or limitations, you can turn off hardware acceleration for just the affected portion of your application by calling setLayerType(View.LAYER_TYPE_SOFTWARE, null). This way, you can still take advantage of hardware acceleratin everywhere else. See Controlling Hardware Acceleration for more information on how to enable and disable hardware acceleration at different levels in your application.

 

如果你的应用程序被这些缺失特性或限制中的任意一项影响,那么你可以通过调用setLayerType(View.LAYER_TYPE_SOFTWARE, null)只对你的应用程序的被影响部分关闭硬件加速。通过这种方式,你仍可以在其它任意地方利用硬件加速。参见控制硬件加速以获得关于在你的应用程序的不同层面上如何使能和屏蔽硬件加速的更多信息。

 

-------------------------------

 

View Layers

 

视图图层

 

In all versions of Android, views have had the ability to render into off-screen buffers, either by using a view's drawing cache, or by using Canvas.saveLayer(). Off-screen buffers, or layers, have several uses. You can use them to get better performance when animating complex views or to apply composition effects. For instance, you can implement fade effects using Canvas.saveLayer() to temporarily render a view into a layer and then composite it back on screen with an opacity factor.

 

在所有版本的Android中,视图已经有能力渲染进离屏缓冲,或者通过使用一个视图的绘画缓冲,或者通过使用Canvas.saveLayer()。离屏缓冲,或图层,拥有几个用途。你可以使用它们来获得更佳性能,当动画化复杂的视图或应用组合效果。例如,你可以使用Canvas.saveLayer()实现淡出效果以临时地渲染一个视图进一个图层,然后用一个不透明度因子向后组合它在屏幕上。

 

Beginning in Android 3.0 (API level 11), you have more control on how and when to use layers with the View.setLayerType() method. This API takes two parameters: the type of layer you want to use and an optional Paint object that describes how the layer should be composited. You can use the Paint parameter to apply color filters, special blending modes, or opacity to a layer. A view can use one of three layer types:

 

从Android 3.0(API级别11)开始,你用View.setLayerType()方法拥有关于如何和何时使用图层的更多控制权。这个API传入两个参数:你希望使用的图层类型以及一个可选Paint对象,它描述图层应该如何被组合。你可以使用Paint参数来应用颜色滤镜、特殊混合模式,或者对于一个图层的不透明度。一个视图可以使用三种图层类型中的一种:

 

* LAYER_TYPE_NONE: The view is rendered normally and is not backed by an off-screen buffer. This is the default behavior.

 

* LAYER_TYPE_NONE:视图被正常渲染并且不被一个离屏缓冲支持。这是默认行为。

 

* LAYER_TYPE_HARDWARE: The view is rendered in hardware into a hardware texture if the application is hardware accelerated. If the application is not hardware accelerated, this layer type behaves the same as LAYER_TYPE_SOFTWARE.

 

* LAYER_TYPE_HARDWARE:视图被硬件渲染进一个硬件纹理,如果应用程序被硬件加速。如果应用程序不被硬件加速,那么这个图层类型的行为与LAYER_TYPE_SOFTWARE相同。

 

* LAYER_TYPE_SOFTWARE: The view is rendered in software into a bitmap.

 

* LAYER_TYPE_SOFTWARE:视图被软件渲染进一个位图。

 

The type of layer you use depends on your goal:

 

你使用的图层类型依赖于你的目的:

 

* Performance: Use a hardware layer type to render a view into a hardware texture. Once a view is rendered into a layer, its drawing code does not have to be executed until the view calls invalidate(). Some animations, such as alpha animations, can then be applied directly onto the layer, which is very efficient for the GPU to do.

 

* 性能,使用一个硬件图层类型以渲染一个视图进一个硬件纹理。一旦视图被渲染进一个图层,那么它的绘画代码不必被执行直至视图调用invalidate()。一些动画,诸如透明度动画,然后可以被直接应用到图层上,这让GPU来是非常高效的。

 

* Visual effects: Use a hardware or software layer type and a Paint to apply special visual treatments to a view. For instance, you can draw a view in black and white using a ColorMatrixColorFilter.

 

* 可视化效果:使用硬件或软件图层类型和一个Paint以应用对一个视图的特殊可视化处理(注:对待)。例如,你可以使用ColorMatrixColorFilter来把一个视图绘画成黑白色。

 

* Compatibility: Use a software layer type to force a view to be rendered in software. If a view that is hardware accelerated (for instance, if your whole application is hardware acclerated), is having rendering problems, this is an easy way to work around limitations of the hardware rendering pipeline.

 

* 兼容性:使用一个软件图层类型以强制用软件渲染一个视图。如果一个被硬件加速的视图(例如,如果你的整个应用程序被硬件加速),正存在渲染问题,那么这是一个绕过(注:暂时解决)硬件渲染管线限制的简单方式。

 

View layers and animations

 

视图图层和动画

 

Hardware layers can deliver faster and smoother animations when your application is hardware accelerated. Running an animation at 60 frames per second is not always possible when animating complex views that issue a lot of drawing operations. This can be alleviated by using hardware layers to render the view to a hardware texture. The hardware texture can then be used to animate the view, eliminating the need for the view to constantly redraw itself when it is being animated. The view is not redrawn unless you change the view's properties, which calls invalidate(), or if you call invalidate() manually. If you are running an animation in your application and do not obtain the smooth results you want, consider enabling hardware layers on your animated views.

 

硬件图层可以传送更快和更平滑的动画,当你的应用程序被硬件加速。每秒60帧地运行一个动画不总是可能的,当动画化复杂的视图造成大量绘画操作时。可以通过使用硬件图层渲染视图到硬件纹理来减轻它。然后硬件纹理可以被用于动画化视图,消除视图在它正在被动画化的时候经常重绘它自己的需要。视图不被重绘,除非你改变视图的属性,这样会调用invalidate(),或者如果你手动地调用invalidate()。如果你正在在你的用于程序中运行一个动画并且没有获得你希望的平滑结果时,请考虑在你被动画化的视图上使能硬件图层。

 

When a view is backed by a hardware layer, some of its properties are handled by the way the layer is composited on screen. Setting these properties will be efficient because they do not require the view to be invalidated and redrawn. The following list of properties affect the way the layer is composited. Calling the setter for any of these properties results in optimal invalidation and no redrawing of the targeted view:

 

当一个视图被一个应用程序图层支持,它的一些属性通过图层在屏幕上被组合的方式来被控制。设置这些属性将是有效的,因为它们不需要视图被无效化和重绘。以下列表的属性影响图层被组合的方式。对这些属性中的任意调用set方法会导致最佳的无效化并且不重绘目标视图。

 

* alpha: Changes the layer's opacity

 

* alpha:改变图层的不透明度

 

* x, y, translationX, translationY: Changes the layer's position

 

* x,y,translationX,translationY:改变图层的位置

 

* scaleX, scaleY: Changes the layer's size

 

* scaleX,scaleY:改变图层的大小

 

* rotation, rotationX, rotationY: Changes the layer's orientation in 3D space

 

* rotation,rotationX,rotationY:改变图层在三维空间中的方向

 

* pivotX, pivotY: Changes the layer's transformations origin

 

* pivotX,pivotY:改变图层的变换原点(注:枢纽,旋转轴)

 

These properties are the names used when animating a view with an ObjectAnimator. If you want to access these properties, call the appropriate setter or getter. For instance, to modify the alpha property, call setAlpha(). The following code snippet shows the most efficient way to rotate a viewiew in 3D around the Y-axis:

 

这些属性是当用一个ObjectAnimator动画化一个视图时使用的名称。如果你希望访问这些属性,请调用合适的set方法或get方法。例如,为了修改alpha属性,调用setAlpha()。以下代码片段演示在三维空间中绕Y轴旋转一个视图(注:此处viewiew应为view)的最有效方式。

 

-------------------------------

 

view.setLayerType(View.LAYER_TYPE_HARDWARE, null);

ObjectAnimator.ofFloat(view, "rotationY", 180).start();

 

-------------------------------

 

Because hardware layers consume video memory, it is highly recommended that you enable them only for the duration of the animation and then disable them after the animation is done. You can accomplish this using animation listeners:

 

因为硬件图层消耗显存(注:视频内存),所以强烈建议你使能它们仅用于动画期间然后在动画完成后屏蔽它们。你可以使用动画监听器来实现它:

 

-------------------------------

 

View.setLayerType(View.LAYER_TYPE_HARDWARE, null);

ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationY", 180);

animator.addListener(new AnimatorListenerAdapter() {

    @Override

    public void onAnimationEnd(Animator animation) {

        view.setLayerType(View.LAYER_TYPE_NONE, null);

    }

});

animator.start();

 

-------------------------------

 

For more information on property animation, see Property Animation.

 

想获得关于属性动画的更多信息,请参见属性动画。

 

-------------------------------

 

Tips and Tricks

 

提示和技巧

 

Switching to hardware accelerated 2D graphics can instantly increase performance, but you should still design your application to use the GPU effectively by following these recommendations:

 

切换到硬件加速的二维图形可以立刻增加性能,但你仍应该通过遵循这些建议设计你的应用程序以有效地使用GPU。

 

Reduce the number of views in your application

 

降低你的应用程序中的视图数

 

The more views the system has to draw, the slower it will be. This applies to the software rendering pipeline as well. Reducing views is one of the easiest ways to optimize your UI.

 

系统必须绘画的视图越多,它将会越慢。这也适用于软件渲染管线。降低视图是优化你的用户界面的其中一种最简单方式。

 

Avoid overdraw

 

避免上方绘画(过多绘画)

 

Do not draw too many layers on top of each other. Remove any views that are completely obscured by other opaque views on top of it. If you need to draw several layers blended on top of each other, consider merging them into a single layer. A good rule of thumb with current hardware is to not draw more than 2.5 times the number of pixels on screen per frame (transparent pixels in a bitmap count!).

 

不要绘画太多层在各自的上方。移除被其上方的其它不透明视图完全遮挡的任意视图。如果你需要绘画几个图层混合在各自的上方,请考虑融合它们进单一层中。一个适用于当前硬件的经验法则是,每帧不要绘画多于2.5倍的屏幕像素数(透明像素在一个位图的计算内!)。(注:待考)

 

Don't create render objects in draw methods

 

不要在绘画方法中创建渲染对象

 

A common mistake is to create a new Paint or a new Path every time a rendering method is invoked. This forces the garbage collector to run more often and also bypasses caches and optimizations in the hardware pipeline.

 

一个普遍的错误是每当一个渲染方法被调用时创建一个新Paint或一个新Path。这强制垃圾回收器运行得更频繁,而且还忽略了硬件管线(注:图形管线,是指GPU的3D并发渲染,它的数量表示显卡同时处理的指令数)中的缓存和优化。

 

Don't modify shapes too often

 

不要太频繁地修改形状

 

Complex shapes, paths, and circles for instance, are rendered using texture masks. Every time you create or modify a path, the hardware pipeline creates a new mask, which can be expensive.

 

复杂的形状,例如路径,以及圆形,是使用纹理遮罩(注:掩码)来渲染的。每当你创建或修改一个路径时,硬件管线创建一个新的遮罩,它可能是昂贵的。

 

Don't modify bitmaps too often

 

不要太频繁地修改位图

 

Every time you change the content of a bitmap, it is uploaded again as a GPU texture the next time you draw it.

 

每当你改变位图的内容时,它被再次上传作为一个GPU纹理,下次的时候你绘画它(注:待考)。

 

Use alpha with care

 

小心使用透明度

 

When you make a view translucent using setAlpha(), AlphaAnimation, or ObjectAnimator, it is rendered in an off-screen buffer which doubles the required fill-rate. When applying alpha on very large views, consider setting the view's layer type to LAYER_TYPE_HARDWARE.

 

当你使用setAlpha()、AlphaAnimation或ObjectAnimator使一个视图半透明时,它被渲染在一个离屏缓冲区,它加倍了所需的填充速率(注:fillrate,填充率,指像素写入显示帧缓冲区的速率)。当应用透明度在非常大的视图上时,考虑设置视图的图层类型为LAYER_TYPE_HARDWARE。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 29 Feb 2012 2:44

 

Android 4.0 r1 - 06 Mar 2012 7:44

 

-------------------------------

patch:

1. Android 4.0 r1 - 06 Mar 2012 7:44

(1)

++ Because of the increased resources required to enable hardware acceleration, your app will consume more RAM.

 

(2)

not cause any adverse effects

->

not cause any adverse drawing effects

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/


分享到:
评论

相关推荐

    ChatGPT技术的硬件加速与模型部署方案.docx

    ChatGPT 技术的硬件加速与模型部署方案 一、硬件加速 在 ChatGPT 技术中,硬件加速是提高模型训练和推理速度的重要环节。图形处理器(GPU)和领域专用集成电路(ASIC)是两种常见的选择。GPU 由于其并行计算能力而...

    用于FPGA硬件加速和设计分割的HES-DVM培训教程

    针对大规模的ASIC/SoC设计,当RTL设计在仿真需要大量的仿真时间时,最好进行FPGA的硬件仿真加速...本文为FPGA硬件加速和设计分割管理工具HES-DVM的中文培训教程,由Aldec中国的技术FAE翻译、整理和发布的官方培训材料。

    计算机英语 论文 中文加翻译

    论文也可能探讨硬件与软件之间的交互,比如编译器优化对硬件性能的影响,或是硬件加速技术如何提升特定算法的执行效率。 在文档列表中,我们看到多个个人姓名,这可能意味着这些文档是由不同的人完成的翻译工作,每...

    基于BERT的机器翻译模型开发详解.pdf

    为了支持扩展和加速,可以进行模型压缩和硬件优化。 **效果评估**是衡量模型性能的重要步骤。自动评估指标如BLEU(Bilingual Evaluation Understudy)和ROUGE(Recall-Oriented Understudy for Gisting Evaluation...

    (文献翻译)利用机器学习最大化硬件预取效率-李芳达3

    【标题】:“(文献翻译)利用机器学习最大化硬件预取效率-李芳达3” 【内容概述】:本文翻译了一篇关于如何利用机器学习优化硬件预取效率的研究论文。预取技术是解决CPU与内存之间速度差距的重要手段,特别是在...

    arm指令集翻译

    同时,为了确保用户体验,他们还需要解决图形加速、多线程支持以及硬件加速等问题。 在实际应用中,如“bea”这样的文件可能是二进制翻译层的一部分,或者是用于辅助安卓x86环境运行特定ARM应用程序的数据或配置...

    translatetojs:用于硬件加速 CSS 翻译的 jQuery 插件,带有优雅的 .animate() 回退

    一个简单的 jQuery 插件,用于硬件加速 CSS 转换,带有优雅的 .animate() 回退 现在有了 Modernizr 支持! (Modernizr.css3dtransforms) 用法: $(element).translateTo(x, y, duration); 默认值(持续时间总是以...

    计算机硬件基础知识.pdf

    系统板上集成了软盘接口、两个IDE硬盘接口、一个并行接口、两个串行接口、两个USB(Universal Serial Bus,通用串行总线)接口、AGP(Accelerated Graphics Port,加速图形接口)总线、PCI 总线、ISA 总线和键盘接口...

    Zynq Book中文翻译稿电子版_FPGAZYNQ_

    5. **硬件加速**:针对特定算法和应用,如图像处理、数字信号处理(DSP)等,书中演示了如何利用FPGA的并行计算能力实现硬件加速,提高系统性能。 6. **电源管理和功耗优化**:讨论了Zynq的电源管理策略,以及如何...

    基于FPGA软件_硬件协同设计模式.pdf

    文章提出了一种新的轻便的软件与可重构硬件之间的接口,该接口可以拦截软件端的常规方法调用并翻译成硬件消息,反之亦然。这样不仅可以简化实现新的Java/FPGA协同设计,而且能够加快现有Java程序的运行速度。 文章...

    EMLL(Edge ML Library)为加速终端侧设备上机器学习的推理而设计,提供基于端侧处理器的高性能机器学习计算函数库

    EMLL支持fp32、fp16、int8等数据类型,已在有道词典笔、翻译王和超级词典等硬件产品的机器翻译和语音识别引擎中应用,大幅降低了推理延迟。 EMLL(Edge ML Library)为加速终端侧设备上机器学习的推理而设计,提供基于...

    嵌入式外文翻译

    开发者需要进行性能调优,比如利用多线程、硬件加速器(如GPU或DSP)以及缓存策略,以提升翻译速度。 总的来说,基于ARM9的嵌入式电子文档外文翻译涉及到了处理器架构、嵌入式系统设计、文档解析、自然语言处理和...

    ARM指令集翻译器

    5. 协处理器指令:用于与硬件扩展单元进行交互,如浮点运算或加密加速器。 在实际应用中,ARM指令集的翻译器可能涉及到复杂的编译原理,如静态和动态分析、指令模拟、优化等技术。同时,了解Android系统的库加载...

    dx函数总结中文版+D3D中文翻译

    DX函数是DirectX API中的核心部分,提供了丰富的功能来控制硬件加速的图形、声音和其他相关功能。 “dx函数总结中文版”是一个文档,它很可能包含了DirectX主要函数的中文解释和概述,帮助开发者理解并应用这些函数...

    AIAS (AI Acceleration Suite) - 人工智能加速器套件

    AIAS的SDK是核心组件之一,它提供了丰富的API和工具,让开发者能够轻松地与硬件加速器进行交互,如GPU、FPGA或专门的AI芯片。SDK通常包括以下部分: 1. 编译器:将高阶语言转换为适合特定硬件的低级代码,以充分利用...

    深度学习时代下的机器翻译.pdf

    未来,随着硬件加速、模型优化和计算效率的提升,机器翻译有望实现更高效、更准确的翻译,并可能在个性化翻译、专业领域翻译以及跨语言信息检索等方面发挥更大作用。 在政策支持下,机器翻译作为自然语言处理的重要...

    PCI协议中文翻译 PCI协议中文翻译

    1. 计算机系统:在台式机和服务器中,PCI协议用于连接高性能的扩展卡,如图形加速器、网络适配器、RAID控制器等。 2. 工业应用:在自动化、嵌入式系统等领域,PCI接口常用于定制化硬件,实现高速数据采集和处理。 3....

    机器翻译——tf框架

    2. 高效执行:支持GPU加速,实现高效的计算。 3. 可移植性强:能在多种硬件平台和操作系统上运行,包括桌面、服务器、移动设备以及云端。 三、TensorFlow在机器翻译中的应用 1. 构建神经网络模型:在TensorFlow中...

    zynq book 中文翻译版

    在《Zynq Book》中文翻译版中,读者可以学习到如何充分利用Zynq SoC的双重优势,既能在ARM处理器上进行高效的数据处理,又能在FPGA中实现高速的硬件加速。无论是对Zynq新手还是有经验的开发者,这本书都是一个宝贵的...

    用AWS加速AI应用.pdf

    3. **FPGAs**:现场可编程门阵列(Field Programmable Gate Array)实例允许用户定制硬件加速逻辑,适用于特定类型的计算任务。 #### 五、Amazon Elastic Inference 除了GPU实例外,AWS还推出了**Amazon Elastic ...

Global site tag (gtag.js) - Google Analytics