Android - 新的BitmapDrawable已弃用; 替代Bitmap.createBitmap必须具有w/h> 0

Kev*_*sen 9 android warnings bitmap deprecated bitmapdrawable

我用过PopupWindow.使用此PopupWindow,我将BackgroundDrawable设置为空的BitmapDrawable.

当我使用以下代码时,它会给出一个弃用的警告:

myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
Run Code Online (Sandbox Code Playgroud)

所以我改成了:

myPopupWindow.setBackgroundDrawable(new BitmapDrawable(
    getApplicationContext().getResources(),
    Bitmap.createBitmap(0, 0, Bitmap.Config.ARGB_8888)
));
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误,即Bitmap的宽度和高度必须大于0.

现在我使用:

myPopupWindow.setBackgroundDrawable(new BitmapDrawable(
    getApplicationContext().getResources(),
    Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
));
Run Code Online (Sandbox Code Playgroud)

它有效.但是使用1x1像素的Bitmap而不是像我想要的那样完全空的Bitmap似乎有点不对.还有另一种实际使用空BitmapDrawable的方法,而不是1 x 1像素的方法吗?

Kev*_*sen 23

好的,而不是

myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
Run Code Online (Sandbox Code Playgroud)

要么

myPopupWindow.setBackgroundDrawable(new BitmapDrawable(
    getApplicationContext().getResources(),
    Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
));
Run Code Online (Sandbox Code Playgroud)

我只是使用new ColorDrawable(android.graphics.Color color)我想要设置为PopupWindow 的background-color().所以例如,我的一个PopupWindows只有几个没有边距的图像,所以我使用了透明背景:

myPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Run Code Online (Sandbox Code Playgroud)

而对于另一个我想要使用白色背景,所以我用过:

myPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
Run Code Online (Sandbox Code Playgroud)