Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Color.valueOf for API Level < 26?

I'm currently working with Bitmap and trying to make some operation on pixels. I wanted to use Color.argb() and Color.valueOf() but those doesn't work for API Level < 26.

Is there any library or something similar that could work with any API Level > 21 ?

Here is the part of the function I use :

int width =  myBitmap.getWidth();
int height = myBitmap.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int [] allpixels = new int [ bmp.getHeight()*bmp.getWidth()];
myBitmap.getPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),bmp.getHeight());
bmp.setPixels(allpixels, 0, width, 0, 0, width, height);

for(int i =0; i<bmp.getHeight()*bmp.getWidth();i++) {
    allpixels[i] = Color.argb(
        Color.valueOf(allpixels[i]).red(),
        Color.valueOf(allpixels[i]).red(),
        Color.valueOf(allpixels[i]).green(),
        Color.valueOf(allpixels[i]).blue());
}
like image 689
Kamloops Avatar asked Aug 26 '18 14:08

Kamloops


People also ask

What is the latest API level for Android apps?

Google has changed the Target API-level requirements for an Android app from August 2018. From August 2018, new apps must target at least Android 8.0 (API level 26). From November 2018, app updates must target Android 8.0 (API level 26).

What is the API level in a high API level?

a high API Level allows developers to take advantages of the latest APIs and capabilities provided by new platforms. in order to use the latest SupportLibrary, compileSdkVersion has to match the SupportLibrary version too. For example, in order to use SupportLibrary-28.x.x, compileSdkVersion has to be set to 28 as well.

What is the difference between SDK and API?

In Android, it’s true that th e re is a 1:1 relationship between the SDK and the API and often these two terms are used as synonymous, but it’s important to understand they are not the same thing. But it’s correct to say that for each Android version there is an SDK and there is an equivalent API and API Level.

What happens to the old API when the new one comes out?

Usually, updates to the framework API are designed so that the new API remains compatible with earlier versions of the API, for this reason, most of the changes to the new API are additive and the old parts of API are deprecated but not removed. But now someone could argue…


1 Answers

Use the version of argb() that takes int instead of float. That has been around since API Level 1.

like image 77
CommonsWare Avatar answered Oct 13 '22 20:10

CommonsWare