我對android平臺上的編程相當陌生。我在按摩一個int數組時會遇到一些問題,將它變成一個Bitmap。Android:無法使用createBitmap方法創建位圖
int數組中的每個元素都是一個從0到255的數字,並且表示灰度像素顏色(所以0會是黑色的,255會是白色的,等等)。這是我的代碼:
int nPixels = 262144;
int width = 512;
int height = 512;
int[] converted = new int[nPixels];
int alpha = 255;
for (int i = 0; i < nPixels; i++) {
//greyscale, so all r, g, and b have the same value
converted[i] = (alpha << 24) | (pixels[i] << 16) | (pixels[i] << 8) | pixels[i];
}
Bitmap bm = Bitmap.createBitmap(converted, width, height, Bitmap.Config.ARGB_8888);
生成的位圖的高度和寬度都是-1,表示createBitmap函數不起作用。我的方法有什麼問題?
哦,我拿了從http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CreateBitmap.html製作顏色的轉換部分。
編輯:以爲它可能是因爲alpha是256而不是255,但我仍然得到相同的錯誤。
對「顏色」數組定義的位置不太清楚,我看到「已轉換」數組,但未顯示「顏色」。 – satur9nine
哦,對不起,那是我剛剛粘貼的另一部分代碼。 'converted' ='colors' – confusedKid
當我測試它時,你的代碼似乎運行良好。 – satur9nine