0 comments on “7 copy & paste snippets for Android”

7 copy & paste snippets for Android

I listed seven common tasks that Android developers often face, it’s the kind of stuff that interrupts your train of tought and makes you think “Oh God.. this is boring and I have other things to do, let’s google for some code”.
And here it is. In no particular order.
Just copy and paste, no questions asked.

How to rotate a Bitmap in Android

This will rotate any Bitmap to the desired angle, in degrees.

How to copy a file in Android

The smart way, without that nasty new buffer[1024] thing.

How to delete a directory in Android

Yes, the directory itself and all the files and subdirectories recursively.

How to convert a byte array into an hexadecimal String

I will probably google this in the future and find my own post.

How to convert Android dps to pixels

You are welcome.

How to copy a directory in Android

Again yes, the directory itself and all the files and subdirectories recursively.

How to extract a zip archive in Android

This will extract any zip archive to the desired location on the filesystem.

Advertisements
0 comments on “Using BitmapShader in Android”

Using BitmapShader in Android

Recently I had to implement some custom UI controls for one of our customers, and some of these controls involved drawing Bitmaps with rounded corners.
Android provides a handy approach to this problem, all the magic can be done with a little help from a class named BitmapShader.

With BitmapShader you can draw bitmaps in any shape and even more, here’s an example:

bitmap-shader-01

What BitmapShaderdoes is basically providing a pixel source to a Paint object, when you draw something on a Canvasyou have to specify the Paintinstance that will be used to paint your stuff, if that Paintis bound to a BitmapShader you’ll get your stuff painted using the Bitmap that the BitmapShaderwas created with.

Here’s how to create a BitmapShaderobject:

With the BitmapShaderin place you can now draw anything you want using a Paint which is bound to the shader, the binding can be achieved with the setShader() method of the Paint class:

Now everything is in place and you can start drawing your bitmaps, following is a list of what I did in the example screenshot above and the code I used to achieve each sample, also note that in order to draw on a Canvas I created a custom View subclass and I added the drawing code inside the onDraw() method, in a real scenario however try to avoid allocating stuff inside the onDraw() method.

Rounded corners

all-rounded


Bottom corners rounded

bottom-rounded


Top corners rounded

top-rounded


Circle

circle


Narrow oval

oval-narrow


Wide oval

oval-wide


3 comments on “How to get the SHA1 hash sum of a String in Android”

How to get the SHA1 hash sum of a String in Android

Here’s one piece of code that comes very handy in some situations, just copy and paste this to get the SHA1 hash sum from a text String. The Hash is returned as an hex encoded human readable String, see below for an example of use:

Here’s how to use this:

The code above will print this output:

String: Hello World
SHA1 HASH: 0a4d55a8d778e5022fab701977c5d840bbc486d0

Enjoy 😉

0 comments on “Android Snippet. How to quickly run code on the main thread”

Android Snippet. How to quickly run code on the main thread

Here’s a simple code snippet that enables you to run a Runnable object on the main thread, or UI thread. This is very handy if you have to update your UI from a background thread or an AsyncTask. It does not require something in particular, this code can be run from everywhere inside your application:

Enjoy 🙂