【问题标题】:grayscale image displays red - android灰度图像显示红色 - android
【发布时间】:2015-10-16 01:22:36
【问题描述】:

我有一些奇怪的问题。我从int[] 像素阵列设置了一个灰度Bitmap。所以我把这个

    bitmap = Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
    byte[] byteArray = new String(pixelsArray).getBytes("UTF-8");
    Bitmap bitmap = getBitmapFromByte(byteArray, pixelsArray.length);
    ImageView theImage = (ImageView) findViewById(R.id.echo);
    theImage.setImageBitmap(bitmap);

问题是我得到了正确的图像,但是是红色的。所以我认为这与保存位图的ImageView 包装器有关。

<LinearLayout
        android:id="@+id/some_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/theme_blue"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/echo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/some_image" />

所以我放了

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.vMiddle);
linearLayout.setBackgroundColor(Color.BLACK);

但它仍然显示为红色。有人有想法吗?顺便说一句,我在一个测试纯 java 应用程序上测试了 int[] 像素数组,它以灰度颜色正确显示。

【问题讨论】:

    标签: java android bitmap pixel


    【解决方案1】:

    使用 ColorMatrix 和 ColorMatrixColorFilter 将 ImageView 的内容设置为灰度。

    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0);
    
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
    

    然后简单地设置 ImageView 来使用过滤器。

    ImageView imageView = (ImageView)findViewById(R.id.imageView);
    imageView.setColorFilter(filter);
    

    这将使 ImageView 的内容变为灰度。

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 1970-01-01
      • 2014-04-21
      • 2017-05-07
      • 1970-01-01
      • 2011-11-30
      • 2020-10-08
      • 2011-10-24
      相关资源
      最近更新 更多