安卓奶油三文治:TextureView不支持显示背景可绘制图形。

29
我一直在我的Android应用程序中使用TextureView,它一直都很好用。最近我在一个安装了Android API 25(7.1.2)的Android设备上测试我的代码。同样的代码现在不起作用,并抛出错误:java.lang.UnsupportedOperationException: TextureView doesn't support displaying a background drawable
我知道void setBackgroundDrawable (Drawable background)已经被弃用很长时间,现在肯定已经被删除了。但我甚至没有自己设置它。
我正在使用最新的buildTools和SDK。所以,我想知道为什么textureView内部实现还没有更新。 以下是相关的堆栈跟踪:
java.lang.UnsupportedOperationException: TextureView doesn't support displaying a background drawable
at android.view.TextureView.setBackgroundDrawable(TextureView.java:315)
at android.view.View.setBackground(View.java:18124)
at android.view.View.<init>(View.java:4573)
at android.view.View.<init>(View.java:4082)
at android.view.TextureView.<init>(TextureView.java:159)
at com.abdulwasaetariq.xyz.ui.customView.AutoFitTextureView.<init>(AutoFitTextureView.java:24)
at com.abdulwasaetariq.xyz.ui.customView.AutoFitTextureView.<init>(AutoFitTextureView.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[...]
at java.lang.Thread.run(Thread.java:745)

以下是我如何使用尚未自定义的自定义TextureView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.abdulwasaetariq.xyz.ui.activity.MainActivity">

    <com.abdulwasaetariq.xyz.ui.customView.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="1080px"
        android:layout_height="1080px"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

这是我相关的AutoFitTextureView.java:请输入代码

public class AutoFitTextureView extends TextureView {

private int mRatioWidth = 0;
private int mRatioHeight = 0;

public AutoFitTextureView(Context context) {
    this(context, null);
}

public AutoFitTextureView(Context context, AttributeSet attrs) {
    this(context, attrs, 0); //(LINE#20)
}

public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle); //(LINE#24)
}

public void setAspectRatio(int width, int height) {
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Size cannot be negative.");
    }
    mRatioWidth = width;
    mRatioHeight = height;
    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width, height);
    } else {
        if (width < height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}}

如你所见,异常发生在 super() 方法中,这意味着我的自定义 TextureView 不负责此异常。这是内部调用。

以下是我的 gradle 配置:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'
    defaultConfig {
        applicationId "com.abdulwasaetariq.xyz"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        })
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
    compile 'com.github.hotchemi:permissionsdispatcher:2.3.2'
    annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.3.2'
}

有什么想法是为什么会发生这种情况吗?Android API 25的发布说明中是否提到了这个变化?

1
评论不适合进行长时间的讨论;此对话已被移至聊天室 - Bhargav Rao
5个回答

8
以下是 Android Nougat 版本的 View 源代码片段:
/**
 * Allow setForeground/setBackground to be called (and ignored) on a textureview,
 * without throwing
 */
static boolean sTextureViewIgnoresDrawableSetters = false;

在单参数构造函数中(从所有其他函数调用):
        // Prior to N, TextureView would silently ignore calls to setBackground/setForeground.
        // On N+, we throw, but that breaks compatibility with apps that use these methods.
        sTextureViewIgnoresDrawableSetters = targetSdkVersion <= M;

在抛出异常的View构造函数中:
...
        switch (attr) {
            case com.android.internal.R.styleable.View_background:
                background = a.getDrawable(attr);
                break;
...
    if (background != null) {
        setBackground(background);  // <--- this is the problematic line, apparently "background" is not null here
    }

实际上,setBackground的定义是:
/**
 * Set the background to a given Drawable, or remove the background. If the
 * background has padding, this View's padding is set to the background's
 * padding. However, when a background is removed, this View's padding isn't
 * touched. If setting the padding is desired, please use
 * {@link #setPadding(int, int, int, int)}.
 *
 * @param background The Drawable to use as the background, or null to remove the
 *        background
 */
public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

接下来是在TextureView中重写 setBackgroundDrawable 的内容:

@Override
public void setBackgroundDrawable(Drawable background) {
    if (background != null && !sTextureViewIgnoresDrawableSetters) {
        throw new UnsupportedOperationException(
                "TextureView doesn't support displaying a background drawable");
    }
}

所以你可以从中得出以下结论: 1)您的目标SDK版本为N(Nougat)-这在您的构建文件中很明显; 2)View的构造函数确定了一个非空背景(我暂时无法解释这一部分)。
这就是这个问题的实际问题所在。我没有看到您已经成功定义了XML中的可绘制对象,因此覆盖setBackgroundsetBackgroundDrawable似乎是解决问题最明智的可能性。可能还有另一种解决方法(或者也许“建议使用”是更好的术语),您可以设法强制构造函数中的background变量保持为空。

5
我遇到了同样的问题。结果是我的视图有一个实色背景。当我移除它时,一切重新开始工作了。但是我在你的示例中没有看到任何背景定义。 - Andrey Novikov
2
这绝对可以解决问题。然而,由于我很可能在不久的将来需要使用“setBackgroundDrawable”方法,因此我只能把它视为权宜之计,而不是原始问题的正确解决方案。 - Abdul Wasae
3
如果你在 Android N+ 上调用TextureViewsetBackgroundDrawable方法并传入非空值,你将会得到这个异常。我不确定你为什么要这样做或者你认为它可能会产生什么效果。没有"正确的解决方案"。它只是不被支持。 - Dave
谢谢,Andrey Novikov!我也是一样的。 - eldes

6
如果您查看 API 24 的纹理视图源代码,您会看到以下内容:
/**
 * Subclasses of TextureView cannot do their own rendering
 * with the {@link Canvas} object.
 *
 * @param canvas The Canvas to which the View is rendered.
 */
@Override
public final void draw(Canvas canvas) {
    // NOTE: Maintain this carefully (see View#draw)
    mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

    /* Simplify drawing to guarantee the layer is the only thing drawn - so e.g. no background,
    scrolling, or fading edges. This guarantees all drawing is in the layer, so drawing
    properties (alpha, layer paint) affect all of the content of a TextureView. */

    if (canvas.isHardwareAccelerated()) {
        DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;

        HardwareLayer layer = getHardwareLayer();
        if (layer != null) {
            applyUpdate();
            applyTransformMatrix();

            mLayer.setLayerPaint(mLayerPaint); // ensure layer paint is up to date
            displayListCanvas.drawHardwareLayer(layer);
        }
    }
}

draw()函数体中的注释提供了修改的理由。这是我找到的唯一文档。与API 23中的TextureView进行比较:

/**
 * Subclasses of TextureView cannot do their own rendering
 * with the {@link Canvas} object.
 *
 * @param canvas The Canvas to which the View is rendered.
 */
@Override
public final void draw(Canvas canvas) {
    // NOTE: Maintain this carefully (see View.java)
    mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

    applyUpdate();
    applyTransformMatrix();
}

API 24 还引入了对未在 API 23 中进行覆盖的“set background”方法的重写。现在明确不鼓励设置背景并且不被允许。如果您看到不支持操作异常,而您并没有显式设置背景,则可能通过您的样式偷偷进入。尝试在 XML 中设置 android:background="@null" 强制将背景设置为 null 来避免错误。您还可以添加以下代码到您的自定义视图中,在那些支持设置背景的版本上保留此功能:

@Override
public void setBackgroundDrawable(Drawable background) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && background != null) {
        setBackgroundDrawable(background);
    }
}

目前还不清楚如何替换API 24+中你失去的功能,或者你是否需要它,但个人认为在你的工具箱中拥有这个后台工具还是很有用的。


1
啊!“……它可能是通过你的样式悄悄地溜进来的……”一定是这个原因。我会检查一下然后回来。 - Abdul Wasae
@AbdulWasae 这是你的样式吗? - Cheticamp
很不幸,我还没有能够得到一个API 25的设备。而且这个问题在API 24和26上也无法重现 :/ 我会尝试在API 25上进行测试,然后再告诉您结果。 - Abdul Wasae
我相信这个答案是正确的,因为我还没有能够配置出与我最初遇到问题时相同的配置。 - Abdul Wasae
谢谢!就是样式问题。这个 bug 真是太傻了。android:background="@null" 简直完美解决! - halxinate

2
仅提一下,并非仅限于 TextureView:我发现,自 API 24 起,GridLayout 也不支持显示背景 Drawable。
我尝试过:
A)gridLayout.setBackgroundResource(R.drawable.board_960x960px_border_in_bg); B)Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.board_960x960px_border_in_bg); gridLayout.setBackground(drawable); 以上两种方法在 API 23 及以上版本均无法正常工作。
然而,即便在 API 24+ 上,TableLayout 的背景仍将保留,因此我将所有相关代码从 GridLayout 改写为 TableLayout,现在一切都好了。

1

在 TextureView 的 xml 文件中将背景设置为 null。

例如:android:background="@null"

对我来说有效。

或者,如果您有自定义的 Texture 类,则可以在 TextureView 中重写此方法:

@Override public void setBackgroundDrawable(Drawable background) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && background != null) { setBackgroundDrawable(background); } }


0
当我将targetSdkVersion更改为23时,我的问题得到了解决。

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