在API级别28中找不到Canvas变量

问题描述 投票:4回答:1

在Android 28中找不到以下Canvas变量。

canvas.saveLayer(0, 0, getWidth(), getHeight(), null,
                Canvas.MATRIX_SAVE_FLAG |
                        Canvas.CLIP_SAVE_FLAG |
                        Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
                        Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
                        Canvas.CLIP_TO_LAYER_SAVE_FLAG);
android canvas android-9.0-pie
1个回答
8
投票

这些标志已在API 28中删除。请参阅here

类android.graphics.Canvas

删除方法int save(int)

删除了CLIP_SAVE_FLAG中的字段 int CLIP_TO_LAYER_SAVE_FLAG int FULL_COLOR_LAYER_SAVE_FLAG int HAS_ALPHA_LAYER_SAVE_FLAG int MATRIX_SAVE_FLAG

该方法在API 26中已弃用。请参阅here

此方法在API级别26中已弃用。请改用saveLayer(float,float,float,float,Paint)。

用什么代替

根据API 28的Canvas源代码,您使用的标志总和等于ALL_SAVE_FLAG的值:

public  static  final  int ALL_SAVE_FLAG =  0x1F;
public  static  final  int MATRIX_SAVE_FLAG =  0x01;
public  static  final  int CLIP_SAVE_FLAG =  0x02;
public  static  final  int HAS_ALPHA_LAYER_SAVE_FLAG =  0x04;
public  static  final  int FULL_COLOR_LAYER_SAVE_FLAG =  0x08;
public  static  final  int CLIP_TO_LAYER_SAVE_FLAG =  0x10;

从相同的source code调用Canvas#saveLayer(left, top, right, bottom, paint)默认使用ALL_SAVE_FLAG

/**  
 * Convenience for {@link #saveLayer(RectF, Paint)} that takes the four float coordinates of the  
 * bounds rectangle. */
public int saveLayer(float left, float top, float right, float bottom, @Nullable Paint paint) {  
    return saveLayer(left, top, right, bottom, paint, ALL_SAVE_FLAG);  
}

因此,看起来您的代码等同于以下代码,您可以将其用作替换:

canvas.saveLayer(0, 0, getWidth(), getHeight(), null);
© www.soinside.com 2019 - 2024. All rights reserved.