Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not find Canvas variables in API Level 28

The following Canvas Variables are not found in Android 28.

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);
like image 242
Girish Bhutiya Avatar asked Jan 11 '19 14:01

Girish Bhutiya


People also ask

Which canvas variables are not found in Android 28?

Bookmark this question. Show activity on this post. The following Canvas Variables are not found in Android 28. 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);

What's new in Android 9 (API level 28)?

Android 9 (API level 28) introduces a number of changes to the Android system. The following behavior changes apply exclusively to apps that are targeting API level 28 or higher. Apps that set targetSdkVersion to API level 28 or higher must modify their apps to support these behaviors properly, where applicable to the app.

Could not find Android jar for API level?

Severity Code Description Project File Line Suppression State Error Could not find android.jar for API Level . This means the Android SDK platform for API Level is not installed. Either install it in the Android SDK Manager (Tools > Open Android SDK Manager...), or change your Xamarin.Android project to target an API version that is installed.

How do I select a target or minimum API level in Xamarin?

Before you can select a Target or Minimum API level in Xamarin.Android, you must install the Android SDK platform version that corresponds to that API level. The range of available choices for Target Framework, Minimum Android version, and Target Android version is limited to the range of Android SDK versions that you have installed.


2 Answers

Those flags have been removed in API 28. See here:

Class android.graphics.Canvas

Removed Methods int save(int)

Removed Fields 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

That method was deprecated in API 26. See here:

This method was deprecated in API level 26. Use saveLayer(float, float, float, float, Paint) instead.

What to use instead

According to the Canvas source code for API 28, the flags you use all combine to be equal to the value of 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;

From the same source code the call to Canvas#saveLayer(left, top, right, bottom, paint) defaults to using 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);  
}

So, it looks like your code is equivalent to the following code which you can use as a replacement:

canvas.saveLayer(0, 0, getWidth(), getHeight(), null);

This version of saveLayer() is only available on API 21+. To support lower API levels, use

canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

Where Canvas.ALL_SAVE_FLAG is the same as the or'ed values above.

like image 73
Cheticamp Avatar answered Oct 19 '22 02:10

Cheticamp


you can use canvas.save(); instead of canvas.save(Canvas.MATRIX_SAVE_FLAG|CLIP_SAVE_FLAG) reference

like image 34
Abdur Rehman Avatar answered Oct 19 '22 00:10

Abdur Rehman