Android Nougat:TextureView不支持显示背景drawable

Abd*_*sae 25 android android-layout android-textureview android-7.1-nougat

我一直在我的Android应用程序中使用TextureView,它工作正常.就在最近,我在Android设备上使用Android API 25(7.1.2)测试了我的代码.相同的代码现在不起作用并抛出错误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)
Run Code Online (Sandbox Code Playgroud)

这是我如何使用我的(尚未定制)自定义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>
Run Code Online (Sandbox Code Playgroud)

这是我的相关AutoFitTextureView.java: enter code here

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);
        }
    }
}}
Run Code Online (Sandbox Code Playgroud)

因此,正如您所看到的,异常发生在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'
}
Run Code Online (Sandbox Code Playgroud)

任何想法为什么会这样?Android API 25的任何发布说明,其中涉及此更改?

Dav*_*ave 8

以下是ViewAndroid Nougat 源代码的片段:

/**
 * Allow setForeground/setBackground to be called (and ignored) on a textureview,
 * without throwing
 */
static boolean sTextureViewIgnoresDrawableSetters = false;
Run Code Online (Sandbox Code Playgroud)

在单参数构造函数中(从所有其他构造函数调用):

        // 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;
Run Code Online (Sandbox Code Playgroud)

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
    }
Run Code Online (Sandbox Code Playgroud)

实际定义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);
}
Run Code Online (Sandbox Code Playgroud)

然后覆盖setBackgroundDrawablein TextureView:

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

那么你可以从以下所有内容拼凑起来:1)你有一个目标SDK N(Nougat) - 从你的构建文件中可以看出来; 2)构造函数View确定非空背景(此刻我无法解释此部分).

这就是实际问题所需要的一切.我没有看到你已经设法在你的xml中定义了一个drawable,所以压倒setBackgroundsetBackgroundDrawable似乎是解决这个问题的最明智的可能性.可能有另一种解决方法(或者可能是"建议的用法"将是一个更好的术语),您可以设法强制background构造函数中的变量保持为null.

  • 我遇到了同样的问题.看来纯色背景应用于我的视图.当我删除它时,一切都开始工作了.但是我没有在你的例子中看到任何背景定义. (5认同)
  • 如果你在Android N +上的`TextureView`上使用非null值调用base`setBackgroundDrawable`,你将得到这个异常.我不确定你为什么要这样做,或者你认为它可能会完成什么.没有"适当的解决方案".它根本不受支持. (3认同)
  • 这肯定解决了这个问题.但是,由于我很可能需要在不久的将来使用'setBackgroundDrawable'方法,我只能将其视为make-do解决方法,而不是原始问题的正确解决方案. (2认同)

Che*_*amp 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正文中的注释draw()给出了您所看到的更改的基本原理。这是我找到的唯一文档。将此与TextureView来自 API 23 的内容进行比较:

/**
 * 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();
}
Run Code Online (Sandbox Code Playgroud)

API 24 还引入了对 API 23 中未覆盖的“设置背景”方法的覆盖。现在显然不鼓励设置背景,并且是不允许的。如果您看到不受支持的操作异常并且您没有明确设置背景,那么它可能是通过您的样式潜入的。尝试android:background="@null"在您的 XML 中设置以强制背景为空以解决错误。您还可以将以下代码添加到自定义视图中,以保留那些支持设置背景的版本的功能:

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

目前尚不清楚如何替换您为 API 24+ 丢失的功能,或者您是否甚至需要它但只想在您的武器库中拥有后台工具。