安卓:无法创建位图createBitmap方法 [英] Android: can't create Bitmap with createBitmap method

查看:1108
本文介绍了安卓:无法创建位图createBitmap方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的Andr​​oid平台上编程。我有一些问题按摩int数组把它变成一个位图。

I'm fairly new to programming on the android platform. I'm having some problems massaging an int array to turn it into a Bitmap.

的int数组中的每个元素是一个从0到255,并重新presents一个灰度像素的颜色(0所以将是黑色,255是白色的,等等)。这是我的code:

Each element in the int array is a number from 0 to 255, and represents a grey-scale pixel color (so 0 would be black, and 255 would be white, etc...). Here's my code:

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功能不工作。什么是错我的方法?

The resulting bitmap has height and width of -1, which indicates that the createBitmap function didn't work. What is wrong with my method?

呵呵,我把转化部分从<一个在进行颜色href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CreateBitmap.html" rel="nofollow">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CreateBitmap.html.

Oh, I took the shifting part for making the color from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CreateBitmap.html.

编辑:认为它可能已经因为阿尔法是256而不是255,但我仍然得到同样的错误

thought it might've been because alpha was 256 instead of 255, but I still get the same error.

推荐答案

你怎么知道你的位图有[-1 -1]的大小? 要获取大小,使用:

How do you learn that your Bitmap has [-1 -1] size? To get size, use:

int w = bm.getWidth();
int h = bm.getHeight();

这是工作的罚款。

这篇关于安卓:无法创建位图createBitmap方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆