ARGB_8888ビットマップ(Honeycombより前のバージョン)は、RGBA形式でネイティブに保存されます。したがって、アルファチャネルは最後に移動されます。ビットマップのピクセルにネイティブにアクセスするときは、これを考慮に入れる必要があります。
3.2より前のバージョンのAndroid(APIレベル<12)のコードを記述していると仮定します。それ以降、メソッドの動作は
BitmapFactory.decodeFile(pathToImage);
BitmapFactory.decodeFile(pathToImage, opt);
bitmapObject.createScaledBitmap(bitmap, desiredWidth, desiredHeight, false /*filter?*/);
変更されました。
古いプラットフォーム(APIレベル<12)では、BitmapFactory.decodeFile(..)メソッドは、アルファが見つからない場合、デフォルトでRGB_565構成のビットマップを返そうとします。これにより、画像の品質が低下します。を使用してARGB_8888ビットマップを適用できるため、これは引き続き問題ありません。
options.inPrefferedConfig = Bitmap.Config.ARGB_8888
options.inDither = false
実際の問題は、画像の各ピクセルのアルファ値が255(つまり完全に不透明)の場合に発生します。その場合、ビットマップにARGB_8888構成がある場合でも、ビットマップのフラグ'hasAlpha'はfalseに設定されます。* .pngファイルに少なくとも1つの実際の透明なピクセルが含まれている場合、このフラグはtrueに設定されているため、何も心配する必要はありません。
したがって、を使用してスケーリングされたビットマップを作成する場合
bitmapObject.createScaledBitmap(bitmap, desiredWidth, desiredHeight, false /*filter?*/);
このメソッドは、「hasAlpha」フラグがtrueまたはfalseに設定されているかどうかを確認します。この場合、falseに設定されているため、スケーリングされたビットマップが取得され、RGB_565形式に自動的に変換されます。
したがって、APIレベル> = 12には、次のようなパブリックメソッドがあります。
public void setHasAlpha (boolean hasAlpha);
これでこの問題は解決したでしょう。これまでのところ、これは問題の単なる説明でした。調査を行ったところ、setHasAlphaメソッドは長い間存在していて公開されていましたが、非表示になっていることに気付きました(@hideアノテーション)。Android2.3での定義は次のとおりです。
/**
* Tell the bitmap if all of the pixels are known to be opaque (false)
* or if some of the pixels may contain non-opaque alpha values (true).
* Note, for some configs (e.g. RGB_565) this call is ignore, since it does
* not support per-pixel alpha values.
*
* This is meant as a drawing hint, as in some cases a bitmap that is known
* to be opaque can take a faster drawing case than one that may have
* non-opaque per-pixel alpha values.
*
* @hide
*/
public void setHasAlpha(boolean hasAlpha) {
nativeSetHasAlpha(mNativeBitmap, hasAlpha);
}
これが私のソリューションの提案です。ビットマップデータのコピーは含まれません。
現在のビットマップ実装にパブリック'setHasAplha'メソッドがあるかどうかをjava.lang.Reflectを使用して実行時にチェックします。(私のテストによると、APIレベル3以降は完全に機能しますが、JNIが機能しないため、下位バージョンはテストしていません)。メーカーが明示的に非公開、保護、または削除した場合、問題が発生する可能性があります。
JNIを使用して、特定のビットマップオブジェクトに対して「setHasAlpha」メソッドを呼び出します。これは、プライベートメソッドやフィールドでも完全に機能します。JNIは、アクセス制御ルールに違反しているかどうかをチェックしないことは公式です。出典: http: //java.sun.com/docs/books/jni/html/pitfalls.html(10.9)これにより、賢明に使用する必要のある大きな力が得られます。たとえそれが機能するとしても、私は最終フィールドを変更しようとはしません(例を示すためだけに)。そして、これは単なる回避策であることに注意してください...
必要なすべてのメソッドの実装は次のとおりです。
JAVAパート:
// NOTE: this cannot be used in switch statements
private static final boolean SETHASALPHA_EXISTS = setHasAlphaExists();
private static boolean setHasAlphaExists() {
// get all puplic Methods of the class Bitmap
java.lang.reflect.Method[] methods = Bitmap.class.getMethods();
// search for a method called 'setHasAlpha'
for(int i=0; i<methods.length; i++) {
if(methods[i].getName().contains("setHasAlpha")) {
Log.i(TAG, "method setHasAlpha was found");
return true;
}
}
Log.i(TAG, "couldn't find method setHasAlpha");
return false;
}
private static void setHasAlpha(Bitmap bitmap, boolean value) {
if(bitmap.hasAlpha() == value) {
Log.i(TAG, "bitmap.hasAlpha() == value -> do nothing");
return;
}
if(!SETHASALPHA_EXISTS) { // if we can't find it then API level MUST be lower than 12
// couldn't find the setHasAlpha-method
// <-- provide alternative here...
return;
}
// using android.os.Build.VERSION.SDK to support API level 3 and above
// use android.os.Build.VERSION.SDK_INT to support API level 4 and above
if(Integer.valueOf(android.os.Build.VERSION.SDK) <= 11) {
Log.i(TAG, "BEFORE: bitmap.hasAlpha() == " + bitmap.hasAlpha());
Log.i(TAG, "trying to set hasAplha to true");
int result = setHasAlphaNative(bitmap, value);
Log.i(TAG, "AFTER: bitmap.hasAlpha() == " + bitmap.hasAlpha());
if(result == -1) {
Log.e(TAG, "Unable to access bitmap."); // usually due to a bug in the own code
return;
}
} else { //API level >= 12
bitmap.setHasAlpha(true);
}
}
/**
* Decodes a Bitmap from the SD card
* and scales it if necessary
*/
public Bitmap decodeBitmapFromFile(String pathToImage, int pixels_limit) {
Bitmap bitmap;
Options opt = new Options();
opt.inDither = false; //important
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(pathToImage, opt);
if(bitmap == null) {
Log.e(TAG, "unable to decode bitmap");
return null;
}
setHasAlpha(bitmap, true); // if necessary
int numOfPixels = bitmap.getWidth() * bitmap.getHeight();
if(numOfPixels > pixels_limit) { //image needs to be scaled down
// ensures that the scaled image uses the maximum of the pixel_limit while keeping the original aspect ratio
// i use: private static final int pixels_limit = 1280*960; //1,3 Megapixel
imageScaleFactor = Math.sqrt((double) pixels_limit / (double) numOfPixels);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,
(int) (imageScaleFactor * bitmap.getWidth()), (int) (imageScaleFactor * bitmap.getHeight()), false);
bitmap.recycle();
bitmap = scaledBitmap;
Log.i(TAG, "scaled bitmap config: " + bitmap.getConfig().toString());
Log.i(TAG, "pixels_limit = " + pixels_limit);
Log.i(TAG, "scaled_numOfpixels = " + scaledBitmap.getWidth()*scaledBitmap.getHeight());
setHasAlpha(bitmap, true); // if necessary
}
return bitmap;
}
libをロードし、ネイティブメソッドを宣言します。
static {
System.loadLibrary("bitmaputils");
}
private static native int setHasAlphaNative(Bitmap bitmap, boolean value);
ネイティブセクション('jni'フォルダー)
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bitmaputils
LOCAL_SRC_FILES := bitmap_utils.c
LOCAL_LDLIBS := -llog -ljnigraphics -lz -ldl -lgcc
include $(BUILD_SHARED_LIBRARY)
ビットマップユーティリティ.c:
#include <jni.h>
#include <android/bitmap.h>
#include <android/log.h>
#define LOG_TAG "BitmapTest"
#define Log_i(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define Log_e(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
// caching class and method IDs for a faster subsequent access
static jclass bitmap_class = 0;
static jmethodID setHasAlphaMethodID = 0;
jint Java_com_example_bitmaptest_MainActivity_setHasAlphaNative(JNIEnv * env, jclass clazz, jobject bitmap, jboolean value) {
AndroidBitmapInfo info;
void* pixels;
if (AndroidBitmap_getInfo(env, bitmap, &info) < 0) {
Log_e("Failed to get Bitmap info");
return -1;
}
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
Log_e("Incompatible Bitmap format");
return -1;
}
if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) {
Log_e("Failed to lock the pixels of the Bitmap");
return -1;
}
// get class
if(bitmap_class == NULL) { //initializing jclass
// NOTE: The class Bitmap exists since API level 1, so it just must be found.
bitmap_class = (*env)->GetObjectClass(env, bitmap);
if(bitmap_class == NULL) {
Log_e("bitmap_class == NULL");
return -2;
}
}
// get methodID
if(setHasAlphaMethodID == NULL) { //initializing jmethodID
// NOTE: If this fails, because the method could not be found the App will crash.
// But we only call this part of the code if the method was found using java.lang.Reflect
setHasAlphaMethodID = (*env)->GetMethodID(env, bitmap_class, "setHasAlpha", "(Z)V");
if(setHasAlphaMethodID == NULL) {
Log_e("methodID == NULL");
return -2;
}
}
// call java instance method
(*env)->CallVoidMethod(env, bitmap, setHasAlphaMethodID, value);
// if an exception was thrown we could handle it here
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
Log_e("calling setHasAlpha threw an exception");
return -2;
}
if(AndroidBitmap_unlockPixels(env, bitmap) < 0) {
Log_e("Failed to unlock the pixels of the Bitmap");
return -1;
}
return 0; // success
}
それでおしまい。完了です。コピーアンドペーストの目的でコード全体を投稿しました。実際のコードはそれほど大きくはありませんが、これらすべてのパラノイドエラーチェックを行うと、コードははるかに大きくなります。これが誰にとっても役立つことを願っています。