dimanche 21 septembre 2014

Get ImageView bitmap


Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Be carefull to check if your image.getDrawable() can actually be cast to BitmapDrawable (to avoid ClassCastExceptions). 
If, for instance, you use layers in your image then this snippet will be slightly different: Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)image.getDrawable()).getDrawable(0)).getBitmap(‌​);
http://stackoverflow.com/questions/8306623/get-bitmap-attached-to-imageview

samedi 13 septembre 2014

Manage audio volume in my app

private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        return false;
    }
}

jeudi 28 août 2014

PS: Enregistrement d’une image comme motif prédéfini


  1. Utilisez l’outil Rectangle de sélection  sur une image ouverte pour sélectionner une zone à utiliser comme motif. La valeur du contour progressif doit être de 0 pixel. Notez que les grandes images peuvent devenir difficiles à manier.

  2. Choisissez Edition > Utiliser comme motif.

  3. Donnez un nom au motif dans la boîte de dialogue Nom du motif.
    Remarque :
    si vous utilisez un motif provenant d’une image et que vous l’appliquez à une autre, Photoshop convertit le mode colorimétrique.

lundi 11 août 2014

Java copy-constructors

/**
  * Regular constructor.
  */
  public Galaxy(double aMass, String aName) {
    fMass = aMass;
    fName = aName;
  }

  /**
  * Copy constructor.
  */
  public Galaxy(Galaxy aGalaxy) {
    this(aGalaxy.getMass(), aGalaxy.getName());
    //no defensive copies are created here, since 
    //there are no mutable object fields (String is immutable)
  }

  /**
  * Alternative style for a copy constructor, using a static newInstance
  * method.
  */
  public static Galaxy newInstance(Galaxy aGalaxy) {
    return new Galaxy(aGalaxy.getMass(), aGalaxy.getName());
  }

(from http://www.javapractices.com/topic/TopicAction.do?Id=12)

mercredi 16 juillet 2014

Google Play Services not running on Froyo 2.2 and below

Google Mobile Ads is now offered through Google Play Services 4.0 and this error occurs because the Google Play Services is not available. Most likely you will run into this on Froyo, where there is a special version that can be used.
Note: Google Play services 4.0.30 (released November 2013) and newer versions require Android 2.3 or higher. If your app supports Android 2.2, you can continue development with the Google Play services SDK, but must instead install Google Play services for Froyo from the SDK Manager.
http:// android-developers.blogspot.no/2013/10/google-play-services-40.html
With over 97% of devices now running Android 2.3 (Gingerbread) or newer platform versions, we’re dropping support for Froyo from this release of the Google Play services SDK in order to make it possible to offer more powerful APIs in the future. That means you will not be able to utilize these new APIs on devices running Android 2.2 (Froyo).
Important: Because it is hard to anticipate the state of each device, you must always check for a compatible Google Play services APK before you access Google Play services features. For many apps, the best time to check is during the onResume() method of the main activity.
Alernative 1 is to check if Google Play Services is available before using AdView.
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext();
if(status == ConnectionResult.SUCCESS) {
    //Success! Do what you want
}
Alernative 2 is to use GoogleAdMobAdsSdk-6.4.1.jar
There are at least two solutions to solve the problem:
  • Change the minSdkVersion in your app's manifest to 9.
  • Don't use the latest Google Play Services library, but use the Froyo version in stead.
or

There is a third solution: guard the code that requires new functionality in Google Play Services (such as Admob ads) with a if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO)

samedi 12 juillet 2014

Adding Google Play Services via IntelliJ

This was successfully tested with Google Play Services v18 & IntelliJ 13:

You need to have a copy (as opposed to using it directly from the SDK folder), because if Google updates it, things are going to break when you update your SDK, it's safer to keep a copy (albeit more tedious because you will have to manually update when a new version comes out). I found out that it's better to decide when to update than to be surprised with cryptic compilation errors.
With that in mind, copy the library alongside your project. The library is located in
"ANDROID-SDK"/extras/google/google_play_services/libproject/google-play-services_lib
ANDROID-SDK is wherever you stored the SDK when you downloaded it. Google changes this every now and then, so just find it, it will be there.
Copy that entire folder like this:
/mylibpath/google-play-services_lib/ <-- THIS IS THE NEW ONE
Good, now let's import it to IntelliJ-IDEA the right way:
1) Select your project/main module from project panel (⌘ 1) and hit ⌘ ↓ in order to open project structure window or just do File -> Project Structure.
2) Select Modules from left panel, then hit + button (or ⌘ N) then select "import module" and navigate to the google-play-services_lib that you just copied next to your project's module folder. (/mylibpath/).
The wizard will ask a series of questions, just hit next, defaults are fine, but read what's going on, understand what's happening, this is a core concept in Java, Android and Intellij. Experiment if you don't know what each dialog is saying (or why some may be empty).
After you're done, the google play Project will be added to Modules alongside with your project.
Don't close this window (or open it again if you did).
3) Still in the modules, select your project/module, then select "Dependencies" from right panel (it's atab) and finally Click + button at the bottom (or ⌘ N which will work if you have the focus in this tab), then from the popup chose "3 Module dependencies..." and select "google-play-services_lib", which you added in the previous step. It should be there. If it's not, you didn't add the module in step 2.
4) Finally, Click + button again (or ⌘ N) but this time select "1 jars or directories...", navigate to the "/libs" folder of the above path and select "google-play-services.jar".
5) Click ok and close project structure window.
If you did this, your main module should have a dependency that looks like this:
Dependencies
Also make sure that in the Project Structure window, FACETS Tab, the Google Play Services has the "Library Module" checked. It is a library module.
Note: Make sure you have not added "google-play-services_lib" project as library in "Libraries" (left panel under Modules). If you have, you can go to "Libraries and simply remove it. Google Play Library Should Not Appear In Libraries.
Credit: this text comes from this StackOverflow answer