运行时检测硬件加速:Android

9
有没有可能在创建Activity时始终检测到硬件加速是否启用?我担心我的库的用户会通过清单文件启用它,而不是特别为我的Activity禁用它(正如我指示他们所做的那样)。
我能找到的唯一可靠信息(http://android-developers.blogspot.com/2011/03/android-30-hardware-acceleration.html)是我可以查询View.isHardwareAccelerated()Canvas.isHardwareAccelerated()。然而,对于我的目的,我想确保当我的库的Activity被显示时它是关闭的。到目前为止,我无法得到任何一致的报告来判断它是否打开或关闭。我尝试使用一个虚拟视图,将其设置为我的Activity然后进行测试,但它总是返回false。此外,我尝试测试Window.getAttributes( ).flags,但它们也没有显示它。
我正在测试这个,因为我的库的硬件加速绘制路径不能正确地运行,并且似乎没有任何方法可以修复它。
2个回答

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;
}

嗨,布鲁斯。请回答以下问题:http://stackoverflow.com/questions/12481198/hardware-acceleration-supported-device-list - saa

6

尝试在活动中的flags中使用ActivityInfo中的FLAG_HARDWARE_ACCELERATED,您可以通过PackageManagergetActivityInfo()获得该活动。


4
另一种方法是在Activity的onCreate()方法中调用getDecorView().isHardwareAccelerated()。 - Romain Guy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接