9

是否可以始终如一地检测 Activity 在创建时是否启用了硬件加速?我担心我的图书馆的用户会在不应该的时候通过清单启用它,而不是专门为我的活动禁用它(正如我指示他们做的那样。)

我能找到的唯一可靠信息(http://android-developers.blogspot.com/2011/03/android-30-hardware-acceleration.html)说我可以查询View.isHardwareAccelerated()Canvas.isHardwareAccelerated()。但是,出于我的目的,我想确保在显示我的图书馆的活动时它是关闭的。到目前为止,我无法在打开或关闭时报告一致的是/否。我尝试在一个虚拟视图中进行黑客攻击,将其设置为我的活动,然后对其进行测试,但它总是返回 false。另外,我尝试了 testing Window.getAttributes( ).flags,但他们也没有显示出来。

我正在对此进行测试,因为我的库的硬件加速绘制路径无法正常工作,而且似乎没有任何方法可以修复它。

4

2 回答 2

6

Try FLAG_HARDWARE_ACCELERATED参加flags活动ActivityInfo,您可以PackageManager通过getActivityInfo().

于 2011-07-22T16:57:54.683 回答
6

我是Android新手,所以即使上面答案中给出的线索我也被困住了。去四处搜索,在谷歌海洋的某个地方找到了这段代码。希望它可以帮助某人。

/**
 * Returns true if the given Activity has hardware acceleration enabled
 * in its manifest, or in its foreground window.
 *
 * TODO(husky): Remove when initialize() is refactored (see TODO there)
 * TODO(dtrainor) This is still used by other classes.  Make sure to pull some version of this
 * out before removing it.
 */
public static boolean hasHardwareAcceleration(Activity activity) {
    // Has HW acceleration been enabled manually in the current window?
    Window window = activity.getWindow();
    if (window != null) {
        if ((window.getAttributes().flags
                & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
            return true;
        }
    }

    // Has HW acceleration been enabled in the manifest?
    try {
        ActivityInfo info = activity.getPackageManager().getActivityInfo(
                activity.getComponentName(), 0);
        if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
            return true;
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e("Chrome", "getActivityInfo(self) should not fail");
    }

    return false;
}
于 2013-09-03T15:08:25.420 回答