我一直在我的安卓应用程序中使用TextureView,它运行得很好。就在最近,我用Android 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: 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);
}
}
}}
因此,如您所见,异常发生在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 25的任何版本说明吗?
发布于 2017-12-13 14:56:28
如果您查看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中未重写的“设置后台”方法的重写。设置背景现在显然是不鼓励的,只是不允许。如果您正在看到不受支持的操作异常,并且没有显式设置背景,那么它可能是从您的样式中溜进来的。尝试在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+丢失的功能,或者您是否需要它,但只想在您的库中拥有后台工具。
发布于 2017-04-30 12:51:46
下面是用于Android的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);
}
然后在setBackgroundDrawable
中重写TextureView
@Override
public void setBackgroundDrawable(Drawable background) {
if (background != null && !sTextureViewIgnoresDrawableSetters) {
throw new UnsupportedOperationException(
"TextureView doesn't support displaying a background drawable");
}
}
因此,您可以从所有这些方面拼凑起来: 1)您有一个目标SDK (努格特)--从您的构建文件中可以看出这一点;2)来自View
的构造函数确定一个非空背景(我目前无法解释这个部分)。
这才是真正的问题。我不认为您已经在xml中定义了一个可绘制的xml,因此覆盖setBackground
或setBackgroundDrawable
似乎是解决这个问题的最明智的方法。可能还有另一个解决方法(或者“建议使用”可能是一个更好的术语),您可以设法强迫构造函数中的background
变量保持空值。
发布于 2018-01-07 15:24:47
仅提到,不仅仅是TextureView:我发现,GridLayout也不支持显示自API 24以来可绘制的背景。
我试过:
( 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之上工作。
然而,TableLayout的背景即使在API 24+中也不会消失,所以我将所有相关代码从GridLayout重写为TableLayout,现在可以了。
https://stackoverflow.com/questions/43705434
复制相似问题