Authorselfsames

Transparency problem after Bitmap copy in Android 2.1

In our application we need to apply mask to some icons to make them semitransparent. The original images are in RGB format i.e. without alpha channel. To apply mask we need add alpha channel to original images. Before Android 2.0 we did this like:

Bitmap dest = src.copy(Bitmap.Config.ARGB_8888, true);

When we run our application on Android 2.1 device we noticed that the destination bitmap is created but without alpha channel. After applying mask, pixels that should be semitransparent become black.

The getPixel() method returns different value than was set by setPixel() method.

So, how to add alpha channel to RGB bitmap?

There are three ways:

First:

Use ARGB_4444 format:

Bitmap dest = src.copy(Bitmap.Config.ARGB_4444, true);

In this case the alpha channel will be added, but you will lose the quality of image

Second:

Create empty bitmap with dimensions of original image and ARGB_8888 format:

int width =  src.getWidth();
int height = src.getHeight();
Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Copy pixels from source bitmap to the int array:

int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);

And set these pixels to destination bitmap:

dest.setPixels(pixels, 0, width, 0, 0, width, height);

The alpha channel will be added without losing image quality. But this way is too slow ~ 5027ms average for 1000 bitmaps

Third: (Thanks to Cyril Mottier post)

First of all you need to create mask image which you want to apply to the icon and put it in to the resources.

Then you need to create Paint object and set xfermode to it:

paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

And draw source bitmap and mask on canvas:

canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(icon, 0, 0, paint);

This way requires to have mask image (you can’t modify source bitmap on the fly), but it is fast (~ 2659ms average for 1000 bitmaps) and without quality loss

Motally analytics for Android issues

We decided to try some analytic tool to compare it with Flurry analytics (http://www.flurry.com/) that we currently use in our applications.  Our choice has fallen on Motally Mobile Analytics (http://www.motally.com).

After signing up to Mottaly, we add our application and got an “Application Key” which should be used within the API initialization.  Then we download motally library and API documentation and started to integrate Motally into our application…

First of all we read the API documentation (http://motally.com/api/motally_android_api_doc.pdf) and tried the example from this document. And we were surprised that our code is not compiled because some methods cannot be found.  Our next step was motally javadoc studying. We have found out that javadoc and API documentation have a lot of differences. For example API documentation says:

Every Activity or Service within your application must retrieve the MoTrack object in order to invoke tracking calls on it. This is done with the help of the getSharedTracker method.”

But, there are no getSharedTracker() method in the motally library. And all methods are static and non of them do not return instance of Motrack object.

So, motally API Documentation is absolutely useless as does not coincide with the latest motally library for Android.

Eventually, using javadoc we have integrated motally analytics into our application, but… In the sniffer log of our applications we did not see any posts made by motally API.

To understand how motally works we … decompiled there code 🙂

All motally API methods are static in MoTracker class that extends android.app.Service.

To start tracking you need to start initSharedTracker() method, which init some preferences (such as Application Key) and starts Motally Service.

MoTracker object is initializing in onStartCommand() method which is called by the system every time a client explicitly starts the service.

But onStartCommand() method is available only since Android API level 5. Before Android API level 5 the onStart() method were used. Because of that Motally does not work on Android 1.5 and 1.6 devices.

Thus having tried Motally Mobile Analytics we decided to stay with Flurry 🙂

© 2017 Android Tales

Theme by Anders NorenUp ↑