java - 如何创建 ARGB_8888 像素值?

标签 java android bitmap pixel

假设我想创建一个像素值数组以传递到描述的 createBitmap 方法 here .我有 0 - 0xff 范围内的三个 int 值 r, g, b。如何将它们转换为不透明像素 p

alpha channel 是高位还是低位?

我用谷歌搜索了 documentation但它只说明:

Each pixel is stored on 4 bytes. Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values.) This configuration is very flexible and offers the best quality. It should be used whenever possible.

那么,这个方法怎么写呢?

int createPixel(int r, int g, int b)
{
  return ?
}

最佳答案

它看起来像 RGBA pixel format有很好的记录,我假设这就是 Android 文档的意思,只是使用不同的名称来匹配位字段中的 channel 成员位置:

enter image description here

像这样的东西应该可以工作:

int createPixel(int r, int g, int b) {
  return createPixel(r, g, b, 0xff);
}

int createPixel(int r, int g, int b, int a) {
  return (a<<24) | (r<<16) | (g<<8) | b;
}

此外,您可能希望使用 byte 而不是 int 来避免溢出类型的错误,或者只屏蔽您想要的位:

int createPixel(int r, int g, int b, int a) {
  return ((a & 0xff) << 24)
       | ((r & 0xff) << 16)
       | ((g & 0xff) << 8)
       | ((b & 0xff));
}

虽然将值限制在 [0,255] 可能更有意义。

关于java - 如何创建 ARGB_8888 像素值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246566/

相关文章:

java - 有没有办法解决使用 bouncycaSTLe 提供程序后连接重置问题

java - 带有像样的 gui 工具包的跨平台编程语言?

Java 和 Python 归并排序

android - Espresso android - IllegalStateException : UiAutomationService android.accessibilityservice.IAccessibilityServiceClient 已经注册

android - 有没有支持Android图片加水印的库?

java - Java LibGdx 中的图像点击事件

java - 运行react-native run-android时出现错误 react native

java - 暂停和恢复 Android 游戏 ->

java - 从位图更改位深度

java - 安卓、Java : Resized Bitmap ignores bitmap options