【问题标题】:Detect Hardware Acceleration at Runtime: Android在运行时检测硬件加速:Android
【发布时间】:2011-10-11 03:36:34
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-activity hardware-acceleration


    【解决方案1】:

    Try FLAG_HARDWARE_ACCELERATED in flags in ActivityInfo 用于活动,您可以从PackageManager 通过getActivityInfo() 获得。

    【讨论】:

    • 另一种方法是从 Activity 的 onCreate() 方法中调用 getDecorView().isHardwareAccelerated()。
    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-24
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多