当前位置:  编程技术>移动开发
本页文章导读:
    ▪操作CoreData 常见的异常及解决办法        操作CoreData 常见的错误及解决方法 src:   http://blog.csdn.net/ch_soft/article/details/6674224 ......
    ▪ 用Dialog创造带箭头的对话框        用Dialog创建带箭头的对话框       很多应用中,在点击Button后,会弹出一个带箭头的对话框,指向这个Button。网上查了,很多人都说用PopupWindow可以实现,我对PopupWindow不熟,于是试着用.........
    ▪ 【通译】(72)硬件加速       【翻译】(72)硬件加速 【翻译】(72)硬件加速   see http://developer.android.com/guide/topics/graphics/hardware-accel.html   原文见 http://developer.android.com/guide/topics/graphics/hardware-accel.html   --------------------------.........

[1]操作CoreData 常见的异常及解决办法
    来源: 互联网  发布时间: 2014-02-18
操作CoreData 常见的错误及解决方法
src:   http://blog.csdn.net/ch_soft/article/details/6674224

    
[2] 用Dialog创造带箭头的对话框
    来源: 互联网  发布时间: 2014-02-18
用Dialog创建带箭头的对话框
      很多应用中,在点击Button后,会弹出一个带箭头的对话框,指向这个Button。网上查了,很多人都说用PopupWindow可以实现,我对PopupWindow不熟,于是试着用Dialog实现。(需要注意的是:PopupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,这和AlertDialog不同,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的。)
      先选用一个带箭头的图片(pop.gif)做Dialog的背景,Dialog的布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_dialog"
	android:orientation="vertical"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:background="@drawable/pop"
    >

	<Button android:id="@+id/btn_up" android:text="上" 
        android:layout_width="80dip" android:layout_height="25dip" android:background="@drawable/style_white_orange"
        />
    <Button android:id="@+id/btn_down" android:text="下"
        android:layout_width="80dip" android:layout_height="25dip" android:background="@drawable/style_white_orange"
        />

</LinearLayout>


      另外,需要设定Dialog的样式。在values文件夹下新建styles.xml,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@drawable/pop</item>
        <item name="android:windowBackground">@color/transparent_background</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
</resources>


      尤其注意的是,sytles.xml里的background也要设为dialog的背景图片(pop.gif),windowBackground设为透明。
     
      主页面的代码:
public class MainActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PopupDialog popupDialog = new PopupDialog(MainActivity.this, R.style.dialog);
				popupDialog.setCanceledOnTouchOutside(true); // 点击Dialog之外的区域对话框消失
				Window window = popupDialog.getWindow();
				WindowManager.LayoutParams lp = window.getAttributes();
				lp.y = -64; // 设置竖直方向的偏移量
				lp.dimAmount = 0f; // 弹出对话框的时候背景不变暗
				popupDialog.show();
			}
		});
    }
}


      最后效果如下图:



1 楼 rainbow767 2012-06-20  
2 楼 rainbow767 2012-06-20  
写得什么呀,你要是真想告诉人家就写明白。比如main.xml在哪里啊?有些id都没有。到哪里找,服了。
3 楼 rainbow767 2012-06-20  
再说了,你这个程序能不能通过你试过没有,
有没有PopupDialog这个类,还是你自己造的?反正我在android库里没搜到这个PopupDialog。。。

    
[3] 【通译】(72)硬件加速
    来源: 互联网  发布时间: 2014-02-18
【翻译】(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/



    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3